Fix links in proxy
alex wennerberg alex@alexwennerberg.com
Sun, 10 Jan 2021 00:29:33 -0800
2 files changed,
21 insertions(+),
7 deletions(-)
M
proxy.go
→
proxy.go
@@ -8,6 +8,7 @@ "io"
"mime" "net/http" "net/url" + "path" "strings" "time"@@ -20,14 +21,20 @@ w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("404 Not found")) return } - path := strings.SplitN(r.URL.Path, "/", 3) + var spath []string + if r.URL.Path == "/" { + http.Redirect(w, r, "gemini.circumlunar.space", http.StatusSeeOther) + return + } else { + spath = strings.SplitN(r.URL.Path, "/", 3) + } req := gemini.Request{} var err error - req.Host = path[1] - if len(path) > 2 { - req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/%s", path[1], path[2])) + req.Host = spath[1] + if len(spath) > 2 { + req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/%s", spath[1], spath[2])) } else { - req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/", path[1])) + req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/", spath[1])) } client := gemini.Client{ Timeout: 60 * time.Second,@@ -104,14 +111,16 @@ return
} w.Header().Add("Content-Type", "text/html") - htmlString := textToHTML(req.URL, gemini.ParseText(resp.Body)) + if strings.HasSuffix(r.URL.Path, "/") { + r.URL.Path = path.Dir(r.URL.Path) + } data := struct { SiteBody template.HTML Favicon string PageTitle string URI *url.URL - }{template.HTML(htmlString), "", req.URL.String(), req.URL} + }{template.HTML(htmlString), "", r.URL.String(), r.URL} err = t.ExecuteTemplate(w, "user_page.html", data) if err != nil {
M
templates/user_page.html
→
templates/user_page.html
@@ -11,8 +11,13 @@ <body>
<main> {{.SiteBody}} <br> +{{$parent := parent .URI.Path}} {{ if ne .URI.Path "/"}} +{{ if ne $parent "/"}} +<a href="{{parent .URI.Path}}/" title="Go to the parent directory">{{parent .URI.Path}}/</a> +{{ else }} <a href="{{parent .URI.Path}}" title="Go to the parent directory">{{parent .URI.Path}}</a> +{{ end }} <br> <br> {{ end }}