bus
This commit is contained in:
1
TODO
1
TODO
@@ -1,3 +1,2 @@
|
|||||||
Add an interval element
|
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
|
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)
|
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
|
return site
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type reqData struct {
|
type reqData struct {
|
||||||
url string
|
path string
|
||||||
ip string
|
params map[string]string
|
||||||
req int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@@ -120,88 +119,63 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|||||||
return app
|
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()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
rdents, err := fs.ReadDir(config.Core, "core")
|
coreFiles, err := fs.ReadDir(config.Core, "core")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, de := range rdents {
|
for _, de := range coreFiles {
|
||||||
if de.IsDir() {
|
if de.IsDir() || !strings.HasSuffix(de.Name(), ".lua") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
name := de.Name()
|
path := filepath.Join("core", de.Name())
|
||||||
if !strings.HasSuffix(name, ".lua") {
|
fileData, err := config.Core.ReadFile(path)
|
||||||
continue
|
|
||||||
}
|
|
||||||
path := filepath.Join("core", name)
|
|
||||||
data, err := config.Core.ReadFile(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := L.DoString(string(data)); err != nil {
|
L.DoString(string(fileData))
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
L.PreloadModule("core.std", func(L *lua.LState) int {
|
preloadLuaModule := func(name, path string) {
|
||||||
data, err := config.Core.ReadFile("core/std.lua")
|
L.PreloadModule(name, func(L *lua.LState) int {
|
||||||
|
fileData, err := config.Core.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := L.DoString(string(data)); err != nil {
|
if err := L.DoString(string(fileData)); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
L.Push(L.Get(-1))
|
L.Push(L.Get(-1))
|
||||||
return 1
|
return 1
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
L.PreloadModule("core.symbol", func(L *lua.LState) int {
|
preloadLuaModule("core.std", "core/std.lua")
|
||||||
data, err := config.Core.ReadFile("core/symbol.lua")
|
preloadLuaModule("core.symbol", "core/symbol.lua")
|
||||||
if err != nil {
|
preloadLuaModule("core.util", "core/util.lua")
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
L.PreloadModule("fes", func(L *lua.LState) int {
|
||||||
mod := L.NewTable()
|
mod := L.NewTable()
|
||||||
|
|
||||||
coreModules := []string{}
|
coreModules := []string{}
|
||||||
if ents, err := fs.ReadDir(config.Core, "core"); err == nil {
|
if ents, err := fs.ReadDir(config.Core, "core"); err == nil {
|
||||||
for _, e := range ents {
|
for _, e := range ents {
|
||||||
if e.IsDir() {
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
n := e.Name()
|
coreModules = append(coreModules, strings.TrimSuffix(e.Name(), ".lua"))
|
||||||
if strings.HasSuffix(n, ".lua") {
|
|
||||||
coreModules = append(coreModules, strings.TrimSuffix(n, ".lua"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, modName := range coreModules {
|
for _, modName := range coreModules {
|
||||||
path := filepath.Join("core", modName+".lua")
|
path := filepath.Join("core", modName+".lua")
|
||||||
data, err := config.Core.ReadFile(path)
|
fileData, err := config.Core.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := L.DoString(string(data)); err != nil {
|
if err := L.DoString(string(fileData)); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val := L.Get(-1)
|
val := L.Get(-1)
|
||||||
@@ -211,40 +185,38 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
|||||||
tbl = L.NewTable()
|
tbl = L.NewTable()
|
||||||
}
|
}
|
||||||
if modName == "builtin" {
|
if modName == "builtin" {
|
||||||
tbl.ForEach(func(k, v lua.LValue) {
|
tbl.ForEach(func(k, v lua.LValue) { mod.RawSet(k, v) })
|
||||||
mod.RawSet(k, v)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
mod.RawSetString(modName, tbl)
|
mod.RawSetString(modName, tbl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
includeDir := filepath.Join(luaDir, "include")
|
includeDir := filepath.Join(luaDir, "include")
|
||||||
appTbl := loadIncludeModules(L, includeDir)
|
mod.RawSetString("app", loadIncludeModules(L, includeDir))
|
||||||
mod.RawSetString("app", appTbl)
|
|
||||||
|
|
||||||
if cfg != nil {
|
if cfg != nil {
|
||||||
siteTable := L.NewTable()
|
site := L.NewTable()
|
||||||
siteTable.RawSetString("version", lua.LString(cfg.App.Version))
|
site.RawSetString("version", lua.LString(cfg.App.Version))
|
||||||
siteTable.RawSetString("name", lua.LString(cfg.App.Name))
|
site.RawSetString("name", lua.LString(cfg.App.Name))
|
||||||
authorsTable := L.NewTable()
|
authors := L.NewTable()
|
||||||
for i, author := range cfg.App.Authors {
|
for i, a := range cfg.App.Authors {
|
||||||
authorsTable.RawSetInt(i+1, lua.LString(author))
|
authors.RawSetInt(i+1, lua.LString(a))
|
||||||
}
|
}
|
||||||
siteTable.RawSetString("authors", authorsTable)
|
site.RawSetString("authors", authors)
|
||||||
mod.RawSetString("site", siteTable)
|
mod.RawSetString("site", site)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqTable := L.NewTable()
|
bus := L.NewTable()
|
||||||
reqTable.RawSetString("url", lua.LString(data.url))
|
bus.RawSetString("url", lua.LString(requestData.path))
|
||||||
reqTable.RawSetString("ip", lua.LString(data.ip))
|
params := L.NewTable()
|
||||||
reqTable.RawSetString("request", lua.LNumber(data.req))
|
for k, v := range requestData.params {
|
||||||
mod.RawSetString("bus", reqTable)
|
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 {
|
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
||||||
mdText := L.ToString(1)
|
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
|
||||||
html := markdownToHTML(mdText)
|
|
||||||
L.Push(lua.LString(html))
|
|
||||||
return 1
|
return 1
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -256,12 +228,11 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
top := L.GetTop()
|
if L.GetTop() == 0 {
|
||||||
if top == 0 {
|
|
||||||
return "", nil
|
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 {
|
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
||||||
L.GetGlobal("__fes_result")
|
L.GetGlobal("__fes_result")
|
||||||
if s := L.ToString(-1); s != "" {
|
if s := L.ToString(-1); s != "" {
|
||||||
@@ -269,6 +240,7 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (s
|
|||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if s := L.ToString(-1); s != "" {
|
if s := L.ToString(-1); s != "" {
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
@@ -328,11 +300,21 @@ func Start(dir string) error {
|
|||||||
color.Yellow("not found")
|
color.Yellow("not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
params := make(map[string]string)
|
||||||
|
for key, val := range r.URL.Query() {
|
||||||
|
if len(val) > 0 {
|
||||||
|
params[key] = val[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req := reqData{
|
||||||
|
path: r.URL.Path,
|
||||||
|
params: params,
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("> %s ", filepath.Base(lp))
|
fmt.Printf("> %s ", filepath.Base(lp))
|
||||||
data, err := loadLua(dir, lp, &cfg, reqData{
|
data, err := loadLua(dir, lp, &cfg, req)
|
||||||
url: r.URL.Host,
|
|
||||||
ip: r.RemoteAddr,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
||||||
color.Red("bad")
|
color.Red("bad")
|
||||||
@@ -340,7 +322,7 @@ func Start(dir string) error {
|
|||||||
}
|
}
|
||||||
color.Green("ok")
|
color.Green("ok")
|
||||||
w.Write([]byte(data))
|
w.Write([]byte(data))
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
||||||
return http.ListenAndServe(fmt.Sprintf(":%d", *config.Port), nil)
|
return http.ListenAndServe(fmt.Sprintf(":%d", *config.Port), nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user