update TODO & 404 handling

This commit is contained in:
2025-12-01 17:21:17 -05:00
parent a67a654491
commit 2e876a4a47
9 changed files with 104 additions and 15 deletions

3
TODO
View File

@@ -1,2 +1,3 @@
Add an interval element Add an interval element
Add a way to pass data to sites on load via fes.bus Add a way to pass data to sites on load via fes.bus: pass a table of { url, IP, request, headers, body }
Serve site/static at /static

View File

@@ -0,0 +1,17 @@
local fes = require("fes")
local std = fes.std
local u = fes.util
local site = fes.fes()
site.title = "404 Page Not Found"
site.copyright = u.copyright("https://git.vxserver.dev/fSD/", "fSD")
site:banner(std.h1(std.center("Canonical")))
site:note(table.concat({
std.center(std.h1("404 Page Not Found")),
std.center(std.p("The page you are looking for is not here. " .. std.a("/", "Go home?"))),
}))
return site

5
examples/error/Fes.toml Normal file
View File

@@ -0,0 +1,5 @@
[app]
name = "error"
version = "0.0.1"
authors = ["vx-clutch"]

View File

@@ -0,0 +1,9 @@
local fes = require("fes")
local site = fes.fes()
site.title = "error"
UNIX is very simple
GNU's Not UNIX
return site

4
go.mod
View File

@@ -3,8 +3,12 @@ module fes
go 1.25.4 go 1.25.4
require ( require (
github.com/fatih/color v1.18.0 // indirect
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a // indirect github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a // indirect
github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585 // indirect github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect github.com/yuin/gopher-lua v1.1.1 // indirect
golang.org/x/sys v0.25.0 // indirect
) )

11
go.sum
View File

@@ -1,8 +1,19 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a h1:l7A0loSszR5zHd/qK53ZIHMO8b3bBSmENnQ6eKnUT0A= github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a h1:l7A0loSszR5zHd/qK53ZIHMO8b3bBSmENnQ6eKnUT0A=
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585/go.mod h1:6grYm5/uY15CwgBBqwA3+o/cAzaxssckznJ0B35ouBY= github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585/go.mod h1:6grYm5/uY15CwgBBqwA3+o/cAzaxssckznJ0B35ouBY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

View File

@@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/fatih/color"
"fes/src/config" "fes/src/config"
"fes/src/new" "fes/src/new"
"fes/src/server" "fes/src/server"
@@ -16,7 +18,8 @@ import (
var core embed.FS var core embed.FS
func init() { func init() {
config.Port = flag.Int("p", 3000, "set the server port") config.Port = flag.Int("p", 3000, "Set the server port")
config.Color = flag.Bool("no-color", false, "Disable color output")
config.Core = core config.Core = core
} }
@@ -27,6 +30,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if *config.Color {
color.NoColor = true
}
cmd := os.Args[1] cmd := os.Args[1]
dir := os.Args[2] dir := os.Args[2]

View File

@@ -4,6 +4,7 @@ import "embed"
var Core embed.FS var Core embed.FS
var Port *int var Port *int
var Color *bool
type MyConfig struct { type MyConfig struct {
App struct { App struct {

View File

@@ -14,6 +14,7 @@ import (
"github.com/gomarkdown/markdown/parser" "github.com/gomarkdown/markdown/parser"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
"github.com/fatih/color"
"fes/src/config" "fes/src/config"
) )
@@ -157,6 +158,18 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig) (string, error)
return 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{}
@@ -263,22 +276,43 @@ func Start(dir string) error {
return fmt.Errorf("failed to read www directory: %w", err) 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) routes := make(map[string]string)
handleDir(entries, wwwDir, routes, "") handleDir(entries, wwwDir, routes, "")
for route, luaPath := range routes { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
func(rt string, lp string) { path := r.URL.Path
http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) { lp, ok := routes[path]
fmt.Printf("-> %s\n", lp) if !ok {
data, err := loadLua(dir, lp, &cfg) w.WriteHeader(http.StatusNotFound)
if err != nil { w.Write([]byte(notFoundData))
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError) fmt.Printf("> %s.lua ", filepath.Base(path))
return color.Yellow("not found")
} return
w.Write([]byte(data)) }
}) fmt.Printf("> %s ", filepath.Base(lp))
}(route, luaPath) 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) 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)