|
| 1 | +package code |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const InternalServerError = "Internal Server Error" |
| 12 | + |
| 13 | +type CodeHandler struct { |
| 14 | + Loader CodeLoader |
| 15 | + Resource string |
| 16 | + Action string |
| 17 | + RequiredMaster bool |
| 18 | + LogError func(context.Context, string) |
| 19 | + LogWriter LogWriter |
| 20 | +} |
| 21 | + |
| 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) |
| 24 | +} |
| 25 | +func NewCodeHandler(loader CodeLoader, resource string, action string, requiredMaster bool, logError func(context.Context, string), logWriter LogWriter) *CodeHandler { |
| 26 | + if len(resource) == 0 { |
| 27 | + resource = "code" |
| 28 | + } |
| 29 | + if len(action) == 0 { |
| 30 | + action = "load" |
| 31 | + } |
| 32 | + h := CodeHandler{Loader: loader, Resource: resource, Action: action, RequiredMaster: requiredMaster, LogWriter: logWriter, LogError: logError} |
| 33 | + return &h |
| 34 | +} |
| 35 | +func (c *CodeHandler) Load(w http.ResponseWriter, r *http.Request) { |
| 36 | + code := "" |
| 37 | + if c.RequiredMaster { |
| 38 | + if r.Method == "GET" { |
| 39 | + i := strings.LastIndex(r.RequestURI, "/") |
| 40 | + if i >= 0 { |
| 41 | + code = r.RequestURI[i+1:] |
| 42 | + } |
| 43 | + } else { |
| 44 | + b, er1 := ioutil.ReadAll(r.Body) |
| 45 | + if er1 != nil { |
| 46 | + respondString(w, r, http.StatusBadRequest, "Body cannot is empty") |
| 47 | + return |
| 48 | + } |
| 49 | + code = strings.Trim(string(b), " ") |
| 50 | + } |
| 51 | + } |
| 52 | + result, er4 := c.Loader.Load(r.Context(), code) |
| 53 | + if er4 != nil { |
| 54 | + respondError(w, r, http.StatusInternalServerError, InternalServerError, c.LogError, c.Resource, c.Action, er4, c.LogWriter) |
| 55 | + } else { |
| 56 | + succeed(w, r, http.StatusOK, result, c.LogWriter, c.Resource, c.Action) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type LogWriter interface { |
| 61 | + Write(ctx context.Context, resource string, action string, success bool, desc string) error |
| 62 | +} |
| 63 | + |
| 64 | +func respondString(w http.ResponseWriter, r *http.Request, code int, result string) { |
| 65 | + w.WriteHeader(code) |
| 66 | + w.Write([]byte(result)) |
| 67 | +} |
| 68 | +func respond(w http.ResponseWriter, r *http.Request, code int, result interface{}, logWriter LogWriter, resource string, action string, success bool, desc string) { |
| 69 | + response, _ := json.Marshal(result) |
| 70 | + w.Header().Set("Content-Type", "application/json") |
| 71 | + w.WriteHeader(code) |
| 72 | + w.Write(response) |
| 73 | + if logWriter != nil { |
| 74 | + logWriter.Write(r.Context(), resource, action, success, desc) |
| 75 | + } |
| 76 | +} |
| 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) { |
| 78 | + if logError != nil { |
| 79 | + logError(r.Context(), err.Error()) |
| 80 | + } |
| 81 | + respond(w, r, code, result, logWriter, resource, action, false, err.Error()) |
| 82 | +} |
| 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, "") |
| 85 | +} |
0 commit comments