This commit is contained in:
2025-12-03 21:50:51 -05:00
parent 2f877b63b8
commit 7747d415cc
6 changed files with 45 additions and 27 deletions

View File

@@ -24,10 +24,10 @@ type reqData struct {
params map[string]string
}
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base string) error {
for _, entry := range entries {
if entry.IsDir() {
sub := filepath.Join("www", entry.Name())
sub := filepath.Join(dir, entry.Name())
subs, err := os.ReadDir(sub)
if err != nil {
return fmt.Errorf("failed to read %s: %w", sub, err)
@@ -45,7 +45,7 @@ func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, b
}
if strings.HasSuffix(entry.Name(), ".lua") {
name := strings.TrimSuffix(entry.Name(), ".lua")
path := filepath.Join("www", entry.Name())
path := filepath.Join(dir, entry.Name())
if name == "index" {
if base == "" {
routes["/"] = path
@@ -61,6 +61,14 @@ func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, b
routes[base+"/"+name] = path
}
}
} else {
name := entry.Name()
path := filepath.Join(dir, entry.Name())
if base == "" {
routes["/"+name] = path
} else {
routes[base+"/"+name] = path
}
}
}
return nil
@@ -262,10 +270,6 @@ func Start(dir string) error {
return fmt.Errorf("failed to parse Fes.toml: %w", err)
}
entries, err := os.ReadDir("www")
if err != nil {
return fmt.Errorf("failed to read www directory: %w", err)
}
notFoundData := `
<html>
@@ -290,15 +294,25 @@ func Start(dir string) error {
}
routes := make(map[string]string)
entries, err := os.ReadDir("www")
if err != nil {
return fmt.Errorf("failed to read www directory: %w", err)
}
handleDir(entries, "www", routes, "")
entries, err = os.ReadDir("static")
if err == nil {
handleDir(entries, "static", routes, "")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
lp, ok := routes[path]
if !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(notFoundData))
fmt.Printf("> %s.lua ", filepath.Base(path))
fmt.Printf("> %s ", path)
color.Yellow("not found")
return
}