-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_assets.go
More file actions
176 lines (152 loc) · 4.62 KB
/
server_assets.go
File metadata and controls
176 lines (152 loc) · 4.62 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"embed"
"encoding/json"
"html/template"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
)
//go:embed assets/templates/*
var embedTemplates embed.FS
//go:embed assets/static/*
var embedStatic embed.FS
// Template helper functions: stringify, keybindings.
var functionMap = template.FuncMap{
"stringify": func(v interface{}) string {
bytes, err := json.Marshal(v)
if err != nil {
return "{}"
}
return string(bytes)
},
"keybindings": func() template.JS {
return getKeybindingsJSON()
},
"autocomplete_config": func() template.JS {
return getAutocompleteConfigJSON()
},
"search_config": func() template.JS {
return getNotebookSearchConfigJSON()
},
}
/* ─── Custom template render ─────────────────────────────── */
type customHTMLRender struct {
Template *template.Template
}
func (r *customHTMLRender) Instance(name string, data interface{}) render.Render {
return &customHTMLRenderInstance{
Template: r.Template,
Name: name,
Data: data,
}
}
type customHTMLRenderInstance struct {
Template *template.Template
Name string
Data interface{}
}
func (r *customHTMLRenderInstance) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
}
func (r *customHTMLRenderInstance) WriteContentType(w http.ResponseWriter) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = []string{"text/html; charset=utf-8"}
}
}
/* ─── Template loading ───────────────────────────────────── */
func setupTemplate(assets string, router *gin.Engine) (http.FileSystem, *template.Template) {
if len(assets) > 0 {
return setupTemplateDebug(assets, router)
}
return setupTemplateEmbed(router)
}
func setupTemplateEmbed(router *gin.Engine) (http.FileSystem, *template.Template) {
tmpl := template.New("").Funcs(functionMap)
err := fs.WalkDir(embedTemplates, "assets/templates", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(path, ".tmpl") {
return nil
}
data, readErr := embedTemplates.ReadFile(path)
if readErr != nil {
return readErr
}
name := filepath.Base(path)
_, parseErr := tmpl.New(name).Parse(string(data))
return parseErr
})
if err != nil {
panic("failed to load embedded templates: " + err.Error())
}
router.HTMLRender = &customHTMLRender{Template: tmpl}
staticFS, fsErr := fs.Sub(embedStatic, "assets/static")
if fsErr != nil {
panic("failed to load embedded static assets: " + fsErr.Error())
}
return http.FS(staticFS), tmpl
}
func setupTemplateDebug(assets string, router *gin.Engine) (http.FileSystem, *template.Template) {
tmpl := template.New("").Funcs(functionMap)
templateDir := filepath.Join(assets, "templates")
err := filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".tmpl") {
return nil
}
data, readErr := os.ReadFile(path)
if readErr != nil {
return readErr
}
name := filepath.Base(path)
_, parseErr := tmpl.New(name).Parse(string(data))
return parseErr
})
if err != nil {
panic("failed to load debug templates: " + err.Error())
}
router.HTMLRender = &customHTMLRender{Template: tmpl}
staticDir := filepath.Join(assets, "static")
return http.Dir(staticDir), tmpl
}
/* ─── Static file routes ─────────────────────────────────── */
func serverAddAssets(root *gin.RouterGroup, assets string, static http.FileSystem, templates *template.Template) {
root.StaticFS("/static", static)
root.GET("/favicon.ico", func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusNoContent)
_, _ = io.WriteString(c.Writer, "")
})
root.GET("/reload", func(c *gin.Context) {
if len(assets) > 0 {
templateDir := filepath.Join(assets, "templates")
newTmpl := template.New("").Funcs(functionMap)
_ = filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.HasSuffix(path, ".tmpl") {
return err
}
data, readErr := os.ReadFile(path)
if readErr != nil {
return readErr
}
name := filepath.Base(path)
_, parseErr := newTmpl.New(name).Parse(string(data))
return parseErr
})
templates = newTmpl
}
c.HTML(200, "reload.tmpl", gin.H{
"root": webroot,
})
})
}