User handles requests made in the web application by the authenticated user. This provides user-friendly HTML pages and actions that work in the browser.
(f userHandlerFunc)
| 130 | // User handles requests made in the web application by the authenticated user. |
| 131 | // This provides user-friendly HTML pages and actions that work in the browser. |
| 132 | func (h *Handler) User(f userHandlerFunc) http.HandlerFunc { |
| 133 | return func(w http.ResponseWriter, r *http.Request) { |
| 134 | h.handleHTTPError(w, r, func() error { |
| 135 | var status int |
| 136 | start := time.Now() |
| 137 | |
| 138 | defer func() { |
| 139 | if e := recover(); e != nil { |
| 140 | log.Error("%s: %s", e, debug.Stack()) |
| 141 | h.errors.InternalServerError.ExecuteTemplate(w, "base", pageForReq(h.app.App(), r)) |
| 142 | status = http.StatusInternalServerError |
| 143 | } |
| 144 | |
| 145 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 146 | }() |
| 147 | |
| 148 | u := getUserSession(h.app.App(), r) |
| 149 | if u == nil { |
| 150 | err := ErrNotLoggedIn |
| 151 | status = err.Status |
| 152 | return err |
| 153 | } |
| 154 | |
| 155 | err := f(h.app.App(), u, w, r) |
| 156 | if err == nil { |
| 157 | status = http.StatusOK |
| 158 | } else if impErr, ok := err.(impart.HTTPError); ok { |
| 159 | status = impErr.Status |
| 160 | if impErr == ErrUserNotFound { |
| 161 | log.Info("Logged-in user not found. Logging out.") |
| 162 | sendRedirect(w, http.StatusFound, "/me/logout?to="+h.app.App().cfg.App.LandingPath()) |
| 163 | // Reset err so handleHTTPError does nothing |
| 164 | err = nil |
| 165 | } |
| 166 | } else { |
| 167 | status = http.StatusInternalServerError |
| 168 | } |
| 169 | |
| 170 | return err |
| 171 | }()) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Admin handles requests on /admin routes |
| 176 | func (h *Handler) Admin(f userHandlerFunc) http.HandlerFunc { |
no test coverage detected