aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2017-10-15 13:16:23 -0700
committerJames Mills <prologic@shortcircuit.net.au>2017-10-15 13:16:35 -0700
commit74783dccd7cbd6f7dba1c6894d5b68ec0d93131d (patch)
tree612ef4950e4018a8da42dc82b7dcfa1e97677048
parentUpdate README.md (diff)
downloadgopherproxy-74783dccd7cbd6f7dba1c6894d5b68ec0d93131d.tar.gz
gopherproxy-74783dccd7cbd6f7dba1c6894d5b68ec0d93131d.tar.bz2
gopherproxy-74783dccd7cbd6f7dba1c6894d5b68ec0d93131d.zip
Minor refactor/cleanup
-rw-r--r--cmd/gopherproxy/main.go2
-rw-r--r--gopherproxy.go17
-rw-r--r--template.go18
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 (
8) 8)
9 9
10var ( 10var (
11 bind = flag.String("bind", "0.0.0.0:80", "[int]:port to bind to") 11 bind = flag.String("bind", "0.0.0.0:8000", "[int]:port to bind to")
12 uri = flag.String("uri", "floodgap.com", "<host>:[port] to proxy to") 12 uri = flag.String("uri", "floodgap.com", "<host>:[port] to proxy to")
13) 13)
14 14
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
16type tplRow struct { 16type 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
23type Handler func(w http.ResponseWriter, req *http.Request)
24
25func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { 22func 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.
83func MakeGopherProxyHandler(tpl *template.Template, uri string) Handler { 80func 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}
diff --git a/template.go b/template.go
index 3b0fe2f..6d47d06 100644
--- a/template.go
+++ b/template.go
@@ -6,24 +6,6 @@ var tpltext = `<!doctype html>
6<meta charset="utf-8"> 6<meta charset="utf-8">
7<meta name="viewport" content="width=device-width, initial-scale=1" /> 7<meta name="viewport" content="width=device-width, initial-scale=1" />
8<title>{{.Title}}</title> 8<title>{{.Title}}</title>
9<style>
10 body {
11 background: #FFFFFF;
12 }
13
14 section {
15 margin: auto;
16 width: 50%;
17 font-family:Courier;
18 color: #CCCCCC;
19 background: #000000;
20 border: 3px double #CCCCCC;
21 color: #FFFFFF;
22 border-radius: 1em;
23 padding: 1em;
24 overflow-x: auto;
25 }
26</style>
27</head> 9</head>
28<body> 10<body>
29<section> 11<section>