|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + co "github.com/core-go/code" |
| 6 | + "github.com/labstack/echo/v4" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const internalServerError = "Internal Server Error" |
| 13 | + |
| 14 | +type Handler struct { |
| 15 | + Codes func(ctx context.Context, master string) ([]co.Model, error) |
| 16 | + RequiredMaster bool |
| 17 | + Error func(context.Context, string) |
| 18 | + Log func(ctx context.Context, resource string, action string, success bool, desc string) error |
| 19 | + Resource string |
| 20 | + Action string |
| 21 | + Id string |
| 22 | + Name string |
| 23 | +} |
| 24 | + |
| 25 | +func NewDefaultCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler { |
| 26 | + var writeLog func(context.Context, string, string, bool, string) error |
| 27 | + if len(options) >= 1 { |
| 28 | + writeLog = options[0] |
| 29 | + } |
| 30 | + return NewCodeHandlerWithLog(load, logError, true, writeLog, "", "") |
| 31 | +} |
| 32 | +func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]co.Model, error), c co.HandlerConfig, logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler { |
| 33 | + var requireMaster bool |
| 34 | + if c.Master != nil { |
| 35 | + requireMaster = *c.Master |
| 36 | + } else { |
| 37 | + requireMaster = true |
| 38 | + } |
| 39 | + var writeLog func(context.Context, string, string, bool, string) error |
| 40 | + if len(options) >= 1 { |
| 41 | + writeLog = options[0] |
| 42 | + } |
| 43 | + h := NewCodeHandlerWithLog(load, logError, requireMaster, writeLog, c.Resource, c.Action) |
| 44 | + h.Id = c.Id |
| 45 | + h.Name = c.Name |
| 46 | + return h |
| 47 | +} |
| 48 | +func NewCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, options ...func(context.Context, string, string, bool, string) error) *Handler { |
| 49 | + var writeLog func(context.Context, string, string, bool, string) error |
| 50 | + if len(options) >= 1 { |
| 51 | + writeLog = options[0] |
| 52 | + } |
| 53 | + return NewCodeHandlerWithLog(load, logError, requiredMaster, writeLog, "", "") |
| 54 | +} |
| 55 | +func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, writeLog func(context.Context, string, string, bool, string) error, options ...string) *Handler { |
| 56 | + var resource, action string |
| 57 | + if len(options) >= 1 && len(options[0]) > 0 { |
| 58 | + resource = options[0] |
| 59 | + } else { |
| 60 | + resource = "code" |
| 61 | + } |
| 62 | + if len(options) >= 2 && len(options[1]) > 0 { |
| 63 | + action = options[1] |
| 64 | + } else { |
| 65 | + action = "load" |
| 66 | + } |
| 67 | + h := Handler{Codes: load, Resource: resource, Action: action, RequiredMaster: requiredMaster, Log: writeLog, Error: logError} |
| 68 | + return &h |
| 69 | +} |
| 70 | +func (h *Handler) Load() echo.HandlerFunc { |
| 71 | + return func(ctx echo.Context) error { |
| 72 | + r := ctx.Request() |
| 73 | + code := "" |
| 74 | + if h.RequiredMaster { |
| 75 | + if r.Method == "GET" { |
| 76 | + i := strings.LastIndex(r.RequestURI, "/") |
| 77 | + if i >= 0 { |
| 78 | + code = r.RequestURI[i+1:] |
| 79 | + } |
| 80 | + } else { |
| 81 | + b, er1 := ioutil.ReadAll(r.Body) |
| 82 | + if er1 != nil { |
| 83 | + ctx.String(http.StatusBadRequest, "Body cannot is empty") |
| 84 | + return er1 |
| 85 | + } |
| 86 | + code = strings.Trim(string(b), " ") |
| 87 | + } |
| 88 | + } |
| 89 | + result, er4 := h.Codes(r.Context(), code) |
| 90 | + if er4 != nil { |
| 91 | + return respondError(ctx, http.StatusInternalServerError, internalServerError, h.Error, h.Resource, h.Action, er4, h.Log) |
| 92 | + } else { |
| 93 | + if len(h.Id) == 0 && len(h.Name) == 0 { |
| 94 | + return succeed(ctx, http.StatusOK, result, h.Log, h.Resource, h.Action) |
| 95 | + } else { |
| 96 | + rs := make([]map[string]string, 0) |
| 97 | + for _, r := range result { |
| 98 | + m := make(map[string]string) |
| 99 | + m[h.Id] = r.Id |
| 100 | + m[h.Name] = r.Name |
| 101 | + rs = append(rs, m) |
| 102 | + } |
| 103 | + return succeed(ctx, http.StatusOK, rs, h.Log, h.Resource, h.Action) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func respond(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error { |
| 110 | + err := ctx.JSON(code, result) |
| 111 | + if writeLog != nil { |
| 112 | + writeLog(ctx.Request().Context(), resource, action, success, desc) |
| 113 | + } |
| 114 | + return err |
| 115 | +} |
| 116 | +func respondError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string), resource string, action string, err error, writeLog func(context.Context, string, string, bool, string) error) error { |
| 117 | + if logError != nil { |
| 118 | + logError(ctx.Request().Context(), err.Error()) |
| 119 | + } |
| 120 | + respond(ctx, code, result, writeLog, resource, action, false, err.Error()) |
| 121 | + return err |
| 122 | +} |
| 123 | +func succeed(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error { |
| 124 | + return respond(ctx, code, result, writeLog, resource, action, true, "") |
| 125 | +} |
0 commit comments