diff --git a/src/server/server.go b/src/server/server.go index 97823cc..bfa8cbb 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -351,25 +351,7 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) { return b.String(), nil } -func Start(dir string) error { - if err := os.Chdir(dir); err != nil { - return ui.Error(fmt.Sprintf("failed to change directory to %s", dir), err) - } - dir = "." - - tomlDocument, err := os.ReadFile("Fes.toml") - if err != nil { - return ui.Error("failed to read Fes.toml", err) - } - docStr := fixMalformedToml(string(tomlDocument)) - var cfg config.MyConfig - if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil { - ui.Warning("failed to parse Fes.toml", err) - cfg.App.Authors = []string{"unknown"} - cfg.App.Name = "unknown" - cfg.App.Version = "unknown" - } - +func generateNotFoundData(cfg *config.MyConfig) []byte { notFoundData := []byte(` 404 Not Found @@ -380,7 +362,7 @@ func Start(dir string) error { `) if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil { - if nf, err := loadLua("www/404.lua", &cfg, reqData{}); err == nil { + if nf, err := loadLua("www/404.lua", cfg, reqData{}); err == nil { notFoundData = nf } } else if _, err := os.Stat("www/404.html"); err == nil { @@ -388,8 +370,12 @@ func Start(dir string) error { notFoundData = buf } } + return notFoundData +} +func loadDirs() map[string]string { routes := make(map[string]string) + if entries, err := os.ReadDir("www"); err == nil { if err := handleDir(entries, "www", routes, "", false); err != nil { ui.Warning("failed to handle www directory", err) @@ -408,10 +394,50 @@ func Start(dir string) error { } } + return routes +} + +func parseConfig() config.MyConfig { + tomlDocument, err := os.ReadFile("Fes.toml") + if err != nil { + ui.Error("failed to read Fes.toml", err) + os.Exit(1) + } + docStr := fixMalformedToml(string(tomlDocument)) + var cfg config.MyConfig + if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil { + ui.Warning("failed to parse Fes.toml", err) + cfg.App.Authors = []string{"unknown"} + cfg.App.Name = "unknown" + cfg.App.Version = "unknown" + } + return cfg +} + +func readArchive(w http.ResponseWriter, route string) { + fsPath := "." + route + if info, err := os.Stat(fsPath); err == nil && info.IsDir() { + if page, err := generateArchiveIndex(fsPath, route); err == nil { + w.Write([]byte(page)) + } + } +} + +func Start(dir string) error { + if err := os.Chdir(dir); err != nil { + return ui.Error(fmt.Sprintf("failed to change directory to %s", dir), err) + } + + cfg := parseConfig() + notFoundData := generateNotFoundData(&cfg) + routes := loadDirs() + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { route, ok := routes[r.URL.Path] var err error = nil + + /* defer won't update paramaters unless we do this. */ defer func() { ui.Path(route, err) }() @@ -421,18 +447,12 @@ func Start(dir string) error { route = r.URL.Path if strings.HasPrefix(route, "/archive") { - fsPath := "." + route - if info, err := os.Stat(fsPath); err == nil && info.IsDir() { - if page, err := generateArchiveIndex(fsPath, route); err == nil { - w.Write([]byte(page)) - return - } - } + readArchive(w, route) } else { w.WriteHeader(http.StatusNotFound) w.Write([]byte(notFoundData)) - return } + return } params := make(map[string]string)