From ac770231436f8a17d348a6a0ab934429df3c57d0 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 18 May 2020 16:12:43 +0200 Subject: WIP: Refactoring --- internal/port/main.go | 69 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'internal/port/main.go') 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 { PropFontW2 string } -type TemplateVariables struct { - Title string - URI string - Assets AssetList - RawText string - Lines []Item - Error bool - Protocol string +type startTemplateVariables struct { + Title string + URL string + Assets AssetList + Content string } func DefaultHandler(tpl *template.Template, startpagetext string, assetList AssetList) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { - if err := tpl.Execute(w, TemplateVariables{ - Title: "Gopher/Gemini proxy", - Assets: assetList, - RawText: startpagetext, - Protocol: "startpage", + if err := tpl.Execute(w, startTemplateVariables{ + Title: "Gopher/Gemini proxy", + Assets: assetList, + Content: startpagetext, }); err != nil { log.Println("Template error: " + err.Error()) } @@ -118,7 +114,7 @@ func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { // a robotstxt.RobotsData struct for testing user agents against // a configurable robots.txt file. func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug bool, vipsconcurrency int) error { - box := packr.New("assets", "../assets") + box := packr.New("assets", "../../assets") // // Robots @@ -205,24 +201,6 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b // // - var templates *template.Template - - var allFiles []string - files, err := ioutil.ReadDir("./tpl") - if err != nil { - fmt.Println(err) - } - for _, file := range files { - filename := file.Name() - if strings.HasSuffix(filename, ".html") { - allFiles = append(allFiles, "./tpl/"+filename) - } - } - - templates, err = template.ParseFiles(allFiles...) - - // - funcMap := template.FuncMap{ "safeHtml": func(s string) template.HTML { return template.HTML(s) @@ -266,9 +244,30 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b // - startpageTpl := templates.Lookup("startpage.html").Funcs(funcMap) - geminiTpl := templates.Lookup("gemini.html").Funcs(funcMap) - gopherTpl := templates.Lookup("gopher.html").Funcs(funcMap) + var templates *template.Template + + var allFiles []string + files, err := ioutil.ReadDir("./internal/port/tpl") + if err != nil { + return err + } + for _, file := range files { + filename := file.Name() + if strings.HasSuffix(filename, ".html") { + allFiles = append(allFiles, "./internal/port/tpl/"+filename) + } + } + + templates, err = template.New("main.html").Funcs(funcMap).ParseFiles(allFiles...) + if err != nil { + return err + } + + // + + startpageTpl := templates.Lookup("startpage.html") + geminiTpl := templates.Lookup("gemini.html") + gopherTpl := templates.Lookup("gopher.html") // // -- cgit v1.2.3-54-g00ecf