diff --git a/assets/404.html b/assets/404.html new file mode 100644 index 0000000..46320dc --- /dev/null +++ b/assets/404.html @@ -0,0 +1,18 @@ + + + + + + Not found - Fathom + + + + +
+
+

Page not found

+

Sorry, it seems that the requested page does not exist.

+
+
+ + diff --git a/pkg/api/http.go b/pkg/api/http.go index b3c46fa..6859900 100644 --- a/pkg/api/http.go +++ b/pkg/api/http.go @@ -2,13 +2,20 @@ package api import ( "encoding/json" - log "github.com/sirupsen/logrus" "net/http" + + "github.com/gobuffalo/packr" + log "github.com/sirupsen/logrus" ) // Handler is our custom HTTP handler with error returns type Handler func(w http.ResponseWriter, r *http.Request) error +type envelope struct { + Data interface{} `json:",omitempty"` + Error interface{} `json:",omitempty"` +} + func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := h(w, r); err != nil { HandleError(w, r, err) @@ -32,13 +39,38 @@ func HandleError(w http.ResponseWriter, r *http.Request, err error) { w.Write([]byte("false")) } -type envelope struct { - Data interface{} `json:",omitempty"` - Error interface{} `json:",omitempty"` -} - func respond(w http.ResponseWriter, d interface{}) error { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(d) return err } + +func serveFileHandler(box *packr.Box, filename string) http.Handler { + return HandlerFunc(serveFile(box, filename)) +} + +func serveFile(box *packr.Box, filename string) Handler { + return func(w http.ResponseWriter, r *http.Request) error { + f, err := box.Open(filename) + if err != nil { + return err + } + defer f.Close() + + d, err := f.Stat() + if err != nil { + return err + } + + http.ServeContent(w, r, filename, d.ModTime(), f) + return nil + } +} + +func NotFoundHandler(box *packr.Box) http.Handler { + return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { + w.WriteHeader(http.StatusNotFound) + w.Write(box.Bytes("404.html")) + return nil + }) +} diff --git a/pkg/api/routes.go b/pkg/api/routes.go index d03b551..1701e88 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -25,27 +25,12 @@ func Routes() *mux.Router { r.Handle("/api/stats/referrers", Authorize(GetReferrerStatsHandler)).Methods(http.MethodGet) r.Handle("/api/stats/referrers/pageviews", Authorize(GetReferrerStatsPageviewsHandler)).Methods(http.MethodGet) + // static assets & 404 handler box := packr.NewBox("./../../build") - r.Path("/tracker.js").Handler(serveFileFromBox(&box, "js/tracker.js")) - r.Path("/").Handler(serveFileFromBox(&box, "/index.html")) + r.Path("/tracker.js").Handler(serveFileHandler(&box, "js/tracker.js")) + r.Path("/").Handler(serveFileHandler(&box, "index.html")) r.PathPrefix("/assets").Handler(http.StripPrefix("/assets", http.FileServer(box))) + r.NotFoundHandler = NotFoundHandler(&box) + return r } - -func serveFileFromBox(box *packr.Box, filename string) http.Handler { - return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { - f, err := box.Open(filename) - if err != nil { - return err - } - defer f.Close() - - d, err := f.Stat() - if err != nil { - return err - } - - http.ServeContent(w, r, filename, d.ModTime(), f) - return nil - }) -}