aboutsummaryrefslogtreecommitdiffstats
path: root/gopherproxy.go
diff options
context:
space:
mode:
authorFeuerfuchs <git@feuerfuchs.dev>2019-06-04 22:28:02 +0200
committerFeuerfuchs <git@feuerfuchs.dev>2019-06-04 22:28:02 +0200
commitce19e19efce3139d2c7b4024274b6ed2683e015e (patch)
treea6a06ff79bceec34fcf47c0721f03bd5e0f943fb /gopherproxy.go
parentAdd custom styles (diff)
downloadgopherproxy-ce19e19efce3139d2c7b4024274b6ed2683e015e.tar.gz
gopherproxy-ce19e19efce3139d2c7b4024274b6ed2683e015e.tar.bz2
gopherproxy-ce19e19efce3139d2c7b4024274b6ed2683e015e.zip
Style improvements
Diffstat (limited to 'gopherproxy.go')
-rw-r--r--gopherproxy.go31
1 files changed, 22 insertions, 9 deletions
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 {
22 Text string 22 Text string
23} 23}
24 24
25func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { 25func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, hostport string, d gopher.Directory) error {
26 var title string 26 var title string
27 27
28 out := make([]Item, len(d.Items)) 28 out := make([]Item, len(d.Items))
@@ -73,9 +73,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str
73 73
74 return tpl.Execute(w, struct { 74 return tpl.Execute(w, struct {
75 Title string 75 Title string
76 Style string
76 Lines []Item 77 Lines []Item
77 RawText string 78 RawText string
78 }{title, out, ""}) 79 }{title, styletext, out, ""})
79} 80}
80 81
81// GopherHandler returns a Handler that proxies requests 82// GopherHandler returns a Handler that proxies requests
@@ -83,7 +84,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str
83// to the request path and renders the content using the provided template. 84// to the request path and renders the content using the provided template.
84// The optional robots parameters points to a robotstxt.RobotsData struct 85// The optional robots parameters points to a robotstxt.RobotsData struct
85// to test user agents against a configurable robotst.txt file. 86// to test user agents against a configurable robotst.txt file.
86func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri string) http.HandlerFunc { 87func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, styletext string, uri string) http.HandlerFunc {
87 return func(w http.ResponseWriter, req *http.Request) { 88 return func(w http.ResponseWriter, req *http.Request) {
88 agent := req.UserAgent() 89 agent := req.UserAgent()
89 path := strings.TrimPrefix(req.URL.Path, "/") 90 path := strings.TrimPrefix(req.URL.Path, "/")
@@ -127,20 +128,21 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri
127 } 128 }
128 129
129 if res.Body != nil { 130 if res.Body != nil {
130 if strings.HasSuffix(uri, ".txt") { 131 if strings.HasSuffix(uri, ".txt") || strings.HasSuffix(uri, ".md") {
131 // handle .txt files 132 // handle .txt files
132 buf := new(bytes.Buffer) 133 buf := new(bytes.Buffer)
133 buf.ReadFrom(res.Body) 134 buf.ReadFrom(res.Body)
134 tpl.Execute(w, struct { 135 tpl.Execute(w, struct {
135 Title string 136 Title string
137 Style string
136 RawText string 138 RawText string
137 Lines []Item 139 Lines []Item
138 }{uri, buf.String(), nil}) 140 }{uri, styletext, buf.String(), nil})
139 } else { 141 } else {
140 io.Copy(w, res.Body) 142 io.Copy(w, res.Body)
141 } 143 }
142 } else { 144 } else {
143 if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { 145 if err := renderDirectory(w, tpl, styletext, hostport, res.Dir); err != nil {
144 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) 146 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
145 return 147 return
146 } 148 }
@@ -187,19 +189,30 @@ func ListenAndServe(bind, robotsfile, uri string) error {
187 } 189 }
188 } 190 }
189 191
192 styledata, err := ioutil.ReadFile(".style")
193 if err == nil {
194 styletext = string(styledata)
195 }
196
190 tpldata, err := ioutil.ReadFile(".template") 197 tpldata, err := ioutil.ReadFile(".template")
191 if err == nil { 198 if err == nil {
192 tpltext = string(tpldata) 199 tpltext = string(tpldata)
193 } 200 }
194 201
195 tpl, err = template.New("gophermenu").Parse(tpltext) 202 funcMap := template.FuncMap{
203 "safeCss": func(s string) template.CSS {
204 return template.CSS(s)
205 },
206 }
207
208 tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext)
196 if err != nil { 209 if err != nil {
197 log.Fatal(err) 210 log.Fatal(err)
198 } 211 }
199 212
200 http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) 213 http.HandleFunc("/", GopherHandler(tpl, robotsdata, styletext, uri))
201 http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) 214 http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata))
202 http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) 215 // http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
203 216
204 return http.ListenAndServe(bind, nil) 217 return http.ListenAndServe(bind, nil)
205} 218}