Skip to content

Commit

Permalink
Add redirection, fixes parent prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Apr 6, 2018
1 parent dc052de commit 90956c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 10 additions & 6 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 90956c4

Please sign in to comment.