feat: add 404 page
This commit is contained in:
parent
7057e232e6
commit
7b62a82b42
2
ctx.go
2
ctx.go
@ -207,7 +207,7 @@ func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) JSON(data interface{}) error {
|
func (c *Ctx) JSON(data interface{}) error {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
||||||
|
|
||||||
encoder := json.NewEncoder(c.writer)
|
encoder := json.NewEncoder(c.writer)
|
||||||
|
|
||||||
|
13
nf.go
13
nf.go
@ -2,6 +2,7 @@ package nf
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
banner = " _ _ _ ___ _ \n | \\| |___| |_ | __|__ _ _ _ _ __| |\n | .` / _ \\ _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
|
banner = " _ _ _ ___ _ \n | \\| |___| |_ | __|__ _ _ _ _ __| |\n | .` / _ \\ _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
|
||||||
|
_404 = "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Not Found</title><style>body{background:#333;margin:0;color:#ccc;display:flex;align-items:center;max-height:100vh;height:100vh;justify-content:center}textarea{min-height:5rem;min-width:20rem;text-align:center;border:none;background:0 0;color:#ccc;resize:none;user-input:none;user-select:none;cursor:default;-webkit-user-select:none;-webkit-touch-callout:none;-moz-user-select:none;-ms-user-select:none;outline:0}</style></head><body><textarea id=\"banner\" readonly=\"readonly\"></textarea><script type=\"text/javascript\">let htmlCodes = [\n ' _ _ _ ___ _ ',\n '| \\\\| |___| |_ | __|__ _ _ _ _ __| |',\n '| .` / _ \\\\ _| | _/ _ \\\\ || | \\' \\\\/ _` |',\n '|_|\\\\_\\\\___/\\\\__| |_|\\\\___/\\\\_,_|_||_\\\\__,_|'\n].join('\\n');\ndocument.querySelector('#banner').value = htmlCodes</script></body></html>"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Map map[string]interface{}
|
type Map map[string]interface{}
|
||||||
@ -17,11 +18,18 @@ type Config struct {
|
|||||||
DisableLogger bool `json:"-"`
|
DisableLogger bool `json:"-"`
|
||||||
DisableRecover bool `json:"-"`
|
DisableRecover bool `json:"-"`
|
||||||
DisableHttpErrorLog bool `json:"-"`
|
DisableHttpErrorLog bool `json:"-"`
|
||||||
|
|
||||||
|
NotFoundHandler HandlerFunc `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultConfig = &Config{
|
defaultConfig = &Config{
|
||||||
BodyLimit: 4 * 1024 * 1024,
|
BodyLimit: 4 * 1024 * 1024,
|
||||||
|
NotFoundHandler: func(c *Ctx) error {
|
||||||
|
c.Set("Content-Type", MIMETextHTML)
|
||||||
|
_, err := c.Status(404).Write([]byte(_404))
|
||||||
|
return err
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,6 +43,11 @@ func New(config ...Config) *App {
|
|||||||
if app.config.BodyLimit == 0 {
|
if app.config.BodyLimit == 0 {
|
||||||
app.config.BodyLimit = defaultConfig.BodyLimit
|
app.config.BodyLimit = defaultConfig.BodyLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if app.config.NotFoundHandler == nil {
|
||||||
|
app.config.NotFoundHandler = defaultConfig.NotFoundHandler
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
app.config = defaultConfig
|
app.config = defaultConfig
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,7 @@ func (r *router) handle(c *Ctx) error {
|
|||||||
key := c.Method + "-" + node.pattern
|
key := c.Method + "-" + node.pattern
|
||||||
c.handlers = append(c.handlers, r.handlers[key]...)
|
c.handlers = append(c.handlers, r.handlers[key]...)
|
||||||
} else {
|
} else {
|
||||||
_, err := c.Writef("404 NOT FOUND: %s\n", c.path)
|
return c.app.config.NotFoundHandler(c)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next()
|
return c.Next()
|
||||||
|
8
util.go
8
util.go
@ -13,11 +13,9 @@ const (
|
|||||||
MIMETextJavaScript = "text/javascript"
|
MIMETextJavaScript = "text/javascript"
|
||||||
MIMEApplicationXML = "application/xml"
|
MIMEApplicationXML = "application/xml"
|
||||||
MIMEApplicationJSON = "application/json"
|
MIMEApplicationJSON = "application/json"
|
||||||
// Deprecated: use MIMETextJavaScript instead
|
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
||||||
MIMEApplicationJavaScript = "application/javascript"
|
MIMEOctetStream = "application/octet-stream"
|
||||||
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
MIMEMultipartForm = "multipart/form-data"
|
||||||
MIMEOctetStream = "application/octet-stream"
|
|
||||||
MIMEMultipartForm = "multipart/form-data"
|
|
||||||
|
|
||||||
MIMETextXMLCharsetUTF8 = "text/xml; charset=utf-8"
|
MIMETextXMLCharsetUTF8 = "text/xml; charset=utf-8"
|
||||||
MIMETextHTMLCharsetUTF8 = "text/html; charset=utf-8"
|
MIMETextHTMLCharsetUTF8 = "text/html; charset=utf-8"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user