🚸 Fix admin url requiring a slash at the end

This commit is contained in:
Andreas Schneider 2021-01-02 12:25:34 +01:00
parent a8b2db986a
commit fd0d7ddee6
1 changed files with 9 additions and 0 deletions

View File

@ -98,7 +98,16 @@ func (cmd *CmdServe) Run(app *app) error {
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "mapping", &directoryMapping)))
})
// Setup the admin endpoint with convenience routing. (Should the user omit the trailing slash, we add
// it for them.)
r := chi.NewRouter()
adminPathWithoutSlash := strings.TrimSuffix(cmd.AdminPath, "/")
if !strings.HasSuffix(cmd.AdminPath, "/") {
cmd.AdminPath = cmd.AdminPath + "/"
}
r.Get(adminPathWithoutSlash, func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, cmd.AdminPath, http.StatusMovedPermanently)
})
r.Mount(cmd.AdminPath, newWebAdminHandler(app))
// Can't use Chi at the root, since it can't properly handle the mounted WebDAV