cd into project dir

This commit is contained in:
2025-12-02 21:44:14 -05:00
parent 155f3d3ef0
commit 095331b5a9

View File

@@ -27,7 +27,7 @@ type reqData struct {
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error { func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if entry.IsDir() {
sub := filepath.Join(wwwDir, entry.Name()) sub := filepath.Join("www", entry.Name())
subs, err := os.ReadDir(sub) subs, err := os.ReadDir(sub)
if err != nil { if err != nil {
return fmt.Errorf("failed to read %s: %w", sub, err) 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") { if strings.HasSuffix(entry.Name(), ".lua") {
name := strings.TrimSuffix(entry.Name(), ".lua") name := strings.TrimSuffix(entry.Name(), ".lua")
path := filepath.Join(wwwDir, entry.Name()) path := filepath.Join("www", entry.Name())
if name == "index" { if name == "index" {
if base == "" { if base == "" {
routes["/"] = path routes["/"] = path
@@ -248,7 +248,10 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
} }
func Start(dir string) error { func Start(dir string) error {
tomlDocument, err := os.ReadFile(filepath.Join(dir, "Fes.toml")) os.Chdir(dir)
dir = "."
tomlDocument, err := os.ReadFile("Fes.toml")
if err != nil { if err != nil {
return err return err
} }
@@ -259,8 +262,7 @@ func Start(dir string) error {
return fmt.Errorf("failed to parse Fes.toml: %w", err) return fmt.Errorf("failed to parse Fes.toml: %w", err)
} }
wwwDir := filepath.Join(dir, "www") entries, err := os.ReadDir("www")
entries, err := os.ReadDir(wwwDir)
if err != nil { if err != nil {
return fmt.Errorf("failed to read www directory: %w", err) return fmt.Errorf("failed to read www directory: %w", err)
} }
@@ -274,13 +276,13 @@ func Start(dir string) error {
</body> </body>
</html> </html>
` `
if _, err := os.Stat(filepath.Join(wwwDir, "404.lua")); err == nil { if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
notFoundData, err = loadLua(dir, filepath.Join(wwwDir, "404.lua"), &cfg, reqData{}) notFoundData, err = loadLua(dir, "www/404.lua", &cfg, reqData{})
if err != nil { if err != nil {
panic(err) panic(err)
} }
} else if _, err := os.Stat(filepath.Join(wwwDir, "404.html")); err == nil { } else if _, err := os.Stat("www/404.html"); err == nil {
buf, err := os.ReadFile(filepath.Join(wwwDir, "404.html")) buf, err := os.ReadFile("www/404.html")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -288,7 +290,7 @@ func Start(dir string) error {
} }
routes := make(map[string]string) routes := make(map[string]string)
handleDir(entries, wwwDir, routes, "") handleDir(entries, "www", routes, "")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path