template engine and static engine applied with debug and release mode
This commit is contained in:
parent
75234ebd31
commit
6a7317bca1
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"ImportPath": "amuz.es/go/mnemonics",
|
"ImportPath": "amuz.es/go/mnemonics",
|
||||||
"GoVersion": "go1.5",
|
"GoVersion": "go1.6",
|
||||||
"Deps": [
|
"Deps": [
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/elazarl/go-bindata-assetfs",
|
"ImportPath": "github.com/elazarl/go-bindata-assetfs",
|
||||||
|
|
12
main.go
12
main.go
|
@ -34,7 +34,7 @@ func main() {
|
||||||
setMaxProcs()
|
setMaxProcs()
|
||||||
util.InitLogger()
|
util.InitLogger()
|
||||||
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
// gin.SetMode(gin.ReleaseMode)
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
// middleware settings
|
// middleware settings
|
||||||
r.Use(util.Ginrus(time.RFC3339, true))
|
r.Use(util.Ginrus(time.RFC3339, true))
|
||||||
|
@ -45,8 +45,7 @@ func main() {
|
||||||
c.HTML(http.StatusOK, "page_404.html", util.Context{})
|
c.HTML(http.StatusOK, "page_404.html", util.Context{})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.StaticFS("/static", route.Static(""))
|
// handlers
|
||||||
|
|
||||||
r.GET("/api", route.Api)
|
r.GET("/api", route.Api)
|
||||||
r.GET("/self", route.Self)
|
r.GET("/self", route.Self)
|
||||||
r.GET("/metric", route.Metric)
|
r.GET("/metric", route.Metric)
|
||||||
|
@ -54,6 +53,13 @@ func main() {
|
||||||
c.String(200, "OK! - mnemonics services are fully operational!")
|
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() {
|
if gin.IsDebugging() {
|
||||||
r.HTMLRender = util.TemplateSourceDebug("asset/template")
|
r.HTMLRender = util.TemplateSourceDebug("asset/template")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -87,7 +87,7 @@ func (m memoryTemplateLoader) Get(path string) (io.Reader, error) {
|
||||||
|
|
||||||
Log().Errorf("hello template %s", filepath.Clean(path))
|
Log().Errorf("hello template %s", filepath.Clean(path))
|
||||||
// t := pongo2.Must(p.templateSet.FromFile(path.Join(p.Path, name)))
|
// 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 err != nil {
|
||||||
if m.fallbackLoader != nil {
|
if m.fallbackLoader != nil {
|
||||||
return (*m.fallbackLoader).Get(path)
|
return (*m.fallbackLoader).Get(path)
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package route
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
assetfs "github.com/elazarl/go-bindata-assetfs"
|
|
||||||
"amuz.es/go/mnemonics/bind/static"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"amuz.es/go/mnemonics/bind/static"
|
||||||
|
assetfs "github.com/elazarl/go-bindata-assetfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type binaryFileSystem struct {
|
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 {
|
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
|
||||||
|
|
||||||
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
||||||
if _, err := b.fs.Open(p); err != nil {
|
if _, err := b.fs.Open(p); err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -26,10 +26,13 @@ func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
|
||||||
return false
|
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}
|
fs := &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo, Prefix: root}
|
||||||
return &binaryFileSystem{
|
return &binaryFileSystem{
|
||||||
fs,
|
fs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StaticDebug(root string) http.FileSystem {
|
||||||
|
return http.Dir("asset/static")
|
||||||
|
}
|
Loading…
Reference in New Issue