port most routes

This commit is contained in:
orangix
2026-01-19 18:57:06 +01:00
parent 04fbc7f5f4
commit cd4a36c9f7
17 changed files with 310 additions and 213 deletions

View File

@@ -1,25 +1,38 @@
package utils
import (
"io"
"net/http"
"strconv"
"strings"
"codeberg.org/rimgo/rimgo/render"
"codeberg.org/rimgo/rimgo/static"
"github.com/gofiber/fiber/v2"
)
func RenderError(c *fiber.Ctx, code int) error {
if !strings.Contains(c.Get("Accept"), "html") && c.Params("extension") != "" {
func RenderError(w http.ResponseWriter, r *http.Request, code int) error {
if !Accepts(r, "text/html") && r.PathValue("extension") != "" {
codeStr := "generic"
if code != 0 {
codeStr = strconv.Itoa(code)
}
img, _ := ReadFile("img/error-"+codeStr+".png", static.GetFiles())
c.Set("Content-Type", "image/png")
return c.Status(code).Send(img)
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(code)
file, _ := static.GetFiles().Open("img/error-" + codeStr + ".png")
defer file.Close()
_, err := io.Copy(w, file)
if err != nil {
// panic on error to avoid a loop
panic(err)
}
} else {
return c.Status(code).Render("errors/"+strconv.Itoa(code), fiber.Map{
"path": c.Path(),
w.WriteHeader(code)
err := render.Render(w, "errors/"+strconv.Itoa(code), map[string]any{
"path": r.URL.Path,
})
if err != nil {
// panic on error to avoid a loop
panic(err)
}
}
return nil
}