delphi.market/router.go

63 lines
1.2 KiB
Go
Raw Normal View History

2023-09-09 20:52:50 +00:00
package main
import (
2023-09-09 20:52:50 +00:00
"fmt"
2023-09-09 20:52:50 +00:00
"html/template"
"io"
"net/http"
2023-09-09 20:52:50 +00:00
"os"
2023-09-09 20:52:50 +00:00
"github.com/labstack/echo/v4"
)
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func index(c echo.Context) error {
return c.Render(http.StatusOK, "index.html", map[string]string{"VERSION": VERSION, "COMMIT_LONG_SHA": COMMIT_LONG_SHA})
}
func login(c echo.Context) error {
return c.Render(http.StatusOK, "login.html", map[string]string{"user": ""})
}
2023-09-09 20:52:50 +00:00
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
}
}