1package ext
2
3import (
4 "bytes"
5 "fmt"
6
7 "github.com/valyala/fasthttp"
8
9 "git.sr.ht/~gabrielgio/img"
10)
11
12var (
13 ContentTypeJSON = []byte("application/json")
14 ContentTypeHTML = []byte("text/html")
15 ContentTypeMARKDOWN = []byte("text/markdown")
16 ContentTypeJPEG = []byte("image/jpeg")
17)
18
19func NotFoundHTML(ctx *fasthttp.RequestCtx) {
20 ctx.Response.Header.SetContentType("text/html")
21 //nolint:errcheck
22 img.Render(ctx, "error.html", &img.HTMLView[string]{
23 Data: "NotFound",
24 })
25}
26
27func NotFound(ctx *fasthttp.RequestCtx) {
28 ctx.Response.SetStatusCode(404)
29 ct := ctx.Response.Header.ContentType()
30 if bytes.Equal(ct, ContentTypeHTML) {
31 NotFoundHTML(ctx)
32 }
33}
34
35func InternalServerError(ctx *fasthttp.RequestCtx, err error) {
36 ctx.Response.Header.SetContentType("text/html")
37 message := fmt.Sprintf("Internal Server Error:\n%+v", err)
38 //nolint:errcheck
39 respErr := img.Render(ctx, "error.html", &img.HTMLView[string]{
40 Data: message,
41 })
42
43 if respErr != nil {
44 fmt.Println(respErr.Error())
45 }
46}
47
48func NoContent(ctx *fasthttp.RequestCtx) {
49 ctx.Response.SetStatusCode(204)
50}