| 93 | } |
| 94 | |
| 95 | func (api *API) handle(method, route string, handler func(_ http.ResponseWriter, r *http.Request) Response) { |
| 96 | api.router.Methods(method).Path(route).HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 | log.Println("handling", r.Method, r.URL.String()) |
| 98 | resp := handler(w, r) |
| 99 | if resp.Error == "" { |
| 100 | resp.OK = true |
| 101 | } |
| 102 | if resp.Status > 0 { |
| 103 | w.WriteHeader(resp.Status) |
| 104 | } |
| 105 | w.Header().Add("content-type", "application/json") |
| 106 | enc := json.NewEncoder(w) |
| 107 | enc.Encode(resp) |
| 108 | }) |
| 109 | } |