update TODO & 404 handling
This commit is contained in:
@@ -4,6 +4,7 @@ import "embed"
|
||||
|
||||
var Core embed.FS
|
||||
var Port *int
|
||||
var Color *bool
|
||||
|
||||
type MyConfig struct {
|
||||
App struct {
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"github.com/fatih/color"
|
||||
|
||||
"fes/src/config"
|
||||
)
|
||||
@@ -157,6 +158,18 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error)
|
||||
return 1
|
||||
})
|
||||
|
||||
L.PreloadModule("core.util", func(L *lua.LState) int {
|
||||
data, err := config.Core.ReadFile("core/util.lua")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := L.DoString(string(data)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
L.Push(L.Get(-1))
|
||||
return 1
|
||||
})
|
||||
|
||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
||||
mod := L.NewTable()
|
||||
coreModules := []string{}
|
||||
@@ -263,22 +276,43 @@ func Start(dir string) error {
|
||||
return fmt.Errorf("failed to read www directory: %w", err)
|
||||
}
|
||||
|
||||
notFoundData := "404 Page Not Found"
|
||||
if _, err := os.Stat(filepath.Join(wwwDir, "404.lua")); err == nil {
|
||||
notFoundData, err = loadLua(dir, filepath.Join(wwwDir, "404.lua"), &cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if _, err := os.Stat(filepath.Join(wwwDir, "404.html")); err == nil {
|
||||
buf, err := os.ReadFile(filepath.Join(wwwDir, "404.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
notFoundData = string(buf)
|
||||
}
|
||||
|
||||
routes := make(map[string]string)
|
||||
handleDir(entries, wwwDir, routes, "")
|
||||
|
||||
for route, luaPath := range routes {
|
||||
func(rt string, lp string) {
|
||||
http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("-> %s\n", lp)
|
||||
data, err := loadLua(dir, lp, &cfg)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write([]byte(data))
|
||||
})
|
||||
}(route, luaPath)
|
||||
}
|
||||
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))
|
||||
color.Yellow("not found")
|
||||
return
|
||||
}
|
||||
fmt.Printf("> %s ", filepath.Base(lp))
|
||||
data, err := loadLua(dir, lp, &cfg)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
||||
color.Red("bad")
|
||||
return
|
||||
}
|
||||
color.Green("ok")
|
||||
w.Write([]byte(data))
|
||||
})
|
||||
|
||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
||||
return http.ListenAndServe(fmt.Sprintf(":%d", *config.Port), nil)
|
||||
|
||||
Reference in New Issue
Block a user