summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/http.go9
-rw-r--r--src/http/posts.go57
-rw-r--r--src/http/tpl.go2
3 files changed, 35 insertions, 33 deletions
diff --git a/src/http/http.go b/src/http/http.go
index 6e57a77..4403a69 100644
--- a/src/http/http.go
+++ b/src/http/http.go
@@ -126,9 +126,10 @@ type api struct {
params Params
srv *http.Server
- redirectTpl *template.Template
- auther Auther
- urlBuilder render.URLBuilder
+ redirectTpl *template.Template
+ auther Auther
+ urlBuilder render.URLBuilder
+ postPreprocessFuncs postPreprocessFuncs
}
// New initializes and returns a new API instance, including setting up all
@@ -156,12 +157,12 @@ func New(params Params) (API, error) {
),
}
+ a.postPreprocessFuncs = newPostPreprocessFuncs(a.urlBuilder)
a.redirectTpl = mustParseTpl(template.New(""), "redirect.html")
a.srv = &http.Server{Handler: a.handler()}
go func() {
-
err := a.srv.Serve(l)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
ctx := mctx.WithAnnotator(context.Background(), &a.params)
diff --git a/src/http/posts.go b/src/http/posts.go
index 8488996..1bc65c8 100644
--- a/src/http/posts.go
+++ b/src/http/posts.go
@@ -5,10 +5,10 @@ import (
"context"
"errors"
"fmt"
+ "html/template"
"net/http"
"path/filepath"
"strings"
- txttpl "text/template"
"time"
"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
@@ -18,7 +18,18 @@ import (
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
)
-func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
+type postPreprocessFuncs struct {
+ urlBuilder render.URLBuilder
+ imageTpl *template.Template
+}
+
+func newPostPreprocessFuncs(urlBuilder render.URLBuilder) postPreprocessFuncs {
+ imageTpl := template.New("image.html")
+ imageTpl = template.Must(imageTpl.Parse(mustReadTplFile("image.html")))
+ return postPreprocessFuncs{urlBuilder, imageTpl}
+}
+
+func (f postPreprocessFuncs) Image(args ...string) (string, error) {
var (
id = args[0]
@@ -29,39 +40,29 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
descr = args[1]
}
- tpl := txttpl.New("image.html")
- tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html")))
-
- tplPayload := struct {
- RootURL render.URLBuilder
- ID string
- Descr string
- Resizable bool
- }{
- RootURL: render.NewURLBuilder(
- a.params.PublicURL,
- a.params.PublicURL, // httpURL
- a.params.GeminiPublicURL,
- ),
- ID: id,
- Descr: descr,
- Resizable: asset.IsImageResizable(id),
- }
+ var (
+ tplPayload = struct {
+ RootURL render.URLBuilder
+ ID string
+ Descr string
+ Resizable bool
+ }{
+ RootURL: f.urlBuilder,
+ ID: id,
+ Descr: descr,
+ Resizable: asset.IsImageResizable(id),
+ }
+ buf = new(bytes.Buffer)
+ )
- buf := new(bytes.Buffer)
- if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
+ err := f.imageTpl.ExecuteTemplate(buf, "image.html", tplPayload)
+ if err != nil {
return "", err
}
return buf.String(), nil
}
-func (a *api) postPreprocessFuncs() post.PreprocessFunctions {
- return post.PreprocessFunctions{
- Image: a.postPreprocessFuncImage,
- }
-}
-
func (a *api) getPostsHandler() http.Handler {
var (
tpl = a.mustParseBasedTpl("posts.html")
diff --git a/src/http/tpl.go b/src/http/tpl.go
index 47cea80..42341a2 100644
--- a/src/http/tpl.go
+++ b/src/http/tpl.go
@@ -66,7 +66,7 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData {
a.params.PostStore,
a.params.PostAssetStore,
a.params.PostDraftStore,
- a.postPreprocessFuncs(),
+ a.postPreprocessFuncs,
),
Payload: payload,
Title: "mediocregopher's lil web corner",