mirror of
https://github.com/status-im/fathom.git
synced 2025-02-28 11:00:43 +00:00
implement notfound handler with simple 404 page, closes #19
This commit is contained in:
parent
6482378734
commit
73e011b6cc
18
assets/404.html
Normal file
18
assets/404.html
Normal file
@ -0,0 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
<html class="no-js" lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Not found - Fathom</title>
|
||||
<link href="/assets/css/styles.css" rel="stylesheet">
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicon.png">
|
||||
</head>
|
||||
<body class="404">
|
||||
<div class="login-page flex-rapper">
|
||||
<div>
|
||||
<h1>Page not found</h1>
|
||||
<p>Sorry, it seems that the requested page does not exist.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -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
|
||||
})
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user