-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
58 lines (54 loc) · 1.48 KB
/
templates.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
package gw
import (
"path/filepath"
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
"html/template"
"github.com/juju/errors"
)
func ListenForReload(c Settings, dir string) {
for {
signal_channel := make(chan os.Signal, 1)
signal.Notify(signal_channel, syscall.SIGUSR1)
<- signal_channel
tmpls, err := LoadTemplates(dir)
if err != nil {
log.Print(err)
}
c.SetTemplates(tmpls)
}
}
func LoadTemplates(directory string) (map[string]*template.Template, error) {
tmpls := make(map[string]*template.Template)
fullDir, err := filepath.Abs(directory)
if err != nil {
return nil, errors.Trace(err)
}
fs, err := ioutil.ReadDir(fullDir)
if err != nil {
return nil, errors.Trace(err)
}
for _, f := range fs {
if !f.IsDir() && f.Name() != "base.html" {
if filepath.Ext(f.Name()) == ".html" {
tmpls[filepath.Base(f.Name())] = template.Must(template.New("").Delims("<<", ">>").ParseFiles(filepath.Join(fullDir, "base.html"), filepath.Join(fullDir, f.Name())))
}
}
// assuming only one level of subdirectories in views
if f.IsDir() {
sfs, serr := ioutil.ReadDir(filepath.Join(fullDir, f.Name()))
if serr != nil {
return nil, errors.Trace(serr)
}
for _, sf := range sfs {
if filepath.Ext(sf.Name()) == ".html" {
tmpls[f.Name() + "/" + filepath.Base(sf.Name())] = template.Must(template.New("").Delims("<<", ">>").ParseFiles(filepath.Join(fullDir, "base.html"), filepath.Join(fullDir, f.Name(), sf.Name())))
}
}
}
}
return tmpls, nil
}