diff options
Diffstat (limited to 'gopherproxy.go')
| -rw-r--r-- | gopherproxy.go | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/gopherproxy.go b/gopherproxy.go index fcce1ee..dd416b2 100644 --- a/gopherproxy.go +++ b/gopherproxy.go | |||
| @@ -13,19 +13,16 @@ import ( | |||
| 13 | "github.com/prologic/go-gopher" | 13 | "github.com/prologic/go-gopher" |
| 14 | ) | 14 | ) |
| 15 | 15 | ||
| 16 | type tplRow struct { | 16 | type Item struct { |
| 17 | Link template.URL | 17 | Link template.URL |
| 18 | Type string | 18 | Type string |
| 19 | Text string | 19 | Text string |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | // Handler is an aliased type for the standard HTTP handler functions | ||
| 23 | type Handler func(w http.ResponseWriter, req *http.Request) | ||
| 24 | |||
| 25 | func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { | 22 | func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { |
| 26 | var title string | 23 | var title string |
| 27 | 24 | ||
| 28 | out := make([]tplRow, len(d.Items)) | 25 | out := make([]Item, len(d.Items)) |
| 29 | 26 | ||
| 30 | for i, x := range d.Items { | 27 | for i, x := range d.Items { |
| 31 | if x.Type == gopher.INFO && x.Selector == "TITLE" { | 28 | if x.Type == gopher.INFO && x.Selector == "TITLE" { |
| @@ -33,7 +30,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str | |||
| 33 | continue | 30 | continue |
| 34 | } | 31 | } |
| 35 | 32 | ||
| 36 | tr := tplRow{ | 33 | tr := Item{ |
| 37 | Text: x.Description, | 34 | Text: x.Description, |
| 38 | Type: x.Type.String(), | 35 | Type: x.Type.String(), |
| 39 | } | 36 | } |
| @@ -73,14 +70,14 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str | |||
| 73 | 70 | ||
| 74 | return tpl.Execute(w, struct { | 71 | return tpl.Execute(w, struct { |
| 75 | Title string | 72 | Title string |
| 76 | Lines []tplRow | 73 | Lines []Item |
| 77 | }{title, out}) | 74 | }{title, out}) |
| 78 | } | 75 | } |
| 79 | 76 | ||
| 80 | // MakeGopherProxyHandler returns a Handler that proxies requests | 77 | // Handler returns a Handler that proxies requests |
| 81 | // to the specified Gopher server as denoated by the first argument | 78 | // to the specified Gopher server as denoated by the first argument |
| 82 | // to the request path and renders the content using the provided template. | 79 | // to the request path and renders the content using the provided template. |
| 83 | func MakeGopherProxyHandler(tpl *template.Template, uri string) Handler { | 80 | func Handler(tpl *template.Template, uri string) http.HandlerFunc { |
| 84 | return func(w http.ResponseWriter, req *http.Request) { | 81 | return func(w http.ResponseWriter, req *http.Request) { |
| 85 | parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/") | 82 | parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/") |
| 86 | hostport := parts[0] | 83 | hostport := parts[0] |
| @@ -147,6 +144,6 @@ func ListenAndServe(bind, uri string) error { | |||
| 147 | log.Fatal(err) | 144 | log.Fatal(err) |
| 148 | } | 145 | } |
| 149 | 146 | ||
| 150 | http.HandleFunc("/", MakeGopherProxyHandler(tpl, uri)) | 147 | http.HandleFunc("/", Handler(tpl, uri)) |
| 151 | return http.ListenAndServe(bind, nil) | 148 | return http.ListenAndServe(bind, nil) |
| 152 | } | 149 | } |
