From ec5616b2c3624558a409b6e4e4002e7bea05d153 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 25 Dec 2018 10:30:22 -0500 Subject: [PATCH] Embed static assets in binary This embeds assets from the static/ dir in the binary and serves them. Ref T536 --- .gitignore | 2 ++ Makefile | 16 +++++++++++++--- app.go | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index f1e3331..95ad31b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ build config.ini *.db + +bindata.go diff --git a/Makefile b/Makefile index 3e0e4c6..8a49295 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ IMAGE_NAME=writeas/writefreely all : build -build: deps +build: assets deps cd cmd/writefreely; $(GOBUILD) -v build-linux: deps @@ -50,11 +50,10 @@ install : build cmd/writefreely/$(BINARY_NAME) --gen-keys cd less/; $(MAKE) install $(MFLAGS) -release : clean ui +release : clean ui assets mkdir build cp -r templates build cp -r pages build - cp -r static build mkdir build/keys cp schema.sql build cp sqlite.sql build @@ -78,6 +77,17 @@ release-docker : ui : force_look cd less/; $(MAKE) $(MFLAGS) +assets : generate + go-bindata -pkg writefreely -ignore=\\.gitignore static/... + +dev-assets : generate + go-bindata -pkg writefreely -ignore=\\.gitignore -debug static/... + +generate : + @hash go-bindata > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GOGET) -u github.com/jteeuwen/go-bindata/...; \ + fi + clean : -rm -rf build cd less/; $(MAKE) clean $(MFLAGS) diff --git a/app.go b/app.go index c435a9a..f6c6eb8 100644 --- a/app.go +++ b/app.go @@ -27,6 +27,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" + "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/mux" "github.com/gorilla/schema" "github.com/gorilla/sessions" @@ -41,7 +42,7 @@ import ( ) const ( - staticDir = "static/" + staticDir = "static" assumedTitleLen = 80 postsPerPage = 10 @@ -411,7 +412,7 @@ func Serve() { } // Handle static files - fs := http.FileServer(http.Dir(staticDir)) + fs := http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: staticDir}) shttp.Handle("/", fs) r.PathPrefix("/").Handler(fs)