1
0
Fork 0

template engine and static engine applied with debug and release mode

This commit is contained in:
Sangbum Kim 2016-03-12 00:43:19 +09:00
parent 75234ebd31
commit 6a7317bca1
4 changed files with 20 additions and 11 deletions

2
Godeps/Godeps.json generated
View File

@ -1,6 +1,6 @@
{
"ImportPath": "amuz.es/go/mnemonics",
"GoVersion": "go1.5",
"GoVersion": "go1.6",
"Deps": [
{
"ImportPath": "github.com/elazarl/go-bindata-assetfs",

12
main.go
View File

@ -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 {

View File

@ -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)

View File

@ -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 {
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")
}