change config to static config
alex wennerberg alex@alexwennerberg.com
Tue, 20 Oct 2020 22:57:42 -0700
M
http.go
→
http.go
@@ -9,11 +9,6 @@ )
var t *template.Template -type IndexHandler struct { - Domain string - SiteTitle string -} - // TODO somewhat better error handling const InternalServerErrorMsg = "500: Internal Server Error"@@ -25,7 +20,7 @@ http.Error(w, errorMsg, statusCode)
} } -func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func indexHandler(w http.ResponseWriter, r *http.Request) { indexFiles, err := getIndexFiles() if err != nil { log.Println(err)@@ -43,7 +38,7 @@ Domain string
PageTitle string Files []*File Users []string - }{h.Domain, h.SiteTitle, indexFiles, allUsers} + }{c.RootDomain, c.SiteTitle, indexFiles, allUsers} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { log.Println(err)@@ -71,13 +66,13 @@ fmt.Fprintf(w, "%s\n", file.Name)
} } -func runHTTPServer(config *Config) { +func runHTTPServer() { var err error t, err = template.ParseGlob("./templates/*.html") // TODO make template dir configruable if err != nil { log.Fatal(err) } - http.Handle("/", &IndexHandler{config.RootDomain, config.SiteTitle}) + http.HandleFunc("/", indexHandler) http.HandleFunc("/my_site", mySiteHandler) http.HandleFunc("/edit/", editFileHandler) // http.HandleFunc("/delete/", deleteFileHandler)
M
main.go
→
main.go
@@ -9,6 +9,8 @@ "path"
"path/filepath" ) +var c Config // global var to hold static configuration + const ( // todo make configurable userFilesPath = "./files" )@@ -64,11 +66,12 @@ }
func main() { configPath := flag.String("c", "flounder.toml", "path to config file") - config, err := getConfig(*configPath) + var err error + c, err = getConfig(*configPath) if err != nil { log.Fatal(err) } - runHTTPServer(&config) + runHTTPServer() // runGeminiServer(&config) // go log.Fatal(gmi.ListenAndServe(":8080", nil)) }