pre refactor
This commit is contained in:
26
main.go
26
main.go
@@ -11,6 +11,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
@@ -21,11 +22,16 @@ import (
|
||||
|
||||
//go:embed core/builtin.lua
|
||||
var builtinLua string
|
||||
|
||||
//go:embed core/markdown.lua
|
||||
var markdownLua string
|
||||
|
||||
//go:embed core/std.lua
|
||||
var stdLua string
|
||||
|
||||
//go:embed core/console.lua
|
||||
var consoleLua string
|
||||
|
||||
const version = "1.0.0"
|
||||
|
||||
type MyConfig struct {
|
||||
@@ -58,15 +64,24 @@ func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.PreloadModule("core.std", func(L *lua.LState) int {
|
||||
if err := L.DoString(stdLua); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
L.Push(L.Get(-1))
|
||||
return 1
|
||||
})
|
||||
|
||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
||||
mod := L.NewTable()
|
||||
|
||||
|
||||
coreModules := map[string]string{
|
||||
"builtin": builtinLua,
|
||||
"markdown": markdownLua,
|
||||
"std": stdLua,
|
||||
"console": consoleLua,
|
||||
}
|
||||
|
||||
|
||||
for modName, luaCode := range coreModules {
|
||||
if err := L.DoString(luaCode); err != nil {
|
||||
fmt.Println("error loading", modName, ":", err)
|
||||
@@ -220,7 +235,7 @@ func startServer(dir string) error {
|
||||
}
|
||||
|
||||
wwwDir := filepath.Join(dir, "www")
|
||||
|
||||
|
||||
entries, err := os.ReadDir(wwwDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read www directory: %w", err)
|
||||
@@ -231,7 +246,7 @@ func startServer(dir string) error {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lua") {
|
||||
baseName := strings.TrimSuffix(entry.Name(), ".lua")
|
||||
luaPath := filepath.Join(wwwDir, entry.Name())
|
||||
|
||||
|
||||
if baseName == "index" {
|
||||
routes["/"] = luaPath
|
||||
routes["/index"] = luaPath
|
||||
@@ -244,6 +259,7 @@ func startServer(dir string) error {
|
||||
for route, luaPath := range routes {
|
||||
func(rt string, lp string) {
|
||||
http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("[%s] LOAD /%s\n", time.Now().Format(time.RFC1123), lp)
|
||||
data, err := loadLua(dir, lp, &cfg)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
||||
@@ -255,7 +271,7 @@ func startServer(dir string) error {
|
||||
}
|
||||
|
||||
fmt.Printf("Server is running on http://localhost:%d\n", *port)
|
||||
|
||||
|
||||
return http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user