High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Refactor static file serving and bundling for better caching and performance.
Kailash Nadhc94a061a3582
diff --git a/cmd/init.go b/cmd/init.goindex 747d055d..58842d17 100644--- a/cmd/init.go+++ b/cmd/init.go@@ -2,6 +2,7 @@ package mainimport ("bytes"+ "compress/gzip""crypto/md5""database/sql""encoding/json"@@ -12,6 +13,7 @@ import ("log""log/slog""maps"+ "mime""net/http""net/url""os"@@ -141,7 +143,9 @@ type Config struct {}HasLegacyUser bool- AssetVersion string++ AssetVersion string+ CustomAssetVersion stringMediaUpload struct {Provider string@@ -535,8 +539,13 @@ func initConstConfig(ko *koanf.Koanf) *Config {c.BounceLettermintEnabled = ko.Bool("bounce.lettermint.enabled")c.HasLegacyUser = ko.Exists("app.admin_username") || ko.Exists("app.admin_password")- b := md5.Sum([]byte(time.Now().String()))- c.AssetVersion = fmt.Sprintf("%x", b)[0:10]+ // Fixed during build time so that it doesn't change across restarts.+ av := md5.Sum([]byte(buildString))+ c.AssetVersion = fmt.Sprintf("%x", av)[0:10]++ // Changes on every restart (specifically for admin-controlled custom.js|css endpoints).+ cv := md5.Sum([]byte(time.Now().String()))+ c.CustomAssetVersion = fmt.Sprintf("%x", cv)[0:10]pm, err := fs.Read("/permissions.json")if err != nil {@@ -998,23 +1007,25 @@ func initHTTPServer(cfg *Config, urlCfg *UrlConfig, i *i18n.I18n, fs stuffbin.FiLogoURL: urlCfg.LogoURL,FaviconURL: urlCfg.FaviconURL,AssetVersion: cfg.AssetVersion,+ CustomAssetVersion: cfg.CustomAssetVersion,EnablePublicSubPage: cfg.EnablePublicSubPage,EnablePublicArchive: cfg.EnablePublicArchive,IndividualTracking: cfg.Privacy.IndividualTracking,},AdminRenderer: &adminTplRenderer{- templates: adminTpl,- SiteName: cfg.SiteName,- RootURL: urlCfg.RootURL,- LogoURL: urlCfg.LogoURL,- FaviconURL: urlCfg.FaviconURL,- AssetVersion: cfg.AssetVersion,- Lang: cfg.Lang,+ templates: adminTpl,+ SiteName: cfg.SiteName,+ RootURL: urlCfg.RootURL,+ LogoURL: urlCfg.LogoURL,+ FaviconURL: urlCfg.FaviconURL,+ AssetVersion: cfg.AssetVersion,+ CustomAssetVersion: cfg.CustomAssetVersion,+ Lang: cfg.Lang,},}// Initialize the static file server.- fSrv := fs.FileServer()+ fSrv := staticServer(fs)// Public (subscriber) facing static files.srv.GET("/public/static/*", echo.WrapHandler(fSrv))@@ -1052,6 +1063,67 @@ func initHTTPServer(cfg *Config, urlCfg *UrlConfig, i *i18n.I18n, fs stuffbin.Fireturn srv}+// staticServer returns an http.Handler that serves static files from the stuffbin+// FS, preferring precompressed .gz variants.+func staticServer(fs stuffbin.FileSystem) http.Handler {+ fSrv := fs.FileServer()++ // Set long cache headers. New dists have `?v=.AssetVersion` param that changes,+ // so that'll bust cache.+ const cacheControl = "public, max-age=31536000, immutable"++ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Cache-Control", cacheControl)++ // Is there a .gz version?+ f, err := fs.Get(r.URL.Path + ".gz")+ if err != nil {+ // No, server the file as-is.+ fSrv.ServeHTTP(w, r)+ return+ }++ // Yep, there is.+ info, err := f.Stat()+ if err != nil {+ fSrv.ServeHTTP(w, r)+ return+ }++ // Strip .gz and get the content type.+ hdr := w.Header()+ if ctype := mime.TypeByExtension(path.Ext(r.URL.Path)); ctype != "" {+ hdr.Set("Content-Type", ctype)+ }+ hdr.Set("Vary", "Accept-Encoding")++ // Client accepts gzip. Serve bytes as is.+ if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {+ hdr.Set("Content-Encoding", "gzip")+ http.ServeContent(w, r, r.URL.Path, info.ModTime(), f)+ return+ }++ // Client doesn't accept gzip, decompress on the fly and serve the result.+ // This isn't ideal for performance, but rarely will a modern client not+ // support gzip.+ gz, err := gzip.NewReader(f)+ if err != nil {+ http.Error(w, "error reading static asset", http.StatusInternalServerError)+ return+ }+ defer gz.Close()++ b, err := io.ReadAll(gz)+ if err != nil {+ http.Error(w, "error decompressing static asset", http.StatusInternalServerError)+ return+ }++ http.ServeContent(w, r, r.URL.Path, info.ModTime(), bytes.NewReader(b))+ })+}+// initCaptcha initializes the captcha service.func initCaptcha() *captcha.Captcha {var opt captcha.Optdiff --git a/cmd/public.go b/cmd/public.goindex 9ca25da3..981d4716 100644--- a/cmd/public.go+++ b/cmd/public.go@@ -35,6 +35,7 @@ type pubTplRenderer struct {LogoURL stringFaviconURL stringAssetVersion string+ CustomAssetVersion stringEnablePublicSubPage boolEnablePublicArchive boolIndividualTracking bool@@ -42,13 +43,14 @@ type pubTplRenderer struct {// adminTplRenderer wraps admin templates for echo.type adminTplRenderer struct {- templates *template.Template- SiteName string- RootURL string- LogoURL string- FaviconURL string- AssetVersion string- Lang string+ templates *template.Template+ SiteName string+ RootURL string+ LogoURL string+ FaviconURL string+ AssetVersion string+ CustomAssetVersion string+ Lang string}// appTplRenderer dispatches c.Render() calls to the appropriate template set.@@ -65,6 +67,7 @@ type tplData struct {LogoURL stringFaviconURL stringAssetVersion string+ CustomAssetVersion stringEnablePublicSubPage boolEnablePublicArchive boolIndividualTracking bool@@ -74,17 +77,18 @@ type tplData struct {// adminTplData is the data container injected into admin templates.type adminTplData struct {- SiteName string- RootURL string- LogoURL string- FaviconURL string- AssetVersion string- Lang string- ConfigJS serverConfig- I18nJS json.RawMessage- ReqDuration string- Data any- L *i18n.I18n+ SiteName string+ RootURL string+ LogoURL string+ FaviconURL string+ AssetVersion string+ CustomAssetVersion string+ Lang string+ ConfigJS serverConfig+ I18nJS json.RawMessage+ ReqDuration string+ Data any+ L *i18n.I18n}type publicTpl struct {@@ -146,6 +150,7 @@ func (t *pubTplRenderer) Render(w io.Writer, name string, data any, c echo.ConteLogoURL: t.LogoURL,FaviconURL: t.FaviconURL,AssetVersion: t.AssetVersion,+ CustomAssetVersion: t.CustomAssetVersion,EnablePublicSubPage: t.EnablePublicSubPage,EnablePublicArchive: t.EnablePublicArchive,IndividualTracking: t.IndividualTracking,@@ -169,17 +174,18 @@ func (t *adminTplRenderer) Render(w io.Writer, name string, data any, c echo.Con}return t.templates.ExecuteTemplate(w, name, adminTplData{- SiteName: t.SiteName,- RootURL: t.RootURL,- LogoURL: t.LogoURL,- FaviconURL: t.FaviconURL,- AssetVersion: t.AssetVersion,- Lang: t.Lang,- ConfigJS: configJS,- I18nJS: app.adminI18nJS,- ReqDuration: reqDuration,- Data: data,- L: app.i18n,+ SiteName: t.SiteName,+ RootURL: t.RootURL,+ LogoURL: t.LogoURL,+ FaviconURL: t.FaviconURL,+ AssetVersion: t.AssetVersion,+ CustomAssetVersion: t.CustomAssetVersion,+ Lang: t.Lang,+ ConfigJS: configJS,+ I18nJS: app.adminI18nJS,+ ReqDuration: reqDuration,+ Data: data,+ L: app.i18n,})}diff --git a/static/admin/build.mjs b/static/admin/build.mjsindex df9cdc72..eaa15c9c 100644--- a/static/admin/build.mjs+++ b/static/admin/build.mjs@@ -2,7 +2,7 @@// Output (dist/) is stuffed into the Go binary and served at /admin/static/*.// Run: `bun run build` (one-shot) or `bun run watch` (rebuild on change).-import { readdir, rm, mkdir, cp } from 'node:fs/promises';+import { readdir, rm, mkdir, cp, readFile, writeFile, unlink } from 'node:fs/promises';import { watch as fsWatch } from 'node:fs';import path from 'node:path';import { fileURLToPath } from 'node:url';@@ -29,6 +29,9 @@ const TINY_PLUGINS = [];const TINY_LANGS = ['cs', 'de', 'es_MX', 'fr_FR', 'it_IT', 'pl', 'pt_PT', 'pt_BR', 'ro', 'tr'];+// Types to always compress and bundle as .gz.+const COMPRESS_EXT = ['.js', '.css', '.svg'];+async function copyTinyMCE() {const from = (p) => path.join(root, 'node_modules/tinymce', p);const copy = async (src, destRel) => {@@ -99,6 +102,24 @@ async function build() {throw new AggregateError(result.logs, 'admin build failed');}console.log(`built ${result.outputs.length} files -> dist/js`);++ await compressAssets();+}++async function compressAssets() {+ const entries = await readdir(dist, { recursive: true, withFileTypes: true });+ let n = 0;++ for (const e of entries) {+ if (!e.isFile() || !COMPRESS_EXT.includes(path.extname(e.name))) continue;++ const abs = path.join(e.parentPath ?? e.path, e.name);+ await writeFile(`${abs}.gz`, Bun.gzipSync(await readFile(abs), { level: 9 }));+ await unlink(abs);+ n++;+ }++ console.log(`compressed ${n} assets -> .gz`);}await build();diff --git a/static/admin/views/base.html b/static/admin/views/base.htmlindex 360e0304..7ad68f92 100644--- a/static/admin/views/base.html+++ b/static/admin/views/base.html@@ -1,13 +1,13 @@{{ define "admin-head-assets" }}<link href="{{ URI "/admin/static/vendor/oat.min.css" }}?v={{ .AssetVersion }}" rel="stylesheet" type="text/css" /><link href="{{ URI "/admin/static/style.css" }}?v={{ .AssetVersion }}" rel="stylesheet" type="text/css" />- <link href="{{ URI "/admin/custom.css" }}?v={{ .AssetVersion }}" rel="stylesheet" type="text/css" />+ <link href="{{ URI "/admin/custom.css" }}?v={{ .CustomAssetVersion }}" rel="stylesheet" type="text/css" /><script>window._LM_I18N = {{ .I18nJS }};window._LM_CONFIG = {{ ToJSON .ConfigJS }};</script><script src="{{ URI "/admin/static/vendor/oat.min.js" }}?v={{ .AssetVersion }}" defer></script>- <script src="{{ URI "/admin/custom.js" }}?v={{ .AssetVersion }}" defer></script>+ <script src="{{ URI "/admin/custom.js" }}?v={{ .CustomAssetVersion }}" defer></script>{{ if ne .FaviconURL "" }}<link rel="icon" href="{{ .FaviconURL }}" type="image/x-icon" />diff --git a/static/public/templates/index.html b/static/public/templates/index.htmlindex d3966476..0130eb46 100644--- a/static/public/templates/index.html+++ b/static/public/templates/index.html@@ -13,8 +13,8 @@{{ end }}<link href="/public/static/style.css?v={{ .AssetVersion }}" rel="stylesheet" type="text/css" />- <link href="/public/custom.css?v={{ .AssetVersion }}" rel="stylesheet" type="text/css">- <script src="/public/custom.js?v={{ .AssetVersion }}" async defer></script>+ <link href="/public/custom.css?v={{ .CustomAssetVersion }}" rel="stylesheet" type="text/css">+ <script src="/public/custom.js?v={{ .CustomAssetVersion }}" async defer></script>{{ if ne .FaviconURL "" }}<link rel="icon" href="{{ .FaviconURL }}" type="image/x-icon" />