From c6f77e08b8a26ec740f3121edbb7a9d06c04ce20 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 1 Nov 2025 18:34:04 +0100 Subject: feat: Add session variable This will allow the css to be refreshed once the server is restarted. This allows the admin to update the theme configuration and have it reflected in the browser. --- pkg/handler/router.go | 6 ++- templates/base.qtpl | 23 +++++++-- templates/base.qtpl.go | 126 +++++++++++++++++++++++++++++-------------------- 3 files changed, 99 insertions(+), 56 deletions(-) diff --git a/pkg/handler/router.go b/pkg/handler/router.go index cb5d6f5..1fbc4e3 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -55,7 +55,11 @@ func MountHandler( } mux.HandleFunc("/static/{file}", staticHandler) - mux.HandleFunc(fmt.Sprintf("/static/theme%s.css", templates.Slug), cssStaticHandler) // add slug so css file can be cached forever. + // add slug and session so css file can be cached forever. + // Slug follow commit id, which is update every new version + // Session is update every time server restarts, this allows the css to be + // cached forever but refresh if the admin updates the server configuration. + mux.HandleFunc(fmt.Sprintf("/static/theme.%s%s.css", templates.Session, templates.Slug), cssStaticHandler) mux.HandleFunc("/{name}/about/{$}", gitHandler.About) mux.HandleFunc("/{name}", gitHandler.Multiplex) mux.HandleFunc("/{name}/{rest...}", gitHandler.Multiplex) diff --git a/templates/base.qtpl b/templates/base.qtpl index 1dddb57..6ff3d53 100644 --- a/templates/base.qtpl +++ b/templates/base.qtpl @@ -1,11 +1,28 @@ This is a base page template. All the other template pages implement this interface. {% import "context" %} +{% import "crypto/rand" %} +{% import "encoding/hex" %} {% import "strconv" %} {% import "time" %} -{% code +{% code + var Slug = "" + var Session = "" + + func init() { + Session = hex.EncodeToString(generateSmallTimeID()) +} + +func generateSmallTimeID() []byte { + b := make([]byte, 4) + _, err := rand.Read(b) + if err != nil { + panic(err) + } + return b +} %} {% interface @@ -58,9 +75,9 @@ Page prints a page implementing Page interface. - {%= p.Title(ctx) %} + {%= p.Title(ctx) %} - + diff --git a/templates/base.qtpl.go b/templates/base.qtpl.go index 3c4d8a9..db8afec 100644 --- a/templates/base.qtpl.go +++ b/templates/base.qtpl.go @@ -11,57 +11,77 @@ package templates import "context" //line templates/base.qtpl:4 -import "strconv" +import "crypto/rand" //line templates/base.qtpl:5 -import "time" +import "encoding/hex" + +//line templates/base.qtpl:6 +import "strconv" //line templates/base.qtpl:7 +import "time" + +//line templates/base.qtpl:9 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line templates/base.qtpl:7 +//line templates/base.qtpl:9 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line templates/base.qtpl:8 +//line templates/base.qtpl:11 var Slug = "" +var Session = "" -//line templates/base.qtpl:12 +func init() { + Session = hex.EncodeToString(generateSmallTimeID()) +} + +func generateSmallTimeID() []byte { + b := make([]byte, 4) + _, err := rand.Read(b) + if err != nil { + panic(err) + } + return b +} + +//line templates/base.qtpl:29 type Page interface { -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 Title(ctx context.Context) string -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 StreamTitle(qw422016 *qt422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 WriteTitle(qq422016 qtio422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 Content(ctx context.Context) string -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 StreamContent(qw422016 *qt422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 WriteContent(qq422016 qtio422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 Script(ctx context.Context) string -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 StreamScript(qw422016 *qt422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 WriteScript(qq422016 qtio422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 Navbar(ctx context.Context) string -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 StreamNavbar(qw422016 *qt422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 WriteNavbar(qq422016 qtio422016.Writer, ctx context.Context) -//line templates/base.qtpl:12 +//line templates/base.qtpl:29 } -//line templates/base.qtpl:21 +//line templates/base.qtpl:38 func FromUInttoString(u *uint) string { if u != nil { return strconv.FormatUint(uint64(*u), 10) @@ -69,23 +89,23 @@ func FromUInttoString(u *uint) string { return "" } -//line templates/base.qtpl:31 +//line templates/base.qtpl:48 func TimeFormat(t time.Time) string { return t.Format("02.01.2006") } -//line templates/base.qtpl:36 +//line templates/base.qtpl:53 func Ignore[T any](v T, _ error) T { return v } -//line templates/base.qtpl:42 +//line templates/base.qtpl:59 func IsAuthenticationDisabled(ctx context.Context) bool { t, ok := ctx.Value("disableAuthentication").(bool) return ok && t } -//line templates/base.qtpl:48 +//line templates/base.qtpl:65 func IsLoggedIn(ctx context.Context) bool { t, ok := ctx.Value("logged").(bool) return ok && t @@ -93,9 +113,9 @@ func IsLoggedIn(ctx context.Context) bool { // Page prints a page implementing Page interface. -//line templates/base.qtpl:55 +//line templates/base.qtpl:72 func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) { -//line templates/base.qtpl:55 +//line templates/base.qtpl:72 qw422016.N().S(` @@ -103,19 +123,21 @@ func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) `) -//line templates/base.qtpl:61 +//line templates/base.qtpl:78 p.StreamTitle(qw422016, ctx) -//line templates/base.qtpl:61 - qw422016.N().S(` +//line templates/base.qtpl:78 + qw422016.N().S(` - @@ -123,53 +145,53 @@ func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) `) -//line templates/base.qtpl:69 +//line templates/base.qtpl:86 p.StreamNavbar(qw422016, ctx) -//line templates/base.qtpl:69 +//line templates/base.qtpl:86 qw422016.N().S(`
`) -//line templates/base.qtpl:71 +//line templates/base.qtpl:88 p.StreamContent(qw422016, ctx) -//line templates/base.qtpl:71 +//line templates/base.qtpl:88 qw422016.N().S(`
`) -//line templates/base.qtpl:74 +//line templates/base.qtpl:91 p.StreamScript(qw422016, ctx) -//line templates/base.qtpl:74 +//line templates/base.qtpl:91 qw422016.N().S(` `) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 } -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 func WritePageTemplate(qq422016 qtio422016.Writer, p Page, ctx context.Context) { -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 StreamPageTemplate(qw422016, p, ctx) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 qt422016.ReleaseWriter(qw422016) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 } -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 func PageTemplate(p Page, ctx context.Context) string { -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 qb422016 := qt422016.AcquireByteBuffer() -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 WritePageTemplate(qb422016, p, ctx) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 qs422016 := string(qb422016.B) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 qt422016.ReleaseByteBuffer(qb422016) -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 return qs422016 -//line templates/base.qtpl:79 +//line templates/base.qtpl:96 } -- cgit v1.2.3