-
Notifications
You must be signed in to change notification settings - Fork 41
/
assets.go
46 lines (40 loc) · 1.15 KB
/
assets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"html/template"
"github.com/GeertJohan/go.rice"
"github.com/gin-gonic/contrib/renders/multitemplate"
"github.com/gin-gonic/gin"
"github.com/Depado/goploader/server/logger"
)
// InitAssetsTemplates initializes the router to use either a ricebox or the
// filesystem in case the ricebox couldn't be found.
func InitAssetsTemplates(r *gin.Engine, tbox, abox *rice.Box, verbose bool, names ...string) error {
var err error
if tbox != nil {
mt := multitemplate.New()
var tmpl string
var message *template.Template
for _, x := range names {
if tmpl, err = tbox.String(x); err != nil {
return err
}
if message, err = template.New(x).Parse(tmpl); err != nil {
return err
}
mt.Add(x, message)
}
logger.Debug("server", "Loaded templates from \"templates\" box")
r.HTMLRender = mt
} else {
r.LoadHTMLGlob("templates/*")
logger.Debug("server", "Loaded templates from disk")
}
if abox != nil {
r.StaticFS("/static", abox.HTTPBox())
logger.Debug("server", "Loaded assets from \"assets\" box")
} else {
r.Static("/static", "assets")
logger.Debug("server", "Loaded assets from disk")
}
return nil
}