refactor Start
This commit is contained in:
@@ -351,25 +351,7 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
|||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(dir string) error {
|
func generateNotFoundData(cfg *config.MyConfig) []byte {
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
notFoundData := []byte(`
|
notFoundData := []byte(`
|
||||||
<html>
|
<html>
|
||||||
<head><title>404 Not Found</title></head>
|
<head><title>404 Not Found</title></head>
|
||||||
@@ -380,7 +362,7 @@ func Start(dir string) error {
|
|||||||
</html>
|
</html>
|
||||||
`)
|
`)
|
||||||
if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
|
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
|
notFoundData = nf
|
||||||
}
|
}
|
||||||
} else if _, err := os.Stat("www/404.html"); err == nil {
|
} else if _, err := os.Stat("www/404.html"); err == nil {
|
||||||
@@ -388,8 +370,12 @@ func Start(dir string) error {
|
|||||||
notFoundData = buf
|
notFoundData = buf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return notFoundData
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadDirs() map[string]string {
|
||||||
routes := make(map[string]string)
|
routes := make(map[string]string)
|
||||||
|
|
||||||
if entries, err := os.ReadDir("www"); err == nil {
|
if entries, err := os.ReadDir("www"); err == nil {
|
||||||
if err := handleDir(entries, "www", routes, "", false); err != nil {
|
if err := handleDir(entries, "www", routes, "", false); err != nil {
|
||||||
ui.Warning("failed to handle www directory", err)
|
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) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
route, ok := routes[r.URL.Path]
|
route, ok := routes[r.URL.Path]
|
||||||
|
|
||||||
var err error = nil
|
var err error = nil
|
||||||
|
|
||||||
|
/* defer won't update paramaters unless we do this. */
|
||||||
defer func() {
|
defer func() {
|
||||||
ui.Path(route, err)
|
ui.Path(route, err)
|
||||||
}()
|
}()
|
||||||
@@ -421,18 +447,12 @@ func Start(dir string) error {
|
|||||||
route = r.URL.Path
|
route = r.URL.Path
|
||||||
|
|
||||||
if strings.HasPrefix(route, "/archive") {
|
if strings.HasPrefix(route, "/archive") {
|
||||||
fsPath := "." + route
|
readArchive(w, route)
|
||||||
if info, err := os.Stat(fsPath); err == nil && info.IsDir() {
|
|
||||||
if page, err := generateArchiveIndex(fsPath, route); err == nil {
|
|
||||||
w.Write([]byte(page))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Write([]byte(notFoundData))
|
w.Write([]byte(notFoundData))
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
params := make(map[string]string)
|
params := make(map[string]string)
|
||||||
|
|||||||
Reference in New Issue
Block a user