flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit dd52cd247d7bc0f86bbb64c9fa33f4355d6f9b7b
parent a8a98dc62ceebfb9d27bc574e419066bb6f5cf16
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri, 30 Oct 2020 23:33:16 -0700

use servefile function from gemini lib

Diffstat:
Mgemini.go | 22+++-------------------
Mhttp.go | 2+-
2 files changed, 4 insertions(+), 20 deletions(-)

diff --git a/gemini.go b/gemini.go @@ -2,11 +2,9 @@ package main import ( "crypto/tls" - "mime" "strings" // "fmt" gmi "git.sr.ht/~adnano/go-gemini" - "io/ioutil" "log" "path" "path/filepath" @@ -41,28 +39,14 @@ func gmiIndex(w *gmi.ResponseWriter, r *gmi.Request) { } func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { - userName := strings.Split(r.URL.Host, ".")[0] + userName := filepath.Clean(strings.Split(r.URL.Host, ".")[0]) // clean probably unnecessary fileName := filepath.Clean(r.URL.Path) if fileName == "/" { fileName = "index.gmi" } - filePath := path.Join(c.FilesDirectory, userName, fileName) - log.Println("Request for gemini file at", filePath) - data, err := ioutil.ReadFile(filePath) + log.Println("Request for gemini file", fileName, "for user", userName) - if err != nil { - w.WriteHeader(51, "Not Found") - return - } - ext := filepath.Ext(filePath) - mimetype := mime.TypeByExtension(ext) - w.SetMimetype(mimetype) - _, err = w.Write(data) - if err != nil { - log.Println(err) - w.WriteHeader(40, "Internal server error") - return - } + gmi.ServeFile(w, gmi.Dir(path.Join(c.FilesDirectory, userName)), fileName) } func runGeminiServer() { diff --git a/http.go b/http.go @@ -374,7 +374,7 @@ func adminHandler(w http.ResponseWriter, r *http.Request) { // Server a user's file func userFile(w http.ResponseWriter, r *http.Request) { - userName := strings.Split(r.Host, ".")[0] + userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // clean probably unnecessary p := filepath.Clean(r.URL.Path) if p == "/" { p = "index.gmi"