@@ -10,15 +10,15 @@ import (
1010
1111const internalServerError = "Internal Server Error"
1212
13- type CodeHandlerConfig struct {
13+ type HandlerConfig struct {
1414 Master * bool `mapstructure:"master" json:"master,omitempty" gorm:"column:master" bson:"master,omitempty" dynamodbav:"master,omitempty" firestore:"master,omitempty"`
1515 Id string `mapstructure:"id" json:"id,omitempty" gorm:"column:id" bson:"id,omitempty" dynamodbav:"id,omitempty" firestore:"id,omitempty"`
1616 Name string `mapstructure:"name" json:"name,omitempty" gorm:"column:name" bson:"name,omitempty" dynamodbav:"name,omitempty" firestore:"name,omitempty"`
1717 Resource string `mapstructure:"resource" json:"resource,omitempty" gorm:"column:resource" bson:"resource,omitempty" dynamodbav:"resource,omitempty" firestore:"resource,omitempty"`
1818 Action string `mapstructure:"action" json:"action,omitempty" gorm:"column:action" bson:"action,omitempty" dynamodbav:"action,omitempty" firestore:"action,omitempty"`
1919}
20- type CodeHandler struct {
21- Codes func (ctx context.Context , master string ) ([]CodeModel , error )
20+ type Handler struct {
21+ Codes func (ctx context.Context , master string ) ([]Model , error )
2222 RequiredMaster bool
2323 Error func (context.Context , string )
2424 Log func (ctx context.Context , resource string , action string , success bool , desc string ) error
@@ -28,14 +28,14 @@ type CodeHandler struct {
2828 Name string
2929}
3030
31- func NewDefaultCodeHandler (load func (ctx context.Context , master string ) ([]CodeModel , error ), logError func (context.Context , string ), options ... func (context.Context , string , string , bool , string ) error ) * CodeHandler {
31+ func NewDefaultCodeHandler (load func (ctx context.Context , master string ) ([]Model , error ), logError func (context.Context , string ), options ... func (context.Context , string , string , bool , string ) error ) * Handler {
3232 var writeLog func (context.Context , string , string , bool , string ) error
3333 if len (options ) >= 1 {
3434 writeLog = options [0 ]
3535 }
3636 return NewCodeHandlerWithLog (load , logError , true , writeLog , "" , "" )
3737}
38- func NewCodeHandlerByConfig (load func (ctx context.Context , master string ) ([]CodeModel , error ), c CodeHandlerConfig , logError func (context.Context , string ), options ... func (context.Context , string , string , bool , string ) error ) * CodeHandler {
38+ func NewCodeHandlerByConfig (load func (ctx context.Context , master string ) ([]Model , error ), c HandlerConfig , logError func (context.Context , string ), options ... func (context.Context , string , string , bool , string ) error ) * Handler {
3939 var requireMaster bool
4040 if c .Master != nil {
4141 requireMaster = * c .Master
@@ -51,14 +51,14 @@ func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]Cod
5151 h .Name = c .Name
5252 return h
5353}
54- func NewCodeHandler (load func (ctx context.Context , master string ) ([]CodeModel , error ), logError func (context.Context , string ), requiredMaster bool , options ... func (context.Context , string , string , bool , string ) error ) * CodeHandler {
54+ func NewCodeHandler (load func (ctx context.Context , master string ) ([]Model , error ), logError func (context.Context , string ), requiredMaster bool , options ... func (context.Context , string , string , bool , string ) error ) * Handler {
5555 var writeLog func (context.Context , string , string , bool , string ) error
5656 if len (options ) >= 1 {
5757 writeLog = options [0 ]
5858 }
5959 return NewCodeHandlerWithLog (load , logError , requiredMaster , writeLog , "" , "" )
6060}
61- func NewCodeHandlerWithLog (load func (ctx context.Context , master string ) ([]CodeModel , error ), logError func (context.Context , string ), requiredMaster bool , writeLog func (context.Context , string , string , bool , string ) error , options ... string ) * CodeHandler {
61+ func NewCodeHandlerWithLog (load func (ctx context.Context , master string ) ([]Model , error ), logError func (context.Context , string ), requiredMaster bool , writeLog func (context.Context , string , string , bool , string ) error , options ... string ) * Handler {
6262 var resource , action string
6363 if len (options ) >= 1 && len (options [0 ]) > 0 {
6464 resource = options [0 ]
@@ -70,12 +70,12 @@ func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]Code
7070 } else {
7171 action = "load"
7272 }
73- h := CodeHandler {Codes : load , Resource : resource , Action : action , RequiredMaster : requiredMaster , Log : writeLog , Error : logError }
73+ h := Handler {Codes : load , Resource : resource , Action : action , RequiredMaster : requiredMaster , Log : writeLog , Error : logError }
7474 return & h
7575}
76- func (c * CodeHandler ) Load (w http.ResponseWriter , r * http.Request ) {
76+ func (h * Handler ) Load (w http.ResponseWriter , r * http.Request ) {
7777 code := ""
78- if c .RequiredMaster {
78+ if h .RequiredMaster {
7979 if r .Method == "GET" {
8080 i := strings .LastIndex (r .RequestURI , "/" )
8181 if i >= 0 {
@@ -84,34 +84,30 @@ func (c *CodeHandler) Load(w http.ResponseWriter, r *http.Request) {
8484 } else {
8585 b , er1 := ioutil .ReadAll (r .Body )
8686 if er1 != nil {
87- respondString (w , r , http . StatusBadRequest , "Body cannot is empty" )
87+ http . Error (w , "Body cannot is empty" , http . StatusBadRequest )
8888 return
8989 }
9090 code = strings .Trim (string (b ), " " )
9191 }
9292 }
93- result , er4 := c .Codes (r .Context (), code )
93+ result , er4 := h .Codes (r .Context (), code )
9494 if er4 != nil {
95- respondError (w , r , http .StatusInternalServerError , internalServerError , c .Error , c .Resource , c .Action , er4 , c .Log )
95+ respondError (w , r , http .StatusInternalServerError , internalServerError , h .Error , h .Resource , h .Action , er4 , h .Log )
9696 } else {
97- if len (c .Id ) == 0 && len (c .Name ) == 0 {
98- succeed (w , r , http .StatusOK , result , c .Log , c .Resource , c .Action )
97+ if len (h .Id ) == 0 && len (h .Name ) == 0 {
98+ succeed (w , r , http .StatusOK , result , h .Log , h .Resource , h .Action )
9999 } else {
100100 rs := make ([]map [string ]string , 0 )
101101 for _ , r := range result {
102102 m := make (map [string ]string )
103- m [c .Id ] = r .Id
104- m [c .Name ] = r .Name
103+ m [h .Id ] = r .Id
104+ m [h .Name ] = r .Name
105105 rs = append (rs , m )
106106 }
107- succeed (w , r , http .StatusOK , rs , c .Log , c .Resource , c .Action )
107+ succeed (w , r , http .StatusOK , rs , h .Log , h .Resource , h .Action )
108108 }
109109 }
110110}
111- func respondString (w http.ResponseWriter , r * http.Request , code int , result string ) {
112- w .WriteHeader (code )
113- w .Write ([]byte (result ))
114- }
115111func 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 ) {
116112 response , _ := json .Marshal (result )
117113 w .Header ().Set ("Content-Type" , "application/json" )
0 commit comments