Added templating support
This commit is contained in:
parent
773606bd45
commit
669492455b
70
main.go
70
main.go
|
@ -3,7 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -17,31 +19,79 @@ var (
|
||||||
port = flag.Int("port", 70, "port to proxy to")
|
port = flag.Int("port", 70, "port to proxy to")
|
||||||
)
|
)
|
||||||
|
|
||||||
func proxy(res http.ResponseWriter, req *http.Request) {
|
type tplRow struct {
|
||||||
|
Link template.URL
|
||||||
|
Type string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Directory) error {
|
||||||
|
out := make([]tplRow, len(d))
|
||||||
|
|
||||||
|
for i, x := range d {
|
||||||
|
tr := tplRow{
|
||||||
|
Text: x.Description,
|
||||||
|
Type: x.Type.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if x.Type == gopher.INFO {
|
||||||
|
out[i] = tr
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(x.Selector, "URL:") {
|
||||||
|
tr.Link = template.URL(x.Selector[4:])
|
||||||
|
} else {
|
||||||
|
tr.Link = template.URL(
|
||||||
|
fmt.Sprintf(
|
||||||
|
"%s%s", string(byte(x.Type)), x.Selector,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
out[i] = tr
|
||||||
|
}
|
||||||
|
|
||||||
|
return tpl.Execute(w, struct {
|
||||||
|
Title string
|
||||||
|
Lines []tplRow
|
||||||
|
}{"XXX", out})
|
||||||
|
}
|
||||||
|
|
||||||
|
func proxy(w http.ResponseWriter, req *http.Request) {
|
||||||
path := strings.TrimPrefix(req.URL.Path, "/")
|
path := strings.TrimPrefix(req.URL.Path, "/")
|
||||||
|
|
||||||
gr, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path))
|
res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
|
io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if gr.Body != nil {
|
if res.Body != nil {
|
||||||
io.Copy(res, gr.Body)
|
io.Copy(w, res.Body)
|
||||||
} else {
|
} else {
|
||||||
bytes, err := gr.Dir.ToText()
|
if err := renderDirectory(w, tpl, res.Dir); err != nil {
|
||||||
if err != nil {
|
io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
|
||||||
io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
io.WriteString(res, string(bytes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tpl *template.Template
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
tpldata, err := ioutil.ReadFile(".template")
|
||||||
|
if err == nil {
|
||||||
|
tpltext = string(tpldata)
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl, err = template.New("gophermenu").Parse(tpltext)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", proxy)
|
http.HandleFunc("/", proxy)
|
||||||
log.Fatal(http.ListenAndServe(*bind, nil))
|
log.Fatal(http.ListenAndServe(*bind, nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
var tpltext = `<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>{{.Title}}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre>
|
||||||
|
{{range .Lines}} {{if .Link}}({{.Type}}) <a href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}}
|
||||||
|
{{end}}</pre>
|
||||||
|
</body>
|
||||||
|
</html>`
|
Loading…
Reference in New Issue