alpha p5
This commit is contained in:
2
TODO
2
TODO
@@ -1,3 +1,3 @@
|
|||||||
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 }
|
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
|
||||||
|
|||||||
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"
|
"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 {
|
func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, base string) error {
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.IsDir() {
|
if entry.IsDir() {
|
||||||
@@ -27,7 +33,12 @@ func handleDir(entries []os.DirEntry, wwwDir string, routes map[string]string, b
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read %s: %w", sub, err)
|
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 {
|
if err := handleDir(subs, sub, routes, next); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -109,7 +120,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|||||||
return app
|
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()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
@@ -224,6 +235,12 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error)
|
|||||||
mod.RawSetString("site", siteTable)
|
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 {
|
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
||||||
mdText := L.ToString(1)
|
mdText := L.ToString(1)
|
||||||
html := markdownToHTML(mdText)
|
html := markdownToHTML(mdText)
|
||||||
@@ -286,7 +303,7 @@ func Start(dir string) error {
|
|||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
if _, err := os.Stat(filepath.Join(wwwDir, "404.lua")); err == nil {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -312,7 +329,10 @@ func Start(dir string) error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("> %s ", filepath.Base(lp))
|
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 {
|
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user