This repository was archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathassets.go
73 lines (66 loc) · 1.7 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"html/template"
"net/http"
"strings"
assetfs "github.com/elazarl/go-bindata-assetfs"
log "github.com/sirupsen/logrus"
)
type binaryFileSystem struct {
fs http.FileSystem
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
return b.fs.Open(name)
}
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
}
return true
}
return false
}
func newBinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{
Asset: Asset,
// Don't render directory index, return 404 for /static/ requests)
AssetDir: func(path string) ([]string, error) { return nil, errors.New("Not found") },
Prefix: root,
}
return &binaryFileSystem{
fs,
}
}
// load all templates from binary asset resource
// this function will iterate all files with given prefix (e.g. /templates/)
// and return Template instance with all templates loaded
func loadTemplates(t *template.Template, prefix string) *template.Template {
for _, filename := range AssetNames() {
if strings.HasPrefix(filename, prefix) {
templateContent, err := Asset(filename)
if err != nil {
log.Fatal(err)
}
var tmpl *template.Template
if t == nil {
// if template wasn't yet initialized do it here
t = template.New(filename)
}
if filename == t.Name() {
tmpl = t
} else {
// if we already have an instance of template.Template then
// add a new file to it
tmpl = t.New(filename)
}
_, err = tmpl.Parse(string(templateContent))
if err != nil {
log.Fatal(err)
return nil
}
}
}
return t
}