main.go: fixed routing for url with dots

When a url contained a dot and the user refreshed the site, the server
would have returned a 404. Now it will always return index.html, if the
requested file is not found.
pull/3/head
leonnicolas 4 years ago
parent bd3a10bad8
commit f9ec2419ab
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -3,28 +3,26 @@ package main
import (
"log"
"net/http"
"strings"
"github.com/rakyll/statik/fs"
_ "github.com/fLotte-meets-HWR-DB/frontend/statik"
)
func strip(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.Path, ".") {
r.URL.Path = "/"
}
handler.ServeHTTP(w, r)
})
}
func main() {
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
fserver := http.FileServer(statikFS)
m := http.NewServeMux()
m.Handle("/", strip(http.FileServer(statikFS)))
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if _, err := statikFS.Open(r.URL.Path); err != nil {
r.URL.Path = "/"
}
fserver.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":8080", m))
}

Loading…
Cancel
Save