Add basic file hosting
alex wennerberg alex@alexwennerberg.com
Wed, 21 Oct 2020 22:41:46 -0700
M
gemini.go
→
gemini.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.go
→
http.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)) }