aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2016-09-22 11:22:10 +1000
committerJames Mills <prologic@shortcircuit.net.au>2016-09-22 11:22:10 +1000
commit773606bd45e283d05a4aaa06787289ad322ca7b7 (patch)
treef82287ca30e93aa5b30c8cc258e01b0ce851891a /main.go
downloadgopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.tar.gz
gopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.tar.bz2
gopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.zip
Initial Commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..be625c6
--- /dev/null
+++ b/main.go
@@ -0,0 +1,47 @@
1package main
2
3import (
4 "flag"
5 "fmt"
6 "io"
7 "log"
8 "net/http"
9 "strings"
10
11 "github.com/prologic/go-gopher"
12)
13
14var (
15 bind = flag.String("bind", ":80", "[int]:port to bind to")
16 host = flag.String("host", "localhost", "host to proxy to")
17 port = flag.Int("port", 70, "port to proxy to")
18)
19
20func proxy(res http.ResponseWriter, req *http.Request) {
21 path := strings.TrimPrefix(req.URL.Path, "/")
22
23 gr, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path))
24 if err != nil {
25 io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
26 return
27 }
28
29 if gr.Body != nil {
30 io.Copy(res, gr.Body)
31 } else {
32 bytes, err := gr.Dir.ToText()
33 if err != nil {
34 io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
35 return
36 }
37
38 io.WriteString(res, string(bytes))
39 }
40}
41
42func main() {
43 flag.Parse()
44
45 http.HandleFunc("/", proxy)
46 log.Fatal(http.ListenAndServe(*bind, nil))
47}