From c207077877a7e55dc8c421f3425083025948cf89 Mon Sep 17 00:00:00 2001 From: Daniel Heath Date: Wed, 31 Jan 2018 14:15:16 +1100 Subject: [PATCH] Enable hot-template-reloading in development --- Makefile | 6 +++--- handlers.go | 16 ++++++++++++++-- main.go | 2 ++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 8161251..d6a0d77 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # make -j4 release VERSION=$(shell git describe) -LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" -a -installsuffix cgo +LDFLAGS=-ldflags "-s -w -X main.version=${VERSION} -X main.hotCodeReloading=false" -a -installsuffix cgo .PHONY: build build: @@ -11,8 +11,8 @@ build: .PHONY: quick quick: - go-bindata static/... templates/... - go build + go-bindata -debug static/... templates/... + go build -ldflags "-X main.hotCodeReloading=true" .PHONY: linuxarm linuxarm: diff --git a/handlers.go b/handlers.go index 12c3f74..c699b2d 100755 --- a/handlers.go +++ b/handlers.go @@ -37,16 +37,28 @@ func serve( secret string, secretCode string, allowInsecure bool, + hotTemplateReloading bool, ) { + if hotTemplateReloading { + gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) + } - gin.SetMode(gin.ReleaseMode) router := gin.Default() + + if hotTemplateReloading { + router.LoadHTMLGlob("templates/*.tmpl") + } else { + router.HTMLRender = loadTemplates("index.tmpl") + } + store := sessions.NewCookieStore([]byte(secret)) router.Use(sessions.Sessions("mysession", store)) if secretCode != "" { router.Use(secretRequired.RequiresSecretAccessCode(secretCode, "/login/")) } - router.HTMLRender = loadTemplates("index.tmpl") + // router.Use(static.Serve("/static/", static.LocalFile("./static", true))) router.GET("/", func(c *gin.Context) { if defaultPage != "" { diff --git a/main.go b/main.go index 6aaa78d..b3c3dbc 100755 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( ) var version string +var hotTemplateReloading bool var pathToData string func main() { @@ -53,6 +54,7 @@ func main() { c.GlobalString("cookie-secret"), c.GlobalString("access-code"), c.GlobalBool("allow-insecure-markup"), + hotTemplateReloading, ) return nil }