diff options
Diffstat (limited to 'gopherproxy.go')
-rw-r--r-- | gopherproxy.go | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/gopherproxy.go b/gopherproxy.go index 42f67cf..6f1e8ec 100644 --- a/gopherproxy.go +++ b/gopherproxy.go | |||
@@ -1,6 +1,7 @@ | |||
1 | package gopherproxy | 1 | package gopherproxy |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bytes" | ||
4 | "fmt" | 5 | "fmt" |
5 | "html/template" | 6 | "html/template" |
6 | "io" | 7 | "io" |
@@ -73,7 +74,8 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str | |||
73 | return tpl.Execute(w, struct { | 74 | return tpl.Execute(w, struct { |
74 | Title string | 75 | Title string |
75 | Lines []Item | 76 | Lines []Item |
76 | }{title, out}) | 77 | RawText string |
78 | }{title, out, ""}) | ||
77 | } | 79 | } |
78 | 80 | ||
79 | // GopherHandler returns a Handler that proxies requests | 81 | // GopherHandler returns a Handler that proxies requests |
@@ -125,7 +127,18 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri | |||
125 | } | 127 | } |
126 | 128 | ||
127 | if res.Body != nil { | 129 | if res.Body != nil { |
128 | io.Copy(w, res.Body) | 130 | if strings.HasSuffix(uri, ".txt") { |
131 | // handle .txt files | ||
132 | buf := new(bytes.Buffer) | ||
133 | buf.ReadFrom(res.Body) | ||
134 | tpl.Execute(w, struct { | ||
135 | Title string | ||
136 | RawText string | ||
137 | Lines []Item | ||
138 | }{uri, buf.String(), nil}) | ||
139 | } else { | ||
140 | io.Copy(w, res.Body) | ||
141 | } | ||
129 | } else { | 142 | } else { |
130 | if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { | 143 | if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { |
131 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | 144 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) |
@@ -186,6 +199,7 @@ func ListenAndServe(bind, robotsfile, uri string) error { | |||
186 | 199 | ||
187 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) | 200 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) |
188 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) | 201 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) |
202 | http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) | ||
189 | 203 | ||
190 | return http.ListenAndServe(bind, nil) | 204 | return http.ListenAndServe(bind, nil) |
191 | } | 205 | } |