alpha p5
This commit is contained in:
2
TODO
2
TODO
@@ -1,3 +1,3 @@
|
||||
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 }
|
||||
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
|
||||
|
||||
5
examples/bus/Fes.toml
Normal file
5
examples/bus/Fes.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
[app]
|
||||
|
||||
name = "bus"
|
||||
version = "0.0.1"
|
||||
authors = ["vx-clutch"]
|
||||
9
examples/bus/www/index.lua
Normal file
9
examples/bus/www/index.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
local fes = require("fes")
|
||||
local site = fes.fes()
|
||||
|
||||
site.title = "bus"
|
||||
site.copyright = fes.util.copyright("https://git.vxserver.dev/fSD/", "fSD")
|
||||
|
||||
site:h1("URL: " .. fes.bus.url)
|
||||
|
||||
return site
|
||||
@@ -19,6 +19,12 @@ import (
|
||||
"fes/src/config"
|
||||
)
|
||||
|
||||
type reqData struct {
|
||||
url string
|
||||
ip string
|
||||
req int
|
||||
}
|
||||
|
||||
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
@@ -27,7 +33,12 @@ func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, b
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read %s: %w", sub, err)
|
||||
}
|
||||
next := base + "/" + entry.Name()
|
||||
var next string
|
||||
if base == "" {
|
||||
next = "/" + entry.Name()
|
||||
} else {
|
||||
next = base + "/" + entry.Name()
|
||||
}
|
||||
if err := handleDir(subs, sub, routes, next); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -109,7 +120,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
||||
return app
|
||||
}
|
||||
|
||||
func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error) {
|
||||
func loadLua(luaDir string, entry string, cfg *config.MyConfig, data reqData) (string, error) {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
@@ -224,6 +235,12 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error)
|
||||
mod.RawSetString("site", siteTable)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
||||
mdText := L.ToString(1)
|
||||
html := markdownToHTML(mdText)
|
||||
@@ -286,7 +303,7 @@ func Start(dir string) error {
|
||||
</html>
|
||||
`
|
||||
if _, err := os.Stat(filepath.Join(wwwDir, "404.lua")); err == nil {
|
||||
notFoundData, err = loadLua(dir, filepath.Join(wwwDir, "404.lua"), &cfg)
|
||||
notFoundData, err = loadLua(dir, filepath.Join(wwwDir, "404.lua"), &cfg, reqData{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -312,7 +329,10 @@ func Start(dir string) error {
|
||||
return
|
||||
}
|
||||
fmt.Printf("> %s ", filepath.Base(lp))
|
||||
data, err := loadLua(dir, lp, &cfg)
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user