1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Enable hot-template-reloading in development

This commit is contained in:
Daniel Heath 2018-01-31 14:15:16 +11:00
parent 5e4a317b10
commit c207077877
3 changed files with 19 additions and 5 deletions

View File

@ -2,7 +2,7 @@
# make -j4 release # make -j4 release
VERSION=$(shell git describe) 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 .PHONY: build
build: build:
@ -11,8 +11,8 @@ build:
.PHONY: quick .PHONY: quick
quick: quick:
go-bindata static/... templates/... go-bindata -debug static/... templates/...
go build go build -ldflags "-X main.hotCodeReloading=true"
.PHONY: linuxarm .PHONY: linuxarm
linuxarm: linuxarm:

View File

@ -37,16 +37,28 @@ func serve(
secret string, secret string,
secretCode string, secretCode string,
allowInsecure bool, allowInsecure bool,
hotTemplateReloading bool,
) { ) {
if hotTemplateReloading {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
gin.SetMode(gin.ReleaseMode)
router := gin.Default() router := gin.Default()
if hotTemplateReloading {
router.LoadHTMLGlob("templates/*.tmpl")
} else {
router.HTMLRender = loadTemplates("index.tmpl")
}
store := sessions.NewCookieStore([]byte(secret)) store := sessions.NewCookieStore([]byte(secret))
router.Use(sessions.Sessions("mysession", store)) router.Use(sessions.Sessions("mysession", store))
if secretCode != "" { if secretCode != "" {
router.Use(secretRequired.RequiresSecretAccessCode(secretCode, "/login/")) router.Use(secretRequired.RequiresSecretAccessCode(secretCode, "/login/"))
} }
router.HTMLRender = loadTemplates("index.tmpl")
// router.Use(static.Serve("/static/", static.LocalFile("./static", true))) // router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
router.GET("/", func(c *gin.Context) { router.GET("/", func(c *gin.Context) {
if defaultPage != "" { if defaultPage != "" {

View File

@ -9,6 +9,7 @@ import (
) )
var version string var version string
var hotTemplateReloading bool
var pathToData string var pathToData string
func main() { func main() {
@ -53,6 +54,7 @@ func main() {
c.GlobalString("cookie-secret"), c.GlobalString("cookie-secret"),
c.GlobalString("access-code"), c.GlobalString("access-code"),
c.GlobalBool("allow-insecure-markup"), c.GlobalBool("allow-insecure-markup"),
hotTemplateReloading,
) )
return nil return nil
} }