From 8c00a7f69da274183d629439509203805a306a23 Mon Sep 17 00:00:00 2001 From: cuigh Date: Fri, 29 Sep 2017 20:18:29 +0800 Subject: [PATCH] Customize 403/404 error pages --- controller/home.go | 6 ++++++ main.go | 9 +++++++++ views/403.jet | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 views/403.jet diff --git a/controller/home.go b/controller/home.go index 5f612db..d0c6380 100644 --- a/controller/home.go +++ b/controller/home.go @@ -10,6 +10,7 @@ import ( type HomeController struct { Index web.HandlerFunc `path:"/" name:"index" authorize:"?" desc:"index page"` + Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"` Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"` Login web.HandlerFunc `path:"/login" name:"login" authorize:"*" desc:"sign in page"` InitGet web.HandlerFunc `path:"/init" name:"init" authorize:"*" desc:"initialize page"` @@ -95,8 +96,13 @@ func Home() (c *HomeController) { return ajaxResult(ctx, err) } + c.Error403 = func(ctx web.Context) error { + return ctx.Render("403", nil) + } + c.Error404 = func(ctx web.Context) error { return ctx.Render("404", nil) } + return } diff --git a/main.go b/main.go index e8edc0b..beabc48 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "path/filepath" "runtime" "time" @@ -18,6 +19,14 @@ import ( ) func main() { + // customize error handler + web.DefaultErrorHandler.OnCode(http.StatusNotFound, func(ctx web.Context, err error) { + ctx.Redirect("/404") + }) + web.DefaultErrorHandler.OnCode(http.StatusForbidden, func(ctx web.Context, err error) { + ctx.Redirect("/403") + }) + ws := web.Auto() // set render/validator.. diff --git a/views/403.jet b/views/403.jet new file mode 100644 index 0000000..f757693 --- /dev/null +++ b/views/403.jet @@ -0,0 +1,23 @@ +{{ extends "_layouts/empty" }} +{{ block body() }} +
+
+
+
+
+
+

403

+

Forbidden

+

+ You do not have permission to access this page. +

+ +
+
+
+
+
+
+{{ end }} \ No newline at end of file