forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_found.go
93 lines (89 loc) · 1.63 KB
/
not_found.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package buffalo
import (
"encoding/json"
"html/template"
"net/http"
"github.com/pkg/errors"
)
func (a *App) notFound() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if a.Env == "development" {
err := func() error {
routes := a.Routes()
data := map[string]interface{}{
"routes": routes,
"method": req.Method,
"path": req.URL.String(),
}
switch req.Header.Get("Content-Type") {
case "application/json":
res.WriteHeader(404)
return json.NewEncoder(res).Encode(data)
default:
t, err := template.New("not-found").Parse(htmlNotFound)
if err != nil {
res.WriteHeader(500)
err = errors.WithStack(err)
res.Write([]byte(err.Error()))
return err
}
res.WriteHeader(404)
return t.Execute(res, data)
}
}()
if err != nil {
a.Logger.Error(err)
}
return
}
http.NotFound(res, req)
})
}
var htmlNotFound = `
<html>
<head>
<title>404 PAGE NOT FOUND</title>
<style>
body {
font-family: helvetica;
}
table {
width: 100%;
}
th {
text-align: left;
}
tr:nth-child(even) {
background-color: #dddddd;
}
td {
margin: 0px;
padding: 10px;
}
</style>
</head>
<body>
<h1>404 Page Not Found!</h1>
<h3>Could not find path <code>[{{.method}}] {{.path}}</code></h3>
<hr>
<table id="buffalo-routes-table">
<thead>
<tr>
<th>METHOD</th>
<th>PATH</th>
<th>HANDLER</th>
</tr>
</thead>
<tbody>
{{range .routes}}
<tr>
<td>{{.Method}}</td>
<td>{{.Path}}</td>
<td><code>{{.HandlerName}}</code></td>
</tr>
{{end}}
</tbody>
</table>
</body>
</html>
`