From fd0d7ddee6190e73d4e2b423cc97096fd50acc61 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 2 Jan 2021 12:25:34 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Fix=20admin=20url=20requiring=20?= =?UTF-8?q?a=20slash=20at=20the=20end?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd_serve.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd_serve.go b/cmd_serve.go index 8073bcb..e70ff83 100644 --- a/cmd_serve.go +++ b/cmd_serve.go @@ -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