all repos — flounder @ e6d27a6a56bc9bd140c233643855288ca1aa6f42

A small site builder for the Gemini protocol

Add basic file hosting
alex wennerberg alex@alexwennerberg.com
Wed, 21 Oct 2020 22:41:46 -0700
commit

e6d27a6a56bc9bd140c233643855288ca1aa6f42

parent

7dff255849f753cf7b0d73de584f3bb6cb6f1645

4 files changed, 26 insertions(+), 8 deletions(-)

jump to
M README.mdREADME.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.
M gemini.gogemini.go

@@ -40,11 +40,14 @@ userName := strings.Split(r.URL.Host, ".")[0]

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

@@ -5,6 +5,8 @@ "fmt"

"html/template" "log" "net/http" + "path" + "strings" ) var t *template.Template

@@ -66,16 +68,25 @@ fmt.Fprintf(w, "%s\n", file.Name)

} } +// 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)) }
M main.gomain.go

@@ -71,7 +71,7 @@ c, err = getConfig(*configPath)

if err != nil { log.Fatal(err) } - // runHTTPServer() - runGeminiServer() + runHTTPServer() + // runGeminiServer() // go log.Fatal(gmi.ListenAndServe(":8080", nil)) }