-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathloader.go
71 lines (55 loc) · 1.2 KB
/
loader.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
package gohaml
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
// Loader, Entry are not particularly nice and custom tailored to the http handlers
// needs. Probably should be made private.
// The whole convolute is a victim of premature optimization ...
type Loader interface {
Load(id interface{}) (entry *Engine, err error)
}
type fileSystemLoader struct {
baseDir string
}
func NewFileSystemLoader(dir string) (loader Loader, err error) {
var f *os.File
if f, err = os.Open(dir); err != nil {
return
}
defer f.Close()
var fi os.FileInfo
if fi, err = f.Stat(); err != nil {
return
}
if !fi.IsDir() {
return nil, fmt.Errorf("%s: not a directory", fi.Name())
}
if !strings.HasSuffix(dir, "/") {
dir += "/"
}
return &fileSystemLoader{dir}, nil
}
func (l *fileSystemLoader) Load(id_string interface{}) (engine *Engine, err error) {
// check
id, ok := id_string.(string)
if !ok {
err = fmt.Errorf("id: %s is not a string", id)
return
}
var file *os.File
// check fs
var path = l.baseDir + id
if file, err = os.Open(path); err != nil {
return
}
defer file.Close()
var bb bytes.Buffer
if _, err = io.Copy(&bb, file); err != nil {
return
}
return NewEngine(bb.String())
}