flounder

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

commit e6d27a6a56bc9bd140c233643855288ca1aa6f42
parent 7dff255849f753cf7b0d73de584f3bb6cb6f1645
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed, 21 Oct 2020 22:41:46 -0700

Add basic file hosting

Diffstat:
MREADME.md | 4++++
Mgemini.go | 9++++++---
Mhttp.go | 17++++++++++++++---
Mmain.go | 4++--
4 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md @@ -3,3 +3,7 @@ A lightweight server to help users build simple Gemini sites over http(s) Designed to help make the Gemini ecosystem more accessible. + +## Hosting + +Very simple to host -- a single binary with a gemini server, http server included. diff --git a/gemini.go b/gemini.go @@ -40,11 +40,14 @@ func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { fileName := path.Join(c.FilesDirectory, userName, r.URL.Path) data, err := ioutil.ReadFile(fileName) if err != nil { - // return 404 equivalent + // TODO return 404 equivalent + log.Fatal(err) + } + _, err = w.Write(data) + if err != nil { + // return internal server error log.Fatal(err) } - // TODO handle error - w.Write(data) } func runGeminiServer() { diff --git a/http.go b/http.go @@ -5,6 +5,8 @@ import ( "html/template" "log" "net/http" + "path" + "strings" ) var t *template.Template @@ -66,16 +68,25 @@ func mySiteHandler(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] + fileName := path.Join(c.FilesDirectory, userName, r.URL.Path) + http.ServeFile(w, r, fileName) +} + func runHTTPServer() { + log.Println("Running http server") var err error t, err = template.ParseGlob("./templates/*.html") // TODO make template dir configruable if err != nil { log.Fatal(err) } - http.HandleFunc("/", indexHandler) - http.HandleFunc("/my_site", mySiteHandler) - http.HandleFunc("/edit/", editFileHandler) + http.HandleFunc(c.RootDomain+"/", indexHandler) + http.HandleFunc(c.RootDomain+"/my_site", mySiteHandler) + http.HandleFunc(c.RootDomain+"/edit/", editFileHandler) // http.HandleFunc("/delete/", deleteFileHandler) // login+register functions + http.HandleFunc("/", userFile) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/main.go b/main.go @@ -71,7 +71,7 @@ func main() { if err != nil { log.Fatal(err) } - // runHTTPServer() - runGeminiServer() + runHTTPServer() + // runGeminiServer() // go log.Fatal(gmi.ListenAndServe(":8080", nil)) }