mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
web: Implement SPA compatible routing
This commit is contained in:
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
@@ -30,6 +31,29 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// fileHandler creates a handler that sends the named file regardless of the requested URL.
|
||||
func fileHandler(name string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
log.Error().Str("module", "web").Str("path", req.RequestURI).Str("file", name).Err(err).
|
||||
Msg("Error opening file")
|
||||
http.Error(w, "Error opening file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
d, err := f.Stat()
|
||||
if err != nil {
|
||||
log.Error().Str("module", "web").Str("path", req.RequestURI).Str("file", name).Err(err).
|
||||
Msg("Error stating file")
|
||||
http.Error(w, "Error opening file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.ServeContent(w, req, d.Name(), d.ModTime(), f)
|
||||
})
|
||||
}
|
||||
|
||||
// noMatchHandler creates a handler to log requests that Gorilla mux is unable to route,
|
||||
// returning specified statusCode to the client.
|
||||
func noMatchHandler(statusCode int, message string) http.Handler {
|
||||
|
||||
Reference in New Issue
Block a user