diff options
Diffstat (limited to 'pkg/ext/responses.go')
-rw-r--r-- | pkg/ext/responses.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/pkg/ext/responses.go b/pkg/ext/responses.go new file mode 100644 index 0000000..7354395 --- /dev/null +++ b/pkg/ext/responses.go @@ -0,0 +1,50 @@ +package ext + +import ( + "bytes" + "fmt" + + "github.com/valyala/fasthttp" + + "git.sr.ht/~gabrielgio/img" +) + +var ( + ContentTypeJSON = []byte("application/json") + ContentTypeHTML = []byte("text/html") + ContentTypeMARKDOWN = []byte("text/markdown") + ContentTypeJPEG = []byte("image/jpeg") +) + +func NotFoundHTML(ctx *fasthttp.RequestCtx) { + ctx.Response.Header.SetContentType("text/html") + //nolint:errcheck + img.Render(ctx, "error.html", &img.HTMLView[string]{ + Data: "NotFound", + }) +} + +func NotFound(ctx *fasthttp.RequestCtx) { + ctx.Response.SetStatusCode(404) + ct := ctx.Response.Header.ContentType() + if bytes.Equal(ct, ContentTypeHTML) { + NotFoundHTML(ctx) + } +} + +func InternalServerError(ctx *fasthttp.RequestCtx, err error) { + ctx.Response.Header.SetContentType("text/html") + message := fmt.Sprintf("Internal Server Error:\n%+v", err) + //nolint:errcheck + respErr := img.Render(ctx, "error.html", &img.HTMLView[string]{ + Data: message, + }) + + if respErr != nil { + fmt.Println(respErr.Error()) + } +} + +func NoContent(ctx *fasthttp.RequestCtx) { + ctx.Response.SetStatusCode(204) +} |