diff --git a/handler.go b/handler.go index f65c775..823a966 100644 --- a/handler.go +++ b/handler.go @@ -152,17 +152,21 @@ func walk(prefix, fpath, givenTitle, givenColor string, w http.ResponseWriter, r // Get the path to a parent folder parentFolder := "" - if p := path.Join(prefix, r.URL.Path); p != "/" { + if p := path.Join(prefix, r.URL.Path); p != "/" && p != prefix { // If the path is not root, we're in a folder, but since folders // are enforced to use trailing slash then we need to remove it // so path.Dir() can work parentFolder = path.Dir(strings.TrimSuffix(p, "/")) - } + if !strings.HasSuffix(parentFolder, "/") { + parentFolder += "/" + } - // Update prefix to be nothing if it's just "/" - // if prefix == "/" { - // prefix = "" - // } + // Remove the parent folder if there's a prefix, so we don't have a parent for + // a root in a prefixed environment + if parentFolder == "/" && prefix != "/" { + parentFolder = "" + } + } // If we reached this point, we're ready to print the template // so we create a bag, and we save the information there diff --git a/main.go b/main.go index 68d7061..e696ebd 100644 --- a/main.go +++ b/main.go @@ -108,6 +108,12 @@ func main() { // Create the file server http.Handle(fileServerPrefix, logrequest(handler(fileServerPrefix, fileServerPath, givenTitle, givenColor))) + // Check whether or not the fileServerPrefix is set, if so, then + // simply create a temporary redirect to the new path + if fileServerPrefix != "/" { + http.Handle("/", logrequest(redirect("/", fileServerPrefix))) + } + // Graceful shutdown sigquit := make(chan os.Signal, 1) signal.Notify(sigquit, os.Interrupt, os.Kill) diff --git a/redirect.go b/redirect.go new file mode 100644 index 0000000..205a7d8 --- /dev/null +++ b/redirect.go @@ -0,0 +1,16 @@ +package main + +import ( + "net/http" +) + +func redirect(expectedPath, nextURL string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != expectedPath { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + + http.Redirect(w, r, nextURL, http.StatusFound) + }) +}