From adca4d518eea9533e6054666ce6e5681c5a6f196 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Tue, 4 Jun 2019 19:58:44 +0200 Subject: Add custom styles --- gopherproxy.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 42f67cf..6f1e8ec 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -1,6 +1,7 @@ package gopherproxy import ( + "bytes" "fmt" "html/template" "io" @@ -73,7 +74,8 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str return tpl.Execute(w, struct { Title string Lines []Item - }{title, out}) + RawText string + }{title, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -125,7 +127,18 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri } if res.Body != nil { - io.Copy(w, res.Body) + if strings.HasSuffix(uri, ".txt") { + // handle .txt files + buf := new(bytes.Buffer) + buf.ReadFrom(res.Body) + tpl.Execute(w, struct { + Title string + RawText string + Lines []Item + }{uri, buf.String(), nil}) + } else { + io.Copy(w, res.Body) + } } else { if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) @@ -186,6 +199,7 @@ func ListenAndServe(bind, robotsfile, uri string) error { http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) + http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) return http.ListenAndServe(bind, nil) } -- cgit v1.2.3-54-g00ecf