Add admin interface
This commit is contained in:
@@ -59,6 +59,7 @@ func (s *Server) routes() http.Handler {
|
||||
mux.HandleFunc("GET /{$}", s.handleIndex)
|
||||
mux.HandleFunc("POST /api/upload", s.handleUpload)
|
||||
mux.HandleFunc("GET /api/limits", s.handleLimits)
|
||||
mux.HandleFunc("GET /admin", s.handleAdmin)
|
||||
mux.HandleFunc("GET /d/{id}", s.handleDownload)
|
||||
mux.HandleFunc("GET /i/{id}", s.handleInfo)
|
||||
mux.HandleFunc("POST /api/d/{id}/delete", s.handleDelete)
|
||||
@@ -150,7 +151,7 @@ func bearer(r *http.Request) string {
|
||||
|
||||
// --- rendering -----------------------------------------------------------
|
||||
|
||||
var pageNames = []string{"index.html", "result.html", "info.html", "error.html"}
|
||||
var pageNames = []string{"index.html", "result.html", "info.html", "error.html", "admin.html"}
|
||||
|
||||
// parsePages pairs each page with the shared layout. They cannot all be parsed
|
||||
// into one template set because every page defines "content".
|
||||
@@ -172,10 +173,17 @@ type page struct {
|
||||
Base string
|
||||
Title string
|
||||
Script bool
|
||||
Admin bool // show the administration link in the header
|
||||
}
|
||||
|
||||
func (s *Server) page(title string, script bool) page {
|
||||
return page{Base: s.cfg.BasePath, Title: title, Script: script}
|
||||
// page builds the common fields, resolving whether the caller is an admin so
|
||||
// the header can offer the link only to someone who can use it.
|
||||
func (s *Server) page(r *http.Request, title string, script bool) page {
|
||||
admin := false
|
||||
if lim, err := s.limitsFor(credential(r)); err == nil {
|
||||
admin = lim.Admin
|
||||
}
|
||||
return page{Base: s.cfg.BasePath, Title: title, Script: script, Admin: admin}
|
||||
}
|
||||
|
||||
func (s *Server) render(w http.ResponseWriter, status int, name string, data any) {
|
||||
@@ -222,7 +230,7 @@ func (s *Server) fail(w http.ResponseWriter, r *http.Request, status int, msg st
|
||||
return
|
||||
}
|
||||
s.render(w, status, "error.html", errorPage{
|
||||
page: s.page(http.StatusText(status), false),
|
||||
page: s.page(r, http.StatusText(status), false),
|
||||
Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),
|
||||
Message: msg,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user