Serve error pages

This commit is contained in:
ekzyis 2023-09-09 22:52:50 +02:00
parent 06043c791e
commit 08d85ef477
3 changed files with 66 additions and 0 deletions

28
public/500.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>delphi.market</title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="manifest" href="/site.webmanifest" />
<link rel="stylesheet" href="/index.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div class="flex flex-column text-center justify-center h-100vh">
<code>
<strong>
<pre>
____ ___ ___
| ___| / _ \ / _ \
|___ \| | | | | | |
___) | |_| | |_| |
|____/ \___/ \___/ </pre>
</strong>
</code>
<div class="font-mono mb-1">Internal Server Error</div>
</div>
</body>
</html>

View File

@ -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
}
}

View File

@ -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)