aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2016-09-23 00:02:59 +1000
committerJames Mills <prologic@shortcircuit.net.au>2016-09-23 00:02:59 +1000
commit0105bbb0d56f4ca845df1b73cd2a0b8e94a968ae (patch)
treee5b81a0844d4b6f85467d4e3747ca095cffb3f6f
parentAdded templating support (diff)
downloadgopherproxy-0105bbb0d56f4ca845df1b73cd2a0b8e94a968ae.tar.gz
gopherproxy-0105bbb0d56f4ca845df1b73cd2a0b8e94a968ae.tar.bz2
gopherproxy-0105bbb0d56f4ca845df1b73cd2a0b8e94a968ae.zip
Add query support
-rw-r--r--main.go20
-rw-r--r--template.go14
2 files changed, 28 insertions, 6 deletions
diff --git a/main.go b/main.go
index 6815b0b..32ae98c 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
8 "io/ioutil" 8 "io/ioutil"
9 "log" 9 "log"
10 "net/http" 10 "net/http"
11 "net/url"
11 "strings" 12 "strings"
12 13
13 "github.com/prologic/go-gopher" 14 "github.com/prologic/go-gopher"
@@ -17,6 +18,8 @@ var (
17 bind = flag.String("bind", ":80", "[int]:port to bind to") 18 bind = flag.String("bind", ":80", "[int]:port to bind to")
18 host = flag.String("host", "localhost", "host to proxy to") 19 host = flag.String("host", "localhost", "host to proxy to")
19 port = flag.Int("port", 70, "port to proxy to") 20 port = flag.Int("port", 70, "port to proxy to")
21
22 tpl *template.Template
20) 23)
21 24
22type tplRow struct { 25type tplRow struct {
@@ -44,7 +47,9 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
44 } else { 47 } else {
45 tr.Link = template.URL( 48 tr.Link = template.URL(
46 fmt.Sprintf( 49 fmt.Sprintf(
47 "%s%s", string(byte(x.Type)), x.Selector, 50 "/%s%s",
51 string(byte(x.Type)),
52 url.QueryEscape(x.Selector),
48 ), 53 ),
49 ) 54 )
50 } 55 }
@@ -55,13 +60,20 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
55 return tpl.Execute(w, struct { 60 return tpl.Execute(w, struct {
56 Title string 61 Title string
57 Lines []tplRow 62 Lines []tplRow
58 }{"XXX", out}) 63 }{*host, out})
59} 64}
60 65
61func proxy(w http.ResponseWriter, req *http.Request) { 66func proxy(w http.ResponseWriter, req *http.Request) {
62 path := strings.TrimPrefix(req.URL.Path, "/") 67 path := strings.TrimPrefix(req.URL.Path, "/")
63 68
64 res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) 69 res, err := gopher.Get(
70 fmt.Sprintf(
71 "gopher://%s:%d/%s",
72 *host,
73 *port,
74 url.QueryEscape(path),
75 ),
76 )
65 if err != nil { 77 if err != nil {
66 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) 78 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
67 return 79 return
@@ -77,8 +89,6 @@ func proxy(w http.ResponseWriter, req *http.Request) {
77 } 89 }
78} 90}
79 91
80var tpl *template.Template
81
82func main() { 92func main() {
83 flag.Parse() 93 flag.Parse()
84 94
diff --git a/template.go b/template.go
index 7c13451..c0a79a4 100644
--- a/template.go
+++ b/template.go
@@ -8,7 +8,19 @@ var tpltext = `<!doctype html>
8</head> 8</head>
9<body> 9<body>
10<pre> 10<pre>
11{{range .Lines}} {{if .Link}}({{.Type}}) <a href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}} 11{{range .Lines}} {{if .Link}}({{.Type}}) <a class="{{ .Type }}" href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}}
12{{end}}</pre> 12{{end}}</pre>
13<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>
14<script type="text/javascript">
15$(document).ready(function () {
16 $(".QRY").click(function (e) {
17 e.preventDefault();
18 var query = prompt("Please enter required input: ", "");
19 if (query != null) {
20 window.location = e.target.href + "%09" + query;
21 }
22 });
23});
24</script>
13</body> 25</body>
14</html>` 26</html>`