alpha p4
This commit is contained in:
@@ -18,6 +18,43 @@ import (
|
||||
"fes/src/config"
|
||||
)
|
||||
|
||||
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
sub := filepath.Join(wwwDir, entry.Name())
|
||||
subs, err := os.ReadDir(sub)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read %s: %w", sub, err)
|
||||
}
|
||||
next := base + "/" + entry.Name()
|
||||
if err := handleDir(subs, sub, routes, next); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(entry.Name(), ".lua") {
|
||||
name := strings.TrimSuffix(entry.Name(), ".lua")
|
||||
path := filepath.Join(wwwDir, entry.Name())
|
||||
if name == "index" {
|
||||
if base == "" {
|
||||
routes["/"] = path
|
||||
routes["/index"] = path
|
||||
} else {
|
||||
routes[base] = path
|
||||
routes[base+"/index"] = path
|
||||
}
|
||||
} else {
|
||||
if base == "" {
|
||||
routes["/"+name] = path
|
||||
} else {
|
||||
routes[base+"/"+name] = path
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fixMalformedToml(content string) string {
|
||||
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
|
||||
return re.ReplaceAllStringFunc(content, func(match string) string {
|
||||
@@ -209,11 +246,11 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error)
|
||||
}
|
||||
|
||||
func Start(dir string) error {
|
||||
doc, err := os.ReadFile(filepath.Join(dir, "Fes.toml"))
|
||||
tomlDocument, err := os.ReadFile(filepath.Join(dir, "Fes.toml"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docStr := fixMalformedToml(string(doc))
|
||||
docStr := fixMalformedToml(string(tomlDocument))
|
||||
var cfg config.MyConfig
|
||||
err = toml.Unmarshal([]byte(docStr), &cfg)
|
||||
if err != nil {
|
||||
@@ -227,18 +264,7 @@ func Start(dir string) error {
|
||||
}
|
||||
|
||||
routes := make(map[string]string)
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lua") {
|
||||
baseName := strings.TrimSuffix(entry.Name(), ".lua")
|
||||
luaPath := filepath.Join(wwwDir, entry.Name())
|
||||
if baseName == "index" {
|
||||
routes["/"] = luaPath
|
||||
routes["/index"] = luaPath
|
||||
} else {
|
||||
routes["/"+baseName] = luaPath
|
||||
}
|
||||
}
|
||||
}
|
||||
handleDir(entries, wwwDir, routes, "")
|
||||
|
||||
for route, luaPath := range routes {
|
||||
func(rt string, lp string) {
|
||||
|
||||
Reference in New Issue
Block a user