diff options
Diffstat (limited to 'internal/port/main.go')
-rw-r--r-- | internal/port/main.go | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/internal/port/main.go b/internal/port/main.go index 9fa245e..267df44 100644 --- a/internal/port/main.go +++ b/internal/port/main.go | |||
@@ -26,23 +26,19 @@ type AssetList struct { | |||
26 | PropFontW2 string | 26 | PropFontW2 string |
27 | } | 27 | } |
28 | 28 | ||
29 | type TemplateVariables struct { | 29 | type startTemplateVariables struct { |
30 | Title string | 30 | Title string |
31 | URI string | 31 | URL string |
32 | Assets AssetList | 32 | Assets AssetList |
33 | RawText string | 33 | Content string |
34 | Lines []Item | ||
35 | Error bool | ||
36 | Protocol string | ||
37 | } | 34 | } |
38 | 35 | ||
39 | func DefaultHandler(tpl *template.Template, startpagetext string, assetList AssetList) http.HandlerFunc { | 36 | func DefaultHandler(tpl *template.Template, startpagetext string, assetList AssetList) http.HandlerFunc { |
40 | return func(w http.ResponseWriter, req *http.Request) { | 37 | return func(w http.ResponseWriter, req *http.Request) { |
41 | if err := tpl.Execute(w, TemplateVariables{ | 38 | if err := tpl.Execute(w, startTemplateVariables{ |
42 | Title: "Gopher/Gemini proxy", | 39 | Title: "Gopher/Gemini proxy", |
43 | Assets: assetList, | 40 | Assets: assetList, |
44 | RawText: startpagetext, | 41 | Content: startpagetext, |
45 | Protocol: "startpage", | ||
46 | }); err != nil { | 42 | }); err != nil { |
47 | log.Println("Template error: " + err.Error()) | 43 | log.Println("Template error: " + err.Error()) |
48 | } | 44 | } |
@@ -118,7 +114,7 @@ func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { | |||
118 | // a robotstxt.RobotsData struct for testing user agents against | 114 | // a robotstxt.RobotsData struct for testing user agents against |
119 | // a configurable robots.txt file. | 115 | // a configurable robots.txt file. |
120 | func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug bool, vipsconcurrency int) error { | 116 | func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug bool, vipsconcurrency int) error { |
121 | box := packr.New("assets", "../assets") | 117 | box := packr.New("assets", "../../assets") |
122 | 118 | ||
123 | // | 119 | // |
124 | // Robots | 120 | // Robots |
@@ -205,24 +201,6 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b | |||
205 | // | 201 | // |
206 | // | 202 | // |
207 | 203 | ||
208 | var templates *template.Template | ||
209 | |||
210 | var allFiles []string | ||
211 | files, err := ioutil.ReadDir("./tpl") | ||
212 | if err != nil { | ||
213 | fmt.Println(err) | ||
214 | } | ||
215 | for _, file := range files { | ||
216 | filename := file.Name() | ||
217 | if strings.HasSuffix(filename, ".html") { | ||
218 | allFiles = append(allFiles, "./tpl/"+filename) | ||
219 | } | ||
220 | } | ||
221 | |||
222 | templates, err = template.ParseFiles(allFiles...) | ||
223 | |||
224 | // | ||
225 | |||
226 | funcMap := template.FuncMap{ | 204 | funcMap := template.FuncMap{ |
227 | "safeHtml": func(s string) template.HTML { | 205 | "safeHtml": func(s string) template.HTML { |
228 | return template.HTML(s) | 206 | return template.HTML(s) |
@@ -266,9 +244,30 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b | |||
266 | 244 | ||
267 | // | 245 | // |
268 | 246 | ||
269 | startpageTpl := templates.Lookup("startpage.html").Funcs(funcMap) | 247 | var templates *template.Template |
270 | geminiTpl := templates.Lookup("gemini.html").Funcs(funcMap) | 248 | |
271 | gopherTpl := templates.Lookup("gopher.html").Funcs(funcMap) | 249 | var allFiles []string |
250 | files, err := ioutil.ReadDir("./internal/port/tpl") | ||
251 | if err != nil { | ||
252 | return err | ||
253 | } | ||
254 | for _, file := range files { | ||
255 | filename := file.Name() | ||
256 | if strings.HasSuffix(filename, ".html") { | ||
257 | allFiles = append(allFiles, "./internal/port/tpl/"+filename) | ||
258 | } | ||
259 | } | ||
260 | |||
261 | templates, err = template.New("main.html").Funcs(funcMap).ParseFiles(allFiles...) | ||
262 | if err != nil { | ||
263 | return err | ||
264 | } | ||
265 | |||
266 | // | ||
267 | |||
268 | startpageTpl := templates.Lookup("startpage.html") | ||
269 | geminiTpl := templates.Lookup("gemini.html") | ||
270 | gopherTpl := templates.Lookup("gopher.html") | ||
272 | 271 | ||
273 | // | 272 | // |
274 | // | 273 | // |