From 6a7317bca186ce76abc6bced689d065fc0b3e94c Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Sat, 12 Mar 2016 00:43:19 +0900 Subject: [PATCH] template engine and static engine applied with debug and release mode --- Godeps/Godeps.json | 2 +- main.go | 12 +++++++++--- util/renderer.go | 2 +- {route => util}/static.go | 15 +++++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) rename {route => util}/static.go (75%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 36b1115..aa23a66 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "amuz.es/go/mnemonics", - "GoVersion": "go1.5", + "GoVersion": "go1.6", "Deps": [ { "ImportPath": "github.com/elazarl/go-bindata-assetfs", diff --git a/main.go b/main.go index a28f351..8bacb64 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func main() { setMaxProcs() util.InitLogger() - gin.SetMode(gin.ReleaseMode) + // gin.SetMode(gin.ReleaseMode) r := gin.New() // middleware settings r.Use(util.Ginrus(time.RFC3339, true)) @@ -45,8 +45,7 @@ func main() { c.HTML(http.StatusOK, "page_404.html", util.Context{}) }) - r.StaticFS("/static", route.Static("")) - + // handlers r.GET("/api", route.Api) r.GET("/self", route.Self) r.GET("/metric", route.Metric) @@ -54,6 +53,13 @@ func main() { c.String(200, "OK! - mnemonics services are fully operational!") }) + // static route + if gin.IsDebugging() { + r.StaticFS("/static", util.StaticDebug("asset/static")) + } else { + r.StaticFS("/static", util.StaticRelease("")) + } + // template engine setting if gin.IsDebugging() { r.HTMLRender = util.TemplateSourceDebug("asset/template") } else { diff --git a/util/renderer.go b/util/renderer.go index 9b9ac7a..536f2b4 100644 --- a/util/renderer.go +++ b/util/renderer.go @@ -87,7 +87,7 @@ func (m memoryTemplateLoader) Get(path string) (io.Reader, error) { Log().Errorf("hello template %s", filepath.Clean(path)) // t := pongo2.Must(p.templateSet.FromFile(path.Join(p.Path, name))) - data, err := m.loaderFunc(path) + data, err := m.loaderFunc(filepath.Clean(path)) if err != nil { if m.fallbackLoader != nil { return (*m.fallbackLoader).Get(path) diff --git a/route/static.go b/util/static.go similarity index 75% rename from route/static.go rename to util/static.go index 2d0303c..b77e1d1 100644 --- a/route/static.go +++ b/util/static.go @@ -1,10 +1,11 @@ -package route +package util import ( - assetfs "github.com/elazarl/go-bindata-assetfs" - "amuz.es/go/mnemonics/bind/static" "net/http" "strings" + + "amuz.es/go/mnemonics/bind/static" + assetfs "github.com/elazarl/go-bindata-assetfs" ) type binaryFileSystem struct { @@ -16,7 +17,6 @@ func (b *binaryFileSystem) Open(name string) (http.File, error) { } func (b *binaryFileSystem) Exists(prefix string, filepath string) bool { - if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { if _, err := b.fs.Open(p); err != nil { return false @@ -26,10 +26,13 @@ func (b *binaryFileSystem) Exists(prefix string, filepath string) bool { return false } -func Static(root string) *binaryFileSystem { - fs := &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo, Prefix:root} +func StaticRelease(root string) http.FileSystem { + fs := &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo, Prefix: root} return &binaryFileSystem{ fs, } } +func StaticDebug(root string) http.FileSystem { + return http.Dir("asset/static") +}