all repos — flounder @ dd52cd247d7bc0f86bbb64c9fa33f4355d6f9b7b

A small site builder for the Gemini protocol

use servefile function from gemini lib
alex wennerberg alex@alexwennerberg.com
Fri, 30 Oct 2020 23:33:16 -0700
commit

dd52cd247d7bc0f86bbb64c9fa33f4355d6f9b7b

parent

a8a98dc62ceebfb9d27bc574e419066bb6f5cf16

2 files changed, 4 insertions(+), 20 deletions(-)

jump to
M gemini.gogemini.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 @@ t.Execute(w, data)

} 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() {
M http.gohttp.go

@@ -374,7 +374,7 @@ }

// 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"