-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (91 loc) · 1.9 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"embed"
"errors"
"flag"
"fmt"
"html/template"
"io/fs"
"log"
"github.com/fsnotify/fsnotify"
"github.com/marcopeocchi/rei/internal"
"github.com/marcopeocchi/rei/internal/config"
)
//go:generate npm run build
var (
//go:embed tmpl/* tmpl/layouts/*
files embed.FS
tmpls map[string]*template.Template
//go:embed static
static embed.FS
configPath string
wallpaperPath string
)
func init() {
flag.StringVar(&configPath, "conf", "./config.yml", "path of configuration file")
flag.StringVar(&wallpaperPath, "bg", "./static/wallpaper.avif", "path of background image")
flag.Parse()
}
func parseTemplates() error {
tmpls = make(map[string]*template.Template)
tmplFiles, err := fs.ReadDir(files, "tmpl")
if err != nil {
return errors.New("cannot open templates directory")
}
for _, tmpl := range tmplFiles {
if tmpl.IsDir() {
continue
}
parsed, err := template.ParseFS(
files,
"tmpl/"+tmpl.Name(),
"tmpl/layouts/*.html",
"tmpl/fragments/*.html",
)
if err != nil {
return fmt.Errorf("cannot parse template %s, err: %w", tmpl.Name(), err)
}
log.Println("parsed", tmpl.Name())
tmpls[tmpl.Name()] = parsed
}
return nil
}
func main() {
cfg := config.New(configPath)
if err := parseTemplates(); err != nil {
log.Fatalln(err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
log.Println("modified cofig file")
cfg.Load(configPath)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
if err := watcher.Add(configPath); err != nil {
log.Fatalln(err)
}
internal.RunBlocking(internal.ServerConfig{
TmplFS: files,
Templates: &tmpls,
StaticFS: static,
Config: cfg,
})
}