From 3e435fc0d032a6cac0bdd15cdb138905ecdb7267 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Wed, 19 Jul 2023 19:42:39 +0200 Subject: feat: Move error from `html/template` to qtpl --- pkg/ext/responses.go | 25 +++++++------------------ templates/error.html | 5 ----- templates/error.qtpl | 16 ++++++++++++++++ templates/media.html | 24 ------------------------ templates/media.qtpl | 1 - 5 files changed, 23 insertions(+), 48 deletions(-) delete mode 100644 templates/error.html create mode 100644 templates/error.qtpl delete mode 100644 templates/media.html diff --git a/pkg/ext/responses.go b/pkg/ext/responses.go index 7354395..dbad5b2 100644 --- a/pkg/ext/responses.go +++ b/pkg/ext/responses.go @@ -6,21 +6,16 @@ import ( "github.com/valyala/fasthttp" - "git.sr.ht/~gabrielgio/img" + "git.sr.ht/~gabrielgio/img/templates" ) var ( - ContentTypeJSON = []byte("application/json") - ContentTypeHTML = []byte("text/html") - ContentTypeMARKDOWN = []byte("text/markdown") - ContentTypeJPEG = []byte("image/jpeg") + ContentTypeHTML = []byte("text/html") ) func NotFoundHTML(ctx *fasthttp.RequestCtx) { - ctx.Response.Header.SetContentType("text/html") - //nolint:errcheck - img.Render(ctx, "error.html", &img.HTMLView[string]{ - Data: "NotFound", + templates.WritePageTemplate(ctx, &templates.ErrorPage{ + Err: "Not Found", }) } @@ -33,16 +28,10 @@ func NotFound(ctx *fasthttp.RequestCtx) { } 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, + ctx.Response.SetStatusCode(500) + templates.WritePageTemplate(ctx, &templates.ErrorPage{ + Err: fmt.Sprintf("Internal Server Error:\n%s", err.Error()), }) - - if respErr != nil { - fmt.Println(respErr.Error()) - } } func NoContent(ctx *fasthttp.RequestCtx) { diff --git a/templates/error.html b/templates/error.html deleted file mode 100644 index cbde400..0000000 --- a/templates/error.html +++ /dev/null @@ -1,5 +0,0 @@ -{{template "layout.html" .}} -{{define "title"}} Not Found {{end}} -{{define "content"}} -{{.}} -{{end}} diff --git a/templates/error.qtpl b/templates/error.qtpl new file mode 100644 index 0000000..b71adc8 --- /dev/null +++ b/templates/error.qtpl @@ -0,0 +1,16 @@ +{% code +type ErrorPage struct { + Err string +} +%} + +{% func (p *ErrorPage) Title() %} +Error +{% endfunc %} + +{% func (p *ErrorPage) Content() %} +{%s p.Err %} +{% endfunc %} + +{% func (p *ErrorPage) Script() %} +{% endfunc %} diff --git a/templates/media.html b/templates/media.html deleted file mode 100644 index 188d5b4..0000000 --- a/templates/media.html +++ /dev/null @@ -1,24 +0,0 @@ -{{template "layout.html" .}} -{{define "title"}} {{.Title}} {{end}} -{{define "content"}} -
-{{range .Data.Medias}} -
-
- {{ if .IsVideo }} - - {{ else }} -
- -
- {{ end }} -
-
-{{end}} -
-
- next -
-{{end}} diff --git a/templates/media.qtpl b/templates/media.qtpl index 88ce582..b9b3bf5 100644 --- a/templates/media.qtpl +++ b/templates/media.qtpl @@ -11,7 +11,6 @@ type MediaPage struct { Media {% endfunc %} - {% func (p *MediaPage) Content() %}
{% for _, media := range p.Medias %} -- cgit v1.2.3