MCPcopy
hub / github.com/writefreely/writefreely / Admin

Method Admin

handle.go:176–211  ·  view source on GitHub ↗

Admin handles requests on /admin routes

(f userHandlerFunc)

Source from the content-addressed store, hash-verified

174
175// Admin handles requests on /admin routes
176func (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.
214func (h *Handler) AdminApper(f userApperHandlerFunc) http.HandlerFunc {

Callers 1

InitRoutesFunction · 0.80

Calls 7

handleHTTPErrorMethod · 0.95
pageForReqFunction · 0.85
getUserSessionFunction · 0.85
IsAdminMethod · 0.80
AppMethod · 0.65
ReqLogMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected