From 74783dccd7cbd6f7dba1c6894d5b68ec0d93131d Mon Sep 17 00:00:00 2001 From: James Mills Date: Sun, 15 Oct 2017 13:16:23 -0700 Subject: Minor refactor/cleanup --- cmd/gopherproxy/main.go | 2 +- gopherproxy.go | 17 +++++++---------- template.go | 18 ------------------ 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/cmd/gopherproxy/main.go b/cmd/gopherproxy/main.go index fa20c4e..9a7d1f9 100644 --- a/cmd/gopherproxy/main.go +++ b/cmd/gopherproxy/main.go @@ -8,7 +8,7 @@ import ( ) var ( - bind = flag.String("bind", "0.0.0.0:80", "[int]:port to bind to") + bind = flag.String("bind", "0.0.0.0:8000", "[int]:port to bind to") uri = flag.String("uri", "floodgap.com", ":[port] to proxy to") ) diff --git a/gopherproxy.go b/gopherproxy.go index fcce1ee..dd416b2 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -13,19 +13,16 @@ import ( "github.com/prologic/go-gopher" ) -type tplRow struct { +type Item struct { Link template.URL Type string Text string } -// Handler is an aliased type for the standard HTTP handler functions -type Handler func(w http.ResponseWriter, req *http.Request) - func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { var title string - out := make([]tplRow, len(d.Items)) + out := make([]Item, len(d.Items)) for i, x := range d.Items { if x.Type == gopher.INFO && x.Selector == "TITLE" { @@ -33,7 +30,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str continue } - tr := tplRow{ + tr := Item{ Text: x.Description, Type: x.Type.String(), } @@ -73,14 +70,14 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str return tpl.Execute(w, struct { Title string - Lines []tplRow + Lines []Item }{title, out}) } -// MakeGopherProxyHandler returns a Handler that proxies requests +// Handler returns a Handler that proxies requests // to the specified Gopher server as denoated by the first argument // to the request path and renders the content using the provided template. -func MakeGopherProxyHandler(tpl *template.Template, uri string) Handler { +func Handler(tpl *template.Template, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/") hostport := parts[0] @@ -147,6 +144,6 @@ func ListenAndServe(bind, uri string) error { log.Fatal(err) } - http.HandleFunc("/", MakeGopherProxyHandler(tpl, uri)) + http.HandleFunc("/", Handler(tpl, uri)) return http.ListenAndServe(bind, nil) } diff --git a/template.go b/template.go index 3b0fe2f..6d47d06 100644 --- a/template.go +++ b/template.go @@ -6,24 +6,6 @@ var tpltext = ` {{.Title}} -
-- cgit v1.2.3-54-g00ecf