From 7eeb813f1960e0cf2d6501521e76753c4117d8bb Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Thu, 3 Jan 2019 16:48:47 +0530 Subject: [PATCH] Add embedding of static assets for standalone dist binary This is a big commit that involves drastic changes to how static assets (.sql and template files, the whole frontend bundle) are handled. listmonk distribution should be a self-contained single binary distribution, hence all static assets should be bundled. After evaluating several solutions, srtkkou/zgok seemed like the best bet but it lacked several fundamental features, namely the ability to fall back to the local filesystem in the absence of embedded assets (for instance, in the dev mode). Moreover, there was a lot of room for cleanup. After a PR went unanswered, github.com/knadh/stuffbin was created. Just like zgok, this enables arbitrary files and assets to be embedded into a compiled Go binary that can be read during runtime. These changes followed: - Compress and embed all static files into the binary during the build (Makefile) to make it standalone and distributable - Refactor static paths (/public/* for public facing assets, /frontend/* for the frontend app's assets) - Add 'logo_url' to config - Remove 'assets_path' from config - Tweak yarn build to not produce symbol maps and override the default /static (%PUBLIC_URL%) path to /frontend --- Makefile | 7 +- config.toml.sample | 15 +- {templates => email-templates}/base.html | 0 .../campaign-status.html | 0 {templates => email-templates}/default.tpl | 0 .../import-status.html | 0 frontend/my/package.json | 8 +- frontend/my/public/index.html | 4 +- handlers.go | 110 ++++++++--- install.go | 9 +- main.go | 172 +++++++----------- package.json | 5 - public.go | 39 ++-- public/static/style.css | 2 +- public/templates/error.html | 4 +- public/templates/hello.html | 3 - public/templates/hello2.html | 3 - public/templates/index.html | 22 ++- public/templates/unsubscribe.html | 4 +- sqlboiler.toml | 6 - 20 files changed, 228 insertions(+), 185 deletions(-) rename {templates => email-templates}/base.html (100%) rename {templates => email-templates}/campaign-status.html (100%) rename {templates => email-templates}/default.tpl (100%) rename {templates => email-templates}/import-status.html (100%) delete mode 100644 package.json delete mode 100644 public/templates/hello.html delete mode 100644 public/templates/hello2.html delete mode 100644 sqlboiler.toml diff --git a/Makefile b/Makefile index 86eacf4..4a94637 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ BIN := listmonk +STATIC := config.toml.sample schema.sql queries.sql public email-templates frontend/my/build:/frontend HASH := $(shell git rev-parse --short HEAD) COMMIT_DATE := $(shell git show -s --format=%ci ${HASH}) @@ -6,7 +7,11 @@ BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S') VERSION := ${HASH} (${COMMIT_DATE}) build: - go build -o ${BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" + go build -o ${BIN} -ldflags="-s -w -X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" + stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC} + +deps: + go get -u github.com/knadh/stuffbin/... test: go test diff --git a/config.toml.sample b/config.toml.sample index 337c6d5..12472f7 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -4,7 +4,17 @@ address = "0.0.0.0:9000" # Public root URL of the listmonk installation that'll be used # in the messages for linking to images, unsubscribe page etc. -root = "http://listmonk.mysite.com" +root = "https://listmonk.mysite.com" + +# (Optional) full URL to the static logo to be displayed on +# user facing view such as the unsubscription page. +# eg: https://mysite.com/images/logo.svg +logo_url = "" + +# (Optional) full URL to the static favicon to be displayed on +# user facing view such as the unsubscription page. +# eg: https://mysite.com/images/favicon.png +favicon_url = "" # The default 'from' e-mail for outgoing e-mail campaigns. from_email = "listmonk " @@ -22,9 +32,6 @@ upload_path = "uploads" # under this URI, for instance, list.yoursite.com/uploads. upload_uri = "/uploads" -# Directory where the app's static assets are stored (index.html, ./static etc.) -asset_path = "frontend/my/build" - # Maximum concurrent workers that will attempt to send messages # simultaneously. This should depend on the number of CPUs the # machine has and also the number of simultaenous e-mails the diff --git a/templates/base.html b/email-templates/base.html similarity index 100% rename from templates/base.html rename to email-templates/base.html diff --git a/templates/campaign-status.html b/email-templates/campaign-status.html similarity index 100% rename from templates/campaign-status.html rename to email-templates/campaign-status.html diff --git a/templates/default.tpl b/email-templates/default.tpl similarity index 100% rename from templates/default.tpl rename to email-templates/default.tpl diff --git a/templates/import-status.html b/email-templates/import-status.html similarity index 100% rename from templates/import-status.html rename to email-templates/import-status.html diff --git a/frontend/my/package.json b/frontend/my/package.json index 7c90fe1..d39b1b0 100644 --- a/frontend/my/package.json +++ b/frontend/my/package.json @@ -16,15 +16,9 @@ "react-router-dom": "^4.3.1", "react-scripts": "1.1.4" }, - "xxscripts": { - "start": "react-scripts start", - "build": "react-scripts build", - "test": "react-scripts test --env=jsdom", - "eject": "react-scripts eject" - }, "scripts": { "start": "react-app-rewired start", - "build": "react-app-rewired build", + "build": "GENERATE_SOURCEMAP=false PUBLIC_URL=/frontend/ react-app-rewired build", "test": "react-app-rewired test --env=jsdom", "eject": "react-scripts eject" }, diff --git a/frontend/my/public/index.html b/frontend/my/public/index.html index 2288373..c2f23fc 100644 --- a/frontend/my/public/index.html +++ b/frontend/my/public/index.html @@ -4,11 +4,11 @@ - + - %PUBLIC_URL% + listmonk