Add query support

This commit is contained in:
James Mills 2016-09-23 00:02:59 +10:00
parent 669492455b
commit 0105bbb0d5
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 28 additions and 6 deletions

20
main.go
View File

@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/prologic/go-gopher"
@ -17,6 +18,8 @@ var (
bind = flag.String("bind", ":80", "[int]:port to bind to")
host = flag.String("host", "localhost", "host to proxy to")
port = flag.Int("port", 70, "port to proxy to")
tpl *template.Template
)
type tplRow struct {
@ -44,7 +47,9 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
} else {
tr.Link = template.URL(
fmt.Sprintf(
"%s%s", string(byte(x.Type)), x.Selector,
"/%s%s",
string(byte(x.Type)),
url.QueryEscape(x.Selector),
),
)
}
@ -55,13 +60,20 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
return tpl.Execute(w, struct {
Title string
Lines []tplRow
}{"XXX", out})
}{*host, out})
}
func proxy(w http.ResponseWriter, req *http.Request) {
path := strings.TrimPrefix(req.URL.Path, "/")
res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path))
res, err := gopher.Get(
fmt.Sprintf(
"gopher://%s:%d/%s",
*host,
*port,
url.QueryEscape(path),
),
)
if err != nil {
io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
return
@ -77,8 +89,6 @@ func proxy(w http.ResponseWriter, req *http.Request) {
}
}
var tpl *template.Template
func main() {
flag.Parse()

View File

@ -8,7 +8,19 @@ var tpltext = `<!doctype html>
</head>
<body>
<pre>
{{range .Lines}} {{if .Link}}({{.Type}}) <a href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}}
{{range .Lines}} {{if .Link}}({{.Type}}) <a class="{{ .Type }}" href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}}
{{end}}</pre>
<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".QRY").click(function (e) {
e.preventDefault();
var query = prompt("Please enter required input: ", "");
if (query != null) {
window.location = e.target.href + "%09" + query;
}
});
});
</script>
</body>
</html>`