diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000..1fe5b76 --- /dev/null +++ b/public/500.html @@ -0,0 +1,28 @@ + + + + delphi.market + + + + + + + + + +
+ + +
+____   ___   ___   
+| ___| / _ \ / _ \ 
+|___ \| | | | | | |
+ ___) | |_| | |_| |
+|____/ \___/ \___/ 
+
+
+
Internal Server Error
+
+ + diff --git a/router.go b/router.go index eab2dbc..d0d1518 100644 --- a/router.go +++ b/router.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "html/template" "io" "net/http" + "os" "github.com/labstack/echo/v4" ) @@ -23,3 +25,38 @@ func index(c echo.Context) error { func login(c echo.Context) error { return c.Render(http.StatusOK, "login.html", map[string]string{"user": ""}) } + +func serve500(c echo.Context) { + f, err := os.Open("public/500.html") + if err != nil { + c.Logger().Error(err) + return + } + err = c.Stream(500, "text/html", f) + if err != nil { + c.Logger().Error(err) + return + } +} + +func httpErrorHandler(err error, c echo.Context) { + c.Logger().Error(err) + code := http.StatusInternalServerError + httpError, ok := err.(*echo.HTTPError) + if ok { + code = httpError.Code + } + filePath := fmt.Sprintf("public/%d.html", code) + f, err := os.Open(filePath) + if err != nil { + c.Logger().Error(err) + serve500(c) + return + } + err = c.Stream(code, "text/html", f) + if err != nil { + c.Logger().Error(err) + serve500(c) + return + } +} diff --git a/server.go b/server.go index 1e1634f..3c2126b 100644 --- a/server.go +++ b/server.go @@ -49,6 +49,7 @@ func main() { Format: "${time_custom} ${method} ${uri} ${status}\n", CustomTimeFormat: "2006-01-02 15:04:05.00000-0700", })) + e.HTTPErrorHandler = httpErrorHandler err := e.Start(":8080") if err != http.ErrServerClosed { log.Fatal(err)