@@ -8,28 +8,28 @@ import (
88 "strings"
99)
1010
11- const InternalServerError = "Internal Server Error"
11+ const internalServerError = "Internal Server Error"
1212
1313type CodeHandler struct {
1414 Loader CodeLoader
1515 Resource string
1616 Action string
1717 RequiredMaster bool
1818 LogError func (context.Context , string )
19- LogWriter LogWriter
19+ WriteLog func ( ctx context. Context , resource string , action string , success bool , desc string ) error
2020}
2121
22- func NewDefaultCodeHandler (loader CodeLoader , resource string , action string , logError func (context.Context , string ), logWriter LogWriter ) * CodeHandler {
23- return NewCodeHandler (loader , resource , action , true , logError , logWriter )
22+ func NewDefaultCodeHandler (loader CodeLoader , resource string , action string , logError func (context.Context , string ), writeLog func (context. Context , string , string , bool , string ) error ) * CodeHandler {
23+ return NewCodeHandler (loader , resource , action , true , logError , writeLog )
2424}
25- func NewCodeHandler (loader CodeLoader , resource string , action string , requiredMaster bool , logError func (context.Context , string ), logWriter LogWriter ) * CodeHandler {
25+ func NewCodeHandler (loader CodeLoader , resource string , action string , requiredMaster bool , logError func (context.Context , string ), writeLog func (context. Context , string , string , bool , string ) error ) * CodeHandler {
2626 if len (resource ) == 0 {
2727 resource = "code"
2828 }
2929 if len (action ) == 0 {
3030 action = "load"
3131 }
32- h := CodeHandler {Loader : loader , Resource : resource , Action : action , RequiredMaster : requiredMaster , LogWriter : logWriter , LogError : logError }
32+ h := CodeHandler {Loader : loader , Resource : resource , Action : action , RequiredMaster : requiredMaster , WriteLog : writeLog , LogError : logError }
3333 return & h
3434}
3535func (c * CodeHandler ) Load (w http.ResponseWriter , r * http.Request ) {
@@ -51,35 +51,31 @@ func (c *CodeHandler) Load(w http.ResponseWriter, r *http.Request) {
5151 }
5252 result , er4 := c .Loader .Load (r .Context (), code )
5353 if er4 != nil {
54- respondError (w , r , http .StatusInternalServerError , InternalServerError , c .LogError , c .Resource , c .Action , er4 , c .LogWriter )
54+ respondError (w , r , http .StatusInternalServerError , internalServerError , c .LogError , c .Resource , c .Action , er4 , c .WriteLog )
5555 } else {
56- succeed (w , r , http .StatusOK , result , c .LogWriter , c .Resource , c .Action )
56+ succeed (w , r , http .StatusOK , result , c .WriteLog , c .Resource , c .Action )
5757 }
5858}
5959
60- type LogWriter interface {
61- Write (ctx context.Context , resource string , action string , success bool , desc string ) error
62- }
63-
6460func respondString (w http.ResponseWriter , r * http.Request , code int , result string ) {
6561 w .WriteHeader (code )
6662 w .Write ([]byte (result ))
6763}
68- func respond (w http.ResponseWriter , r * http.Request , code int , result interface {}, logWriter LogWriter , resource string , action string , success bool , desc string ) {
64+ func respond (w http.ResponseWriter , r * http.Request , code int , result interface {}, writeLog func (context. Context , string , string , bool , string ) error , resource string , action string , success bool , desc string ) {
6965 response , _ := json .Marshal (result )
7066 w .Header ().Set ("Content-Type" , "application/json" )
7167 w .WriteHeader (code )
7268 w .Write (response )
73- if logWriter != nil {
74- logWriter . Write (r .Context (), resource , action , success , desc )
69+ if writeLog != nil {
70+ writeLog (r .Context (), resource , action , success , desc )
7571 }
7672}
77- func respondError (w http.ResponseWriter , r * http.Request , code int , result interface {}, logError func (context.Context , string ), resource string , action string , err error , logWriter LogWriter ) {
73+ func respondError (w http.ResponseWriter , r * http.Request , code int , result interface {}, logError func (context.Context , string ), resource string , action string , err error , writeLog func (context. Context , string , string , bool , string ) error ) {
7874 if logError != nil {
7975 logError (r .Context (), err .Error ())
8076 }
81- respond (w , r , code , result , logWriter , resource , action , false , err .Error ())
77+ respond (w , r , code , result , writeLog , resource , action , false , err .Error ())
8278}
83- func succeed (w http.ResponseWriter , r * http.Request , code int , result interface {}, logWriter LogWriter , resource string , action string ) {
84- respond (w , r , code , result , logWriter , resource , action , true , "" )
79+ func succeed (w http.ResponseWriter , r * http.Request , code int , result interface {}, writeLog func (context. Context , string , string , bool , string ) error , resource string , action string ) {
80+ respond (w , r , code , result , writeLog , resource , action , true , "" )
8581}
0 commit comments