forked from go-macaron/bindata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindata.go
100 lines (79 loc) · 2.18 KB
/
bindata.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2014 Dustin Webber
// Copyright 2015 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package bindata is a helper module that allows to use in-memory static and template files for Macaron.
package bindata
import (
"github.com/Unknwon/macaron"
"github.com/elazarl/go-bindata-assetfs"
)
const _VERSION = "0.0.1"
func Version() string {
return _VERSION
}
type (
templateFileSystem struct {
files []macaron.TemplateFile
}
templateFile struct {
name string
data []byte
ext string
}
Options struct {
// Asset should return content of file in path if exists
Asset func(path string) ([]byte, error)
// AssetDir should return list of files in the path
AssetDir func(path string) ([]string, error)
// AssetNames should return list of all asset names
AssetNames func() []string
// Prefix would be prepended to http requests
Prefix string
}
)
func Static(opt Options) *assetfs.AssetFS {
fs := &assetfs.AssetFS{
Asset: opt.Asset,
AssetDir: opt.AssetDir,
Prefix: opt.Prefix,
}
return fs
}
func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
return templates.files
}
func (f *templateFile) Name() string {
return f.name
}
func (f *templateFile) Data() []byte {
return f.data
}
func (f *templateFile) Ext() string {
return f.ext
}
func Templates(opt Options) templateFileSystem {
fs := templateFileSystem{}
fs.files = make([]macaron.TemplateFile, 0, 10)
list := opt.AssetNames()
for _, key := range list {
ext := macaron.GetExt(key)
data, err := opt.Asset(key)
if err != nil {
continue
}
name := (key[0 : len(key)-len(ext)])
fs.files = append(fs.files, &templateFile{name, data, ext})
}
return fs
}