@@ 85,54 85,65 @@ func startWebServer(conf *cfg.Config) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/gemini/"):
- geminiURL := r.URL.Path[8:len(r.URL.Path)]
-
+ geminiURL := r.URL.String()[8:len(r.URL.String())]
client := gemini.Client{}
ctx := context.Background()
resp, err := client.Get(ctx, "gemini://" + geminiURL)
if err != nil {
- w.Write([]byte("<h1>Host not found :(</h1>"))
+ if _, err = w.Write([]byte("<h1>Host not found :(</h1>")); err != nil {
+ return
+ }
break
}
switch resp.Status {
case gemini.StatusRedirect:
case gemini.StatusPermanentRedirect:
w.Header().Set("Content-Type", "text/html; charset=utf-8")
- w.Write([]byte("<style>" + STYLE + "</style>" + gmi2html.Convert("=> " + resp.Meta)))
+ if _, err := w.Write([]byte("<style>" + STYLE + "</style>" + gmi2html.Convert("=> " + resp.Meta))); err != nil {
+ return
+ }
break
case gemini.StatusNotFound:
- w.Write([]byte("<h1>Page not found</h1>"))
+ if _, err := w.Write([]byte("<h1>Page not found</h1>")); err != nil {
+ return
+ }
break
case gemini.StatusSuccess:
if strings.HasPrefix(resp.Meta, "text") {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if b, err := io.ReadAll(resp.Body); err == nil {
- path := strings.Split(r.URL.Path, "/")
+ p := strings.Split(r.URL.Path, "/")
var up string
- if len(path) > 0 {
- if path[0] == "/" {
- path = path[1:]
+ if len(p) > 0 {
+ if p[0] == "/" {
+ p = p[1:]
}
- if path[len(path)-1] == "" {
- path = path[:len(path)-1]
+ if p[len(p)-1] == "" {
+ p = p[:len(p)-1]
}
- if len(path) > 3 {
- up = "<p><a href=\""+strings.Join(path[:len(path)-1], "/")+"/\">Go up</a></p>"
+ if len(p) > 3 {
+ up = "<p><a href=\""+strings.Join(p[:len(p)-1], "/")+"/\">Go up</a></p>"
}
}
- w.Write([]byte("<style>" + STYLE + "</style>" + up + gmi2html.Convert(string(b))))
- return
+ if _, err = w.Write([]byte("<style>" + STYLE + "</style>" + up + gmi2html.Convert(string(b)))); err != nil {
+ return
+ }
}
} else {
w.Header().Set("Content-Type", resp.Meta)
- if b, err := io.ReadAll(resp.Body); err == nil {
- w.Write(b)
+ b, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return
+ }
+ if _, err = w.Write(b); err != nil {
return
}
}
break
default:
- w.Write([]byte("<h1>Uhh error :(</h1>"))
+ if _, err = w.Write([]byte("<h1>Uhh error :(</h1>")); err != nil {
+ return
+ }
}
break
default: