summaryrefslogtreecommitdiff
path: root/api/template.go
diff options
context:
space:
mode:
authorsimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
committersimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
commit83cc6267fd5ce2f61200314424c5f400f65ff2ba (patch)
treeeafb35310236a15572cbb6e16ff8d6f181bfe240 /api/template.go
parent569d2788ebfb90774faf361f62bfe7968e091465 (diff)
parentcad8e2c4ed5e3bab61ff243f8677f8a46eaeafb0 (diff)
downloadhatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.tar.gz
hatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.zip
Merge pull request 'testing | dont be recursive for external domains | finalize oauth' (#5) from dont-be-authoritative into main
Reviewed-on: https://git.hatecomputers.club/hatecomputers/hatecomputers.club/pulls/5
Diffstat (limited to 'api/template.go')
-rw-r--r--api/template.go75
1 files changed, 0 insertions, 75 deletions
diff --git a/api/template.go b/api/template.go
deleted file mode 100644
index eeaeb51..0000000
--- a/api/template.go
+++ /dev/null
@@ -1,75 +0,0 @@
-package api
-
-import (
- "bytes"
- "errors"
- "html/template"
- "log"
- "net/http"
- "os"
-)
-
-func renderTemplate(context *RequestContext, templateName string, showBaseHtml bool) (bytes.Buffer, error) {
- templatePath := context.Args.TemplatePath
- basePath := templatePath + "/base_empty.html"
- if showBaseHtml {
- basePath = templatePath + "/base.html"
- }
-
- templateLocation := templatePath + "/" + templateName
- tmpl, err := template.New("").ParseFiles(templateLocation, basePath)
- if err != nil {
- return bytes.Buffer{}, err
- }
-
- dataPtr := context.TemplateData
- if dataPtr == nil {
- dataPtr = &map[string]interface{}{}
- }
-
- data := *dataPtr
- if data["User"] == nil {
- data["User"] = context.User
- }
-
- var buffer bytes.Buffer
- err = tmpl.ExecuteTemplate(&buffer, "base", data)
-
- if err != nil {
- return bytes.Buffer{}, err
- }
- return buffer, nil
-}
-
-func TemplateContinuation(path string, showBase bool) Continuation {
- return func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
- return func(success Continuation, failure Continuation) ContinuationChain {
- html, err := renderTemplate(context, path, true)
- if errors.Is(err, os.ErrNotExist) {
- resp.WriteHeader(404)
- html, err = renderTemplate(context, "404.html", true)
- if err != nil {
- log.Println("error rendering 404 template", err)
- resp.WriteHeader(500)
- return failure(context, req, resp)
- }
-
- resp.Header().Set("Content-Type", "text/html")
- resp.Write(html.Bytes())
- return failure(context, req, resp)
- }
-
- if err != nil {
- log.Println("error rendering template", err)
- resp.WriteHeader(500)
- resp.Write([]byte("error rendering template"))
- return failure(context, req, resp)
- }
-
- resp.WriteHeader(200)
- resp.Header().Set("Content-Type", "text/html")
- resp.Write(html.Bytes())
- return success(context, req, resp)
- }
- }
-}