bus
This commit is contained in:
1
TODO
1
TODO
@@ -1,3 +1,2 @@
|
||||
Add an interval element
|
||||
Add a way to pass data to sites on load via fes.bus: pass a table of { url, IP, request, headers, body } :: fix current impl
|
||||
Serve site/static at /static
|
||||
|
||||
@@ -6,4 +6,10 @@ site.copyright = fes.util.copyright("https://git.vxserver.dev/fSD/", "fSD")
|
||||
|
||||
site:h1("URL: " .. fes.bus.url)
|
||||
|
||||
local params = fes.bus.params
|
||||
|
||||
for key, val in pairs(params) do
|
||||
site:h2(key .. ": " .. val)
|
||||
end
|
||||
|
||||
return site
|
||||
|
||||
@@ -20,9 +20,8 @@ import (
|
||||
)
|
||||
|
||||
type reqData struct {
|
||||
url string
|
||||
ip string
|
||||
req int
|
||||
path string
|
||||
params map[string]string
|
||||
}
|
||||
|
||||
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
|
||||
@@ -120,88 +119,63 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
||||
return app
|
||||
}
|
||||
|
||||
func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (string, error) {
|
||||
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
rdents, err := fs.ReadDir(config.Core, "core")
|
||||
coreFiles, err := fs.ReadDir(config.Core, "core")
|
||||
if err == nil {
|
||||
for _, de := range rdents {
|
||||
if de.IsDir() {
|
||||
for _, de := range coreFiles {
|
||||
if de.IsDir() || !strings.HasSuffix(de.Name(), ".lua") {
|
||||
continue
|
||||
}
|
||||
name := de.Name()
|
||||
if !strings.HasSuffix(name, ".lua") {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join("core", name)
|
||||
data, err := config.Core.ReadFile(path)
|
||||
path := filepath.Join("core", de.Name())
|
||||
fileData, err := config.Core.ReadFile(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if err := L.DoString(string(data)); err != nil {
|
||||
continue
|
||||
}
|
||||
L.DoString(string(fileData))
|
||||
}
|
||||
}
|
||||
|
||||
L.PreloadModule("core.std", func(L *lua.LState) int {
|
||||
data, err := config.Core.ReadFile("core/std.lua")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := L.DoString(string(data)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
L.Push(L.Get(-1))
|
||||
return 1
|
||||
})
|
||||
preloadLuaModule := func(name, path string) {
|
||||
L.PreloadModule(name, func(L *lua.LState) int {
|
||||
fileData, err := config.Core.ReadFile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := L.DoString(string(fileData)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
L.Push(L.Get(-1))
|
||||
return 1
|
||||
})
|
||||
}
|
||||
|
||||
L.PreloadModule("core.symbol", func(L *lua.LState) int {
|
||||
data, err := config.Core.ReadFile("core/symbol.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("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
|
||||
})
|
||||
preloadLuaModule("core.std", "core/std.lua")
|
||||
preloadLuaModule("core.symbol", "core/symbol.lua")
|
||||
preloadLuaModule("core.util", "core/util.lua")
|
||||
|
||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
||||
mod := L.NewTable()
|
||||
|
||||
coreModules := []string{}
|
||||
if ents, err := fs.ReadDir(config.Core, "core"); err == nil {
|
||||
for _, e := range ents {
|
||||
if e.IsDir() {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
|
||||
continue
|
||||
}
|
||||
n := e.Name()
|
||||
if strings.HasSuffix(n, ".lua") {
|
||||
coreModules = append(coreModules, strings.TrimSuffix(n, ".lua"))
|
||||
}
|
||||
coreModules = append(coreModules, strings.TrimSuffix(e.Name(), ".lua"))
|
||||
}
|
||||
}
|
||||
|
||||
for _, modName := range coreModules {
|
||||
path := filepath.Join("core", modName+".lua")
|
||||
data, err := config.Core.ReadFile(path)
|
||||
fileData, err := config.Core.ReadFile(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if err := L.DoString(string(data)); err != nil {
|
||||
if err := L.DoString(string(fileData)); err != nil {
|
||||
continue
|
||||
}
|
||||
val := L.Get(-1)
|
||||
@@ -211,40 +185,38 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
||||
tbl = L.NewTable()
|
||||
}
|
||||
if modName == "builtin" {
|
||||
tbl.ForEach(func(k, v lua.LValue) {
|
||||
mod.RawSet(k, v)
|
||||
})
|
||||
tbl.ForEach(func(k, v lua.LValue) { mod.RawSet(k, v) })
|
||||
} else {
|
||||
mod.RawSetString(modName, tbl)
|
||||
}
|
||||
}
|
||||
|
||||
includeDir := filepath.Join(luaDir, "include")
|
||||
appTbl := loadIncludeModules(L, includeDir)
|
||||
mod.RawSetString("app", appTbl)
|
||||
mod.RawSetString("app", loadIncludeModules(L, includeDir))
|
||||
|
||||
if cfg != nil {
|
||||
siteTable := L.NewTable()
|
||||
siteTable.RawSetString("version", lua.LString(cfg.App.Version))
|
||||
siteTable.RawSetString("name", lua.LString(cfg.App.Name))
|
||||
authorsTable := L.NewTable()
|
||||
for i, author := range cfg.App.Authors {
|
||||
authorsTable.RawSetInt(i+1, lua.LString(author))
|
||||
site := L.NewTable()
|
||||
site.RawSetString("version", lua.LString(cfg.App.Version))
|
||||
site.RawSetString("name", lua.LString(cfg.App.Name))
|
||||
authors := L.NewTable()
|
||||
for i, a := range cfg.App.Authors {
|
||||
authors.RawSetInt(i+1, lua.LString(a))
|
||||
}
|
||||
siteTable.RawSetString("authors", authorsTable)
|
||||
mod.RawSetString("site", siteTable)
|
||||
site.RawSetString("authors", authors)
|
||||
mod.RawSetString("site", site)
|
||||
}
|
||||
|
||||
reqTable := L.NewTable()
|
||||
reqTable.RawSetString("url", lua.LString(data.url))
|
||||
reqTable.RawSetString("ip", lua.LString(data.ip))
|
||||
reqTable.RawSetString("request", lua.LNumber(data.req))
|
||||
mod.RawSetString("bus", reqTable)
|
||||
bus := L.NewTable()
|
||||
bus.RawSetString("url", lua.LString(requestData.path))
|
||||
params := L.NewTable()
|
||||
for k, v := range requestData.params {
|
||||
params.RawSetString(k, lua.LString(v))
|
||||
}
|
||||
bus.RawSetString("params", params)
|
||||
mod.RawSetString("bus", bus)
|
||||
|
||||
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
||||
mdText := L.ToString(1)
|
||||
html := markdownToHTML(mdText)
|
||||
L.Push(lua.LString(html))
|
||||
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
|
||||
return 1
|
||||
}))
|
||||
|
||||
@@ -256,12 +228,11 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
||||
return "", err
|
||||
}
|
||||
|
||||
top := L.GetTop()
|
||||
if top == 0 {
|
||||
if L.GetTop() == 0 {
|
||||
return "", nil
|
||||
}
|
||||
resultVal := L.Get(-1)
|
||||
L.SetGlobal("__fes_result", resultVal)
|
||||
|
||||
L.SetGlobal("__fes_result", L.Get(-1))
|
||||
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
||||
L.GetGlobal("__fes_result")
|
||||
if s := L.ToString(-1); s != "" {
|
||||
@@ -269,6 +240,7 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if s := L.ToString(-1); s != "" {
|
||||
return s, nil
|
||||
}
|
||||
@@ -319,28 +291,38 @@ func Start(dir string) error {
|
||||
handleDir(entries, wwwDir, 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))
|
||||
color.Yellow("not found")
|
||||
return
|
||||
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
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
for key, val := range r.URL.Query() {
|
||||
if len(val) > 0 {
|
||||
params[key] = val[0]
|
||||
}
|
||||
fmt.Printf("> %s ", filepath.Base(lp))
|
||||
data, err := loadLua(dir, lp, &cfg, reqData{
|
||||
url: r.URL.Host,
|
||||
ip: r.RemoteAddr,
|
||||
})
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
req := reqData{
|
||||
path: r.URL.Path,
|
||||
params: params,
|
||||
}
|
||||
|
||||
fmt.Printf("> %s ", filepath.Base(lp))
|
||||
data, err := loadLua(dir, lp, &cfg, req)
|
||||
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