-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
164 lines (150 loc) · 4.45 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
WebChunk, web server for block game maps
Copyright (C) 2022 Maxim Zhuchkov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Contact me via mail: [email protected] or Discord: MaX#6717
*/
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"reflect"
"github.com/davecgh/go-spew/spew"
"github.com/fsnotify/fsnotify"
"github.com/maxsupermanhd/lac"
)
const (
plainmsgColorRed = iota
plainmsgColorGreen
)
var templates *template.Template
var templatesFuncs = template.FuncMap{
"noescape": func(s string) template.HTML {
return template.HTML(s)
},
"noescapeJS": func(s string) template.JS {
return template.JS(s)
},
"inc": func(i int) int {
return i + 1
},
"avail": func(name string, data interface{}) bool {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
m, ok := data.(map[string]interface{})
if ok {
_, ok := m[name]
return ok
}
if v.Kind() != reflect.Struct {
return false
}
return v.FieldByName(name).IsValid()
},
"spew": spew.Sdump,
"add": func(a, b int) int {
return a + b
},
"FormatBytes": ByteCountIEC,
"FormatPercent": FormatPercent,
"getTypeString": func(a any) string {
return reflect.TypeOf(a).String()
},
}
func robotsHandler(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "User-agent: *\nDisallow: /\n\n\n")
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/favicon.ico")
}
func plainmsg(w http.ResponseWriter, r *http.Request, color int, msg string) {
templateRespond("plainmsg", w, r, map[string]interface{}{
"msgred": color == plainmsgColorRed,
"msggreen": color == plainmsgColorGreen,
"msg": msg})
}
func templateManager(exitchan <-chan struct{}, cfg *lac.ConfSubtree) {
log.Println("Loading web templates")
templatesGlob := cfg.GetDSString("templates/*.gohtml", "templates_glob")
var err error
templates, err = template.New("main").Funcs(templatesFuncs).ParseGlob(templatesGlob)
if err != nil {
log.Fatal(err)
}
if !cfg.GetDSBool(false, "template_reload") {
return
}
log.Println("Starting filesystem watcher for web templates")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("Failed to create watcher: ", err)
}
templatesDir := cfg.GetDSString("templates/", "templates_dir")
err = watcher.Add(templatesDir)
if err != nil {
log.Fatal("Failed to add wathcer path: ", err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Println("Layouts watcher failed to read from events channel")
return
}
log.Println("Event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Updating templates")
nlayouts, err := template.New("main").Funcs(templatesFuncs).ParseGlob(templatesGlob)
if err != nil {
log.Println("Error while parsing templates:", err.Error())
} else {
templates = nlayouts.Funcs(templatesFuncs)
}
}
case err, ok := <-watcher.Errors:
if !ok {
log.Println("Layouts watcher failed to read from error channel")
return
}
log.Println("Layouts watcher error:", err)
case <-exitchan:
watcher.Close()
log.Println("Layouts watcher stopped")
return
}
}
}
func templateRespond(page string, w http.ResponseWriter, _ *http.Request, m map[string]interface{}) {
in := templates.Lookup(page)
if in != nil {
m["NavWhere"] = page
m["WebChunkVersion"] = fmt.Sprintf("%s %s built %s %s", GitTag, CommitHash, BuildTime, GoVersion)
w.Header().Set("Server", "WebChunk webserver "+CommitHash)
w.Header().Set("Cache-Control", "no-cache")
err := in.Execute(w, m)
if err != nil {
log.Println(err)
}
} else {
log.Printf("Template %s not found!", page)
http.Error(w, "", http.StatusNotFound)
}
}
func basicTemplateResponseHandler(page string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
templateRespond(page, w, r, map[string]any{})
}
}