merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi
About | Files | Commits | Refs | License
refactor: remove HTTP server and routing API
Author: pi66
Date: 2026-09-19 20:35
commit: fff9958a
Stat
| D | examples/http.lua | 40 | +0 | -40 |
| D | src/config/main.go | 9 | +0 | -9 |
| D | src/core/lua/http.go | 129 | +0 | -129 |
| D | src/core/lua/main.go | 3 | +0 | -3 |
| D | src/core/main.go | 2 | +0 | -2 |
| D | src/core/serve/main.go | 59 | +0 | -59 |
| M | src/utils/main.go | 2 | +1 | -1 |
Diff
diff --git a/examples/http.lua b/examples/http.lua
deleted file mode 100644
index 888d270..0000000
--- a/examples/http.lua
+++ /dev/null
@@ -1,40 +0,0 @@
--- HTTP are available only with `merodi serve`
--- Use routes to handle requests and return responses
-
-merodi.http.route("/path", function()
- return {
- mime = "text/plain",
- value = "response",
- status = 200
- }
-end)
-
--- Registers a handler for a URL path.
--- The handler returns a response table:
--- mime (string) - response Content-Type
...
Show all
Show less
diff --git a/examples/http.lua b/examples/http.lua
deleted file mode 100644
index 888d270..0000000
--- a/examples/http.lua
+++ /dev/null
@@ -1,40 +0,0 @@
--- HTTP are available only with `merodi serve`
--- Use routes to handle requests and return responses
-
-merodi.http.route("/path", function()
- return {
- mime = "text/plain",
- value = "response",
- status = 200
- }
-end)
-
--- Registers a handler for a URL path.
--- The handler returns a response table:
--- mime (string) - response Content-Type
...
diff --git a/examples/http.lua b/examples/http.lua
deleted file mode 100644
index 888d270..0000000
--- a/examples/http.lua
+++ /dev/null
@@ -1,40 +0,0 @@
--- HTTP are available only with `merodi serve`
--- Use routes to handle requests and return responses
-
-merodi.http.route("/path", function()
- return {
- mime = "text/plain",
- value = "response",
- status = 200
- }
-end)
-
--- Registers a handler for a URL path.
--- The handler returns a response table:
--- mime (string) - response Content-Type
--- value (string) - response body
--- status (number) - HTTP status code
--- Defaults: mime = "text/plain", status = 200.
-
-merodi.http.method()
--- Returns the current request method, such as "GET" or "POST".
--- Only available inside a route.
-
-merodi.http.request.body()
--- Returns the request body as a string.
-
-merodi.http.request.remote_addr()
--- Returns the client's IP address.
-
--- Server settings
-merodi.http.settings.host.set("localhost")
--- Sets the host the server listens on.
-
-merodi.http.settings.port.set("8866")
--- Sets the port the server listens on.
-
-merodi.http.settings.host.get()
--- Returns the configured host.
-
-merodi.http.settings.port.get()
--- Returns the configured port.
diff --git a/src/config/main.go b/src/config/main.go
index 424accc..f8e5cca 100644
--- a/src/config/main.go
+++ b/src/config/main.go
@@ -31,15 +31,10 @@ type Tree struct {
ReleaseDest string `toml:"release_dest"`
}
-type Http struct {
- Host string `toml:"host"`
- Port string `toml:"port"`
-}
type Data struct {
Project Project `toml:"project"`
Tree Tree `toml:"tree"`
- Http Http `toml:"http"`
}
type Config struct {
@@ -61,10 +56,6 @@ func (self *Config) DefaultData() {
DraftDest: "build/draft",
ReleaseDest: "build/release",
},
- Http: Http{
- Host: "localhost",
- Port: "8000",
- },
}
}
diff --git a/src/core/lua/http.go b/src/core/lua/http.go
deleted file mode 100644
index fe43e05..0000000
--- a/src/core/lua/http.go
+++ /dev/null
@@ -1,129 +0,0 @@
-package lua
-
-import (
- "merodi/src/utils/log"
-
- glua "github.com/yuin/gopher-lua"
-)
-
-type Request struct {
- RemoteAddr string
- Body string
- Method string
-}
-
-type Response struct {
- Mime string
- Body string
- Status int
-}
-
-type Settings struct {
- Host string
- Port string
-}
-
-type Http struct {
- Settings Settings
- Table *glua.LTable
- Route map[string]*glua.LFunction
-}
-
-func (self *Lua) registerHttp() {
- self.Http.Table = self.Context.NewTable()
-
- self.Http.Route = make(map[string]*glua.LFunction)
- self.httpSettings()
- self.httpRoute()
- self.Merodi.RawSetString("http", self.Http.Table)
-}
-
-func (self *Lua) httpSettings() {
- settingsTable := self.Context.NewTable()
-
- settingsTable.RawSetString("host", self.makeGetSetTable(
- func() string { return self.Http.Settings.Host },
- func(v string) { self.Http.Settings.Host = v },
- ))
-
- settingsTable.RawSetString("port", self.makeGetSetTable(
- func() string { return self.Http.Settings.Port },
- func(v string) { self.Http.Settings.Port = v },
- ))
-
- self.Http.Table.RawSetString("settings", settingsTable)
-}
-
-func (self *Lua) httpRequest(req Request) {
- requestTable := self.Context.NewTable()
-
- self.Context.SetFuncs(requestTable, map[string]glua.LGFunction{
- "body": func(L *glua.LState) int {
- L.Push(glua.LString(req.Body))
- return 1
- },
- "remote_addr": func(L *glua.LState) int {
- L.Push(glua.LString(req.RemoteAddr))
- return 1
- },
- "method": func(L *glua.LState) int {
- L.Push(glua.LString(req.Method))
- return 1
- },
- })
-
- self.Http.Table.RawSetString("request", requestTable)
-}
-
-func (self *Lua) httpRoute() {
- self.Http.Table.RawSetString("route", self.Context.NewFunction(func(L *glua.LState) int {
- path := L.CheckString(1)
- fn := L.CheckFunction(2)
- self.Http.Route[path] = fn
- return 0
- }))
-}
-
-func (self *Lua) RunRoute(path string, req Request) (Response, error) {
- fn, ok := self.Http.Route[path]
- if !ok {
- return Response{Mime: "text/plain", Body: "this page does not exists", Status: 404}, nil
- }
-
- self.httpRequest(req)
- err := self.Context.CallByParam(glua.P{
- Fn: fn,
- NRet: 1,
- Protect: true,
- })
-
- if err != nil {
- log.Warn(log.Title("HTTP"), "route: %s", err)
- return Response{}, err
- }
-
- ret := self.Context.Get(-1)
- self.Context.Pop(1)
-
- resp := Response{
- Mime: "text/plain",
- Body: "",
- Status: 200,
- }
-
- if t, ok := ret.(*glua.LTable); ok {
- if v := t.RawGetString("mime"); v != glua.LNil {
- resp.Mime = v.String()
- }
- if v := t.RawGetString("body"); v != glua.LNil {
- resp.Body = v.String()
- }
- if v := t.RawGetString("status"); v != glua.LNil {
- if n, ok := v.(glua.LNumber); ok {
- resp.Status = int(n)
- }
- }
- }
-
- return resp, nil
-}
diff --git a/src/core/lua/main.go b/src/core/lua/main.go
index 0329316..33d4460 100644
--- a/src/core/lua/main.go
+++ b/src/core/lua/main.go
@@ -17,7 +17,6 @@ type Lua struct {
Jinja map[string]any
Build *Build
Watch *Watcher
- Http *Http
Extensions []goldmark.Extender
ParsersOption []parser.Option
@@ -31,7 +30,6 @@ func (self *Lua) InitLua(cfg *config.Data) error {
self.Hooks = make(map[string]*glua.LFunction)
self.Jinja = make(map[string]any)
self.Context = glua.NewState()
- self.Http = &Http{}
self.Watch = &Watcher{}
self.Build = &Build{}
self.Aborted = false
@@ -64,7 +62,6 @@ func (self *Lua) registerMerodi() {
self.registerConfig()
self.registerJinja()
self.registerBuild()
- self.registerHttp()
self.registerWatch()
self.registerEnable()
self.registerDisable()
diff --git a/src/core/main.go b/src/core/main.go
index fdbc27b..c53c0e9 100644
--- a/src/core/main.go
+++ b/src/core/main.go
@@ -3,7 +3,6 @@ package core
import (
"merodi/src/core/build"
"merodi/src/core/lua"
- "merodi/src/core/serve"
"merodi/src/core/state"
"merodi/src/core/watch"
"merodi/src/utils"
@@ -22,7 +21,6 @@ type Action interface {
var Actions = map[string]Action{
"build": &build.Build{},
"watch": &watch.Watcher{},
- "serve": &serve.Serve{},
}
func Run(act string, args *[]string) (err error) {
diff --git a/src/core/serve/main.go b/src/core/serve/main.go
deleted file mode 100644
index b461871..0000000
--- a/src/core/serve/main.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package serve
-
-import (
- "fmt"
- "io"
- "merodi/src/core/lua"
- "merodi/src/core/state"
- "merodi/src/utils"
- "merodi/src/utils/log"
- "net/http"
-)
-
-var (
- handle = utils.Handle
- check = utils.Check
-)
-
-type Serve struct {
- State *state.State
-}
-
-func (self *Serve) handler(w http.ResponseWriter, r *http.Request) {
- defer r.Body.Close()
- r.Body = http.MaxBytesReader(w, r.Body, 20<<20)
- body, err := io.ReadAll(r.Body)
- if err != nil {
- log.Warn(log.Title("HTTP"), "%s", err)
- return
- }
- requests := lua.Request{}
- requests.Method = r.Method
- requests.Body = string(body)
- requests.RemoteAddr = r.RemoteAddr
- response, err := self.State.Lua.RunRoute(r.URL.Path, requests)
- if err != nil {
- log.Warn(log.Title("HTTP"), "%s", err)
- return
- }
- w.Header().Set("Content-Type", response.Mime)
- w.WriteHeader(response.Status)
- _, err = w.Write([]byte(response.Body))
- if err != nil {
- log.Warn(log.Title("HTTP"), "%s", err)
- }
-}
-func (self *Serve) StartHTTP() (err error) {
- defer handle(&err)
- host := self.State.Lua.Http.Settings.Host
- port := self.State.Lua.Http.Settings.Port
- mux := http.NewServeMux()
- mux.HandleFunc("/", self.handler)
- return http.ListenAndServe(fmt.Sprintf("%s:%s", host, port), mux)
-}
-func (self *Serve) Run(state *state.State, args *[]string) (err error) {
- defer handle(&err)
- self.State = state
- check(self.StartHTTP())
- return err
-}
diff --git a/src/utils/main.go b/src/utils/main.go
index 081944c..e722fa6 100644
--- a/src/utils/main.go
+++ b/src/utils/main.go
@@ -24,7 +24,7 @@ func Check(err error) {
}
func PrintHelp() {
- log.Info(log.Title("Usage"), ": merodi {init,watch,build,serve,version} <path> ...")
+ log.Info(log.Title("Usage"), ": merodi {init,watch,build,version} <path> ...")
}
func Pop(args *[]string, idx int) string {