Admin handles requests on /admin routes
(f userHandlerFunc)
| 174 | |
| 175 | // Admin handles requests on /admin routes |
| 176 | func (h *Handler) Admin(f userHandlerFunc) http.HandlerFunc { |
| 177 | return func(w http.ResponseWriter, r *http.Request) { |
| 178 | h.handleHTTPError(w, r, func() error { |
| 179 | var status int |
| 180 | start := time.Now() |
| 181 | |
| 182 | defer func() { |
| 183 | if e := recover(); e != nil { |
| 184 | log.Error("%s: %s", e, debug.Stack()) |
| 185 | h.errors.InternalServerError.ExecuteTemplate(w, "base", pageForReq(h.app.App(), r)) |
| 186 | status = http.StatusInternalServerError |
| 187 | } |
| 188 | |
| 189 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 190 | }() |
| 191 | |
| 192 | u := getUserSession(h.app.App(), r) |
| 193 | if u == nil || !u.IsAdmin() { |
| 194 | err := impart.HTTPError{http.StatusNotFound, ""} |
| 195 | status = err.Status |
| 196 | return err |
| 197 | } |
| 198 | |
| 199 | err := f(h.app.App(), u, w, r) |
| 200 | if err == nil { |
| 201 | status = http.StatusOK |
| 202 | } else if err, ok := err.(impart.HTTPError); ok { |
| 203 | status = err.Status |
| 204 | } else { |
| 205 | status = http.StatusInternalServerError |
| 206 | } |
| 207 | |
| 208 | return err |
| 209 | }()) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // AdminApper handles requests on /admin routes that require an Apper. |
| 214 | func (h *Handler) AdminApper(f userApperHandlerFunc) http.HandlerFunc { |
no test coverage detected