From ce19e19efce3139d2c7b4024274b6ed2683e015e Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Tue, 4 Jun 2019 22:28:02 +0200 Subject: Style improvements --- gopherproxy.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 6f1e8ec..dc849d0 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -22,7 +22,7 @@ type Item struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -73,9 +73,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str return tpl.Execute(w, struct { Title string + Style string Lines []Item RawText string - }{title, out, ""}) + }{title, styletext, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -83,7 +84,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str // to the request path and renders the content using the provided template. // The optional robots parameters points to a robotstxt.RobotsData struct // to test user agents against a configurable robotst.txt file. -func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri string) http.HandlerFunc { +func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, styletext string, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { agent := req.UserAgent() path := strings.TrimPrefix(req.URL.Path, "/") @@ -127,20 +128,21 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri } if res.Body != nil { - if strings.HasSuffix(uri, ".txt") { + if strings.HasSuffix(uri, ".txt") || strings.HasSuffix(uri, ".md") { // handle .txt files buf := new(bytes.Buffer) buf.ReadFrom(res.Body) tpl.Execute(w, struct { Title string + Style string RawText string Lines []Item - }{uri, buf.String(), nil}) + }{uri, styletext, buf.String(), nil}) } else { io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { + if err := renderDirectory(w, tpl, styletext, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } @@ -187,19 +189,30 @@ func ListenAndServe(bind, robotsfile, uri string) error { } } + styledata, err := ioutil.ReadFile(".style") + if err == nil { + styletext = string(styledata) + } + tpldata, err := ioutil.ReadFile(".template") if err == nil { tpltext = string(tpldata) } - tpl, err = template.New("gophermenu").Parse(tpltext) + funcMap := template.FuncMap{ + "safeCss": func(s string) template.CSS { + return template.CSS(s) + }, + } + + tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) if err != nil { log.Fatal(err) } - http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) + http.HandleFunc("/", GopherHandler(tpl, robotsdata, styletext, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) - http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) + // http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) return http.ListenAndServe(bind, nil) } -- cgit v1.2.3-54-g00ecf