feat(web): Go W-G4 listen, static, middleware, and JSON routes
Add app.static / configure middleware bags and blocking httpx.Listen (home, routes, _part, static, forms, JSON API, CORS/gzip layers). Pass route/static/middleware/part offline smokes against libweb.so. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1fc985db7e
commit
1ef71d7492
9 files changed
+1276
-1
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · 实现进行中(W-G0…W-G3 ✅)** |
|
||||
| 状态 | **Accepted · 实现进行中(W-G0…W-G4 ✅)** |
|
||||
| 日期 | 2026-09-10 |
|
||||
| 相关 | [ext-web.md](ext-web.md) · [ext-web-net.md](ext-web-net.md) · [web-net-capabilities.md](web-net-capabilities.md) · [ext-web-drivers.md](ext-web-drivers.md) · [web-assets-and-images.md](web-assets-and-images.md) · [ext-hosting-fill.md](ext-hosting-fill.md) · [ext-abi.md](ext-abi.md) · [ext-cli.md](ext-cli.md) · [web-asgi-servers-and-marqdo.md](web-asgi-servers-and-marqdo.md) |
|
||||
| 实现目录 | `plugins/web/`(**Go module**;产物仍为 `libweb.so` / `web.dll` / `libweb.dylib`) |
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/form"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/httpx"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
)
|
||||
|
||||
func txnOpt(args map[string]any) string {
|
||||
@@ -219,3 +221,42 @@ func web_app_mount_form(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.i
|
||||
out, err := app.MountForm(asPageMap(args["app"]), id, args["form"])
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_static
|
||||
func web_app_static(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
dir, err := argStrReq(args, "dir")
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
mount, _ := argStr(args, "mount")
|
||||
out, err := app.Static(asPageMap(args["app"]), dir, mount)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_middleware
|
||||
func web_app_middleware(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
out, err := middleware.Configure(asPageMap(args["app"]), args)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_listen
|
||||
func web_listen(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
appBag := asPageMap(args["app"])
|
||||
if len(appBag) == 0 {
|
||||
appBag = args
|
||||
}
|
||||
out, err := httpx.Listen(appBag, entryDir())
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
@@ -109,6 +109,49 @@ func MountForm(appBag map[string]any, id string, form any) (map[string]any, erro
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// NormalizeStaticMount mirrors Rust http::normalize_static_mount.
|
||||
func NormalizeStaticMount(raw string) string {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" {
|
||||
return "/static"
|
||||
}
|
||||
m := s
|
||||
if !strings.HasPrefix(m, "/") {
|
||||
m = "/" + m
|
||||
}
|
||||
for len(m) > 1 && strings.HasSuffix(m, "/") {
|
||||
m = strings.TrimSuffix(m, "/")
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Static sets static_dir + static_mount (Rust web_app_static).
|
||||
func Static(appBag map[string]any, dir, mount string) (map[string]any, error) {
|
||||
dir = strings.TrimSpace(dir)
|
||||
if dir == "" {
|
||||
return nil, fmt.Errorf("static `dir` is empty")
|
||||
}
|
||||
if mount == "" {
|
||||
mount = "/static"
|
||||
}
|
||||
mount = NormalizeStaticMount(mount)
|
||||
if mount == "/_form" || strings.HasPrefix(mount, "/_form/") ||
|
||||
mount == "/_part" || strings.HasPrefix(mount, "/_part/") {
|
||||
return nil, fmt.Errorf("static mount `%s` is reserved", mount)
|
||||
}
|
||||
if boolish(appBag["admin"]) {
|
||||
prefix := strOpt(appBag, "admin_prefix", "/admin")
|
||||
prefix = NormalizeStaticMount(prefix)
|
||||
if mount == prefix || strings.HasPrefix(mount, prefix+"/") {
|
||||
return nil, fmt.Errorf("static mount `%s` is reserved", mount)
|
||||
}
|
||||
}
|
||||
out := clone(appBag)
|
||||
out["static_dir"] = dir
|
||||
out["static_mount"] = mount
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func normalizeRoutePath(raw string) (string, error) {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
)
|
||||
|
||||
func TestNormalizeStaticMount(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"", "/static"},
|
||||
{" ", "/static"},
|
||||
{"static", "/static"},
|
||||
{"/static/", "/static"},
|
||||
{"/assets", "/assets"},
|
||||
{"assets/", "/assets"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := app.NormalizeStaticMount(c.in); got != c.want {
|
||||
t.Fatalf("NormalizeStaticMount(%q)=%q want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticSetsDirAndMount(t *testing.T) {
|
||||
a := app.New(map[string]any{"page": map[string]any{"title": "Home"}})
|
||||
out, err := app.Static(a, "web-fixtures/public", "/static")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out["static_dir"] != "web-fixtures/public" {
|
||||
t.Fatalf("static_dir=%v", out["static_dir"])
|
||||
}
|
||||
if out["static_mount"] != "/static" {
|
||||
t.Fatalf("static_mount=%v", out["static_mount"])
|
||||
}
|
||||
out2, err := app.Static(out, "assets", "/assets/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out2["static_mount"] != "/assets" {
|
||||
t.Fatalf("custom mount=%v", out2["static_mount"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticRejectsReserved(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
for _, mount := range []string{"/_form", "/_form/x", "/_part", "/_part/nav"} {
|
||||
_, err := app.Static(a, "pub", mount)
|
||||
if err == nil || !strings.Contains(err.Error(), "reserved") {
|
||||
t.Fatalf("mount %q: want reserved error, got %v", mount, err)
|
||||
}
|
||||
}
|
||||
admin := app.New(map[string]any{"admin": true, "admin_prefix": "/admin"})
|
||||
_, err := app.Static(admin, "pub", "/admin")
|
||||
if err == nil || !strings.Contains(err.Error(), "reserved") {
|
||||
t.Fatalf("admin mount: want reserved, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,542 @@
|
||||
// Package httpx implements blocking HTTP listen for the Marqdo web app bag.
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/form"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/render"
|
||||
)
|
||||
|
||||
// NewHandler builds the HTTP handler for an app bag (no listen).
|
||||
// Relative static_dir is resolved against entryDir when non-empty, else cwd.
|
||||
func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
if appBag == nil {
|
||||
appBag = map[string]any{}
|
||||
}
|
||||
page, _ := appBag["page"].(map[string]any)
|
||||
if page == nil {
|
||||
page = map[string]any{}
|
||||
}
|
||||
dbURL := dbURLOf(appBag)
|
||||
routes := map[string]any{}
|
||||
if r, ok := appBag["routes"].(map[string]any); ok {
|
||||
routes = r
|
||||
}
|
||||
forms := map[string]any{}
|
||||
if f, ok := appBag["forms"].(map[string]any); ok {
|
||||
forms = cloneMap(f)
|
||||
}
|
||||
collectPageForms(page, forms)
|
||||
for _, p := range routes {
|
||||
if pm, ok := p.(map[string]any); ok {
|
||||
collectPageForms(pm, forms)
|
||||
}
|
||||
}
|
||||
|
||||
staticMount := app.NormalizeStaticMount(strOpt(appBag, "static_mount", "/static"))
|
||||
var staticDir string
|
||||
if s, ok := appBag["static_dir"].(string); ok && strings.TrimSpace(s) != "" {
|
||||
staticDir = resolvePath(strings.TrimSpace(s), entryDir)
|
||||
fi, err := os.Stat(staticDir)
|
||||
if err != nil || !fi.IsDir() {
|
||||
return nil, fmt.Errorf("static dir `%s` is not a directory", staticDir)
|
||||
}
|
||||
}
|
||||
|
||||
mw := middleware.Parse(appBag)
|
||||
st := &state{
|
||||
page: page,
|
||||
dbURL: dbURL,
|
||||
routes: routes,
|
||||
forms: forms,
|
||||
mw: mw,
|
||||
entryDir: entryDir,
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /{$}", st.handleHome)
|
||||
mux.HandleFunc("GET /_part/{id}", st.handleHomePart)
|
||||
mux.HandleFunc("GET /_form/{id}", st.handleFormGet)
|
||||
mux.HandleFunc("POST /_form/{id}", st.handleFormPost)
|
||||
|
||||
paths := sortedKeys(routes)
|
||||
for _, p := range paths {
|
||||
routePath := p
|
||||
pageV, _ := routes[routePath].(map[string]any)
|
||||
if pageV == nil {
|
||||
pageV = map[string]any{}
|
||||
}
|
||||
if strings.Contains(routePath, "{") {
|
||||
mux.HandleFunc("GET "+routePath, st.makeDynamicPage(pageV, routePath))
|
||||
mux.HandleFunc("GET "+routePath+"/_part/{id}", st.makeDynamicPart(pageV, routePath))
|
||||
} else {
|
||||
pv := pageV
|
||||
mux.HandleFunc("GET "+routePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePage(w, pv)
|
||||
})
|
||||
mux.HandleFunc("GET "+routePath+"/_part/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePart(w, pv, r.PathValue("id"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, jr := range mw.JSONRoutes {
|
||||
route := jr
|
||||
pattern := route.Method + " " + route.Path
|
||||
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.handleJSON(w, route)
|
||||
})
|
||||
}
|
||||
|
||||
if staticDir != "" {
|
||||
fmt.Fprintf(os.Stderr, "marqdo web static: %s → %s\n", staticMount, staticDir)
|
||||
fs := http.StripPrefix(staticMount, http.FileServer(http.Dir(staticDir)))
|
||||
mux.Handle("GET "+staticMount+"/", fs)
|
||||
mux.Handle("HEAD "+staticMount+"/", fs)
|
||||
mux.Handle("GET "+staticMount, fs)
|
||||
mux.Handle("HEAD "+staticMount, fs)
|
||||
}
|
||||
|
||||
return withMiddleware(mux, mw), nil
|
||||
}
|
||||
|
||||
// Listen binds host:port from the app bag and serves until interrupt/kill.
|
||||
func Listen(appBag map[string]any, entryDir string) (map[string]any, error) {
|
||||
if appBag == nil {
|
||||
appBag = map[string]any{}
|
||||
}
|
||||
handler, err := NewHandler(appBag, entryDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
host := strOpt(appBag, "host", "127.0.0.1")
|
||||
port := intFrom(appBag["port"], 18081)
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bind %s: %w", addr, err)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "marqdo web listening on http://%s\n", addr)
|
||||
|
||||
srv := &http.Server{Handler: handler}
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- srv.Serve(ln)
|
||||
}()
|
||||
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(sigCh)
|
||||
select {
|
||||
case sig := <-sigCh:
|
||||
_ = srv.Close()
|
||||
fmt.Fprintf(os.Stderr, "marqdo web shutdown (%v)\n", sig)
|
||||
case err := <-errCh:
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
return nil, fmt.Errorf("serve: %w", err)
|
||||
}
|
||||
}
|
||||
return map[string]any{"ok": true}, nil
|
||||
}
|
||||
|
||||
type state struct {
|
||||
page map[string]any
|
||||
dbURL string
|
||||
routes map[string]any
|
||||
forms map[string]any
|
||||
mw middleware.Config
|
||||
entryDir string
|
||||
}
|
||||
|
||||
func (st *state) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePage(w, st.page)
|
||||
}
|
||||
|
||||
func (st *state) handleHomePart(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePart(w, st.page, r.PathValue("id"))
|
||||
}
|
||||
|
||||
func (st *state) writePage(w http.ResponseWriter, page map[string]any) {
|
||||
html := render.RenderPage(page, st.dbURL, "")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) writePart(w http.ResponseWriter, page map[string]any, id string) {
|
||||
html := render.RenderPage(page, st.dbURL, id)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) makeDynamicPage(page map[string]any, pattern string) http.HandlerFunc {
|
||||
paramNames := pathParamNames(pattern)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
p := cloneMap(page)
|
||||
params := map[string]any{}
|
||||
for _, name := range paramNames {
|
||||
params[name] = r.PathValue(name)
|
||||
}
|
||||
injectParams(p, params)
|
||||
st.writePage(w, p)
|
||||
}
|
||||
}
|
||||
|
||||
func (st *state) makeDynamicPart(page map[string]any, pattern string) http.HandlerFunc {
|
||||
paramNames := pathParamNames(pattern)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
p := cloneMap(page)
|
||||
params := map[string]any{}
|
||||
for _, name := range paramNames {
|
||||
params[name] = r.PathValue(name)
|
||||
}
|
||||
injectParams(p, params)
|
||||
st.writePart(w, p, r.PathValue("id"))
|
||||
}
|
||||
}
|
||||
|
||||
func (st *state) handleFormGet(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
frm, ok := st.forms[id].(map[string]any)
|
||||
if !ok {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, fmt.Sprintf("<p>unknown form %s</p>", id))
|
||||
return
|
||||
}
|
||||
html := form.Render(frm, id, nil, nil, "")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
frm, ok := st.forms[id].(map[string]any)
|
||||
if !ok {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, fmt.Sprintf("<p>unknown form %s</p>", id))
|
||||
return
|
||||
}
|
||||
if st.dbURL == "" {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, "<p>no database</p>")
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
data := map[string]any{}
|
||||
for k, vs := range r.PostForm {
|
||||
if len(vs) > 0 {
|
||||
data[k] = vs[0]
|
||||
}
|
||||
}
|
||||
res, err := form.Submit(frm, data, st.dbURL)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
redir := strOpt(res, "redirect", "/")
|
||||
http.Redirect(w, r, redir, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
html := form.Render(frm, id, data, res["errors"], "")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleJSON(w http.ResponseWriter, route middleware.JSONRouteMount) {
|
||||
if st.dbURL == "" {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": "no database"})
|
||||
return
|
||||
}
|
||||
opts := db.SelectOpts{Where: route.Where, Order: route.Order}
|
||||
out, err := db.Select(st.dbURL, route.Table, route.Limit, opts)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
|
||||
h := next
|
||||
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
|
||||
limit := int64(*mw.BodyLimit)
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, limit)
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
if mw.CacheControl != "" {
|
||||
cc := mw.CacheControl
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", cc)
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
if mw.Compress {
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
inner.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
w.Header().Add("Vary", "Accept-Encoding")
|
||||
gz := gzip.NewWriter(w)
|
||||
defer gz.Close()
|
||||
inner.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, w: gz}, r)
|
||||
})
|
||||
}
|
||||
for _, hv := range mw.Security {
|
||||
name, ok := securityHeaderName(hv[0])
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
val := hv[1]
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(name, val)
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
if mw.CORS != nil {
|
||||
c := mw.CORS
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
applyCORS(w, r, c)
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
if mw.AccessLog {
|
||||
inner := h
|
||||
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
rw := &statusRecorder{ResponseWriter: w, status: 200}
|
||||
inner.ServeHTTP(rw, r)
|
||||
fmt.Fprintf(os.Stderr, "marqdo web %s %s %d %dms\n",
|
||||
r.Method, r.URL.Path, rw.status, time.Since(start).Milliseconds())
|
||||
})
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func applyCORS(w http.ResponseWriter, r *http.Request, c *middleware.CORSConfig) {
|
||||
origin := r.Header.Get("Origin")
|
||||
if len(c.AllowOrigins) == 0 {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
} else if origin != "" {
|
||||
for _, o := range c.AllowOrigins {
|
||||
if o == origin {
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(c.Methods) == 0 {
|
||||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||||
} else {
|
||||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(unique(c.Methods), ", "))
|
||||
}
|
||||
if len(c.Headers) == 0 {
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
} else {
|
||||
w.Header().Set("Access-Control-Allow-Headers", strings.Join(unique(c.Headers), ", "))
|
||||
}
|
||||
if len(c.ExposeHeaders) > 0 {
|
||||
w.Header().Set("Access-Control-Expose-Headers", strings.Join(unique(c.ExposeHeaders), ", "))
|
||||
}
|
||||
if c.Credentials {
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
}
|
||||
}
|
||||
|
||||
func securityHeaderName(raw string) (string, bool) {
|
||||
n := strings.ToLower(strings.ReplaceAll(raw, "_", "-"))
|
||||
switch n {
|
||||
case "x-frame-options":
|
||||
return "X-Frame-Options", true
|
||||
case "content-security-policy":
|
||||
return "Content-Security-Policy", true
|
||||
case "x-content-type-options":
|
||||
return "X-Content-Type-Options", true
|
||||
case "referrer-policy":
|
||||
return "Referrer-Policy", true
|
||||
case "strict-transport-security", "hsts":
|
||||
return "Strict-Transport-Security", true
|
||||
case "x-xss-protection":
|
||||
return "X-XSS-Protection", true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
type gzipResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
w *gzip.Writer
|
||||
}
|
||||
|
||||
func (g *gzipResponseWriter) Write(b []byte) (int, error) { return g.w.Write(b) }
|
||||
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (s *statusRecorder) WriteHeader(code int) {
|
||||
s.status = code
|
||||
s.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, code int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(code)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func collectPageForms(page map[string]any, forms map[string]any) {
|
||||
if page == nil {
|
||||
return
|
||||
}
|
||||
if obj, ok := page["forms"].(map[string]any); ok {
|
||||
for k, v := range obj {
|
||||
forms[k] = v
|
||||
}
|
||||
}
|
||||
if id, ok := page["form_id"].(string); ok && id != "" {
|
||||
if f := page["form"]; f != nil {
|
||||
forms[id] = f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func injectParams(page map[string]any, params map[string]any) {
|
||||
page["params"] = params
|
||||
if parts, ok := page["parts"].(map[string]any); ok {
|
||||
for k, cfg := range parts {
|
||||
if m, ok := cfg.(map[string]any); ok {
|
||||
cp := cloneMap(m)
|
||||
cp["params"] = params
|
||||
parts[k] = cp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func pathParamNames(pattern string) []string {
|
||||
var names []string
|
||||
for _, seg := range strings.Split(pattern, "/") {
|
||||
if strings.HasPrefix(seg, "{") && strings.HasSuffix(seg, "}") {
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(seg, "{"), "}")
|
||||
name = strings.TrimPrefix(name, "*")
|
||||
if name != "" && name != "id" {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func dbURLOf(appBag map[string]any) string {
|
||||
if dbObj, ok := appBag["db"].(map[string]any); ok {
|
||||
if s, ok := dbObj["url"].(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
if s, ok := appBag["db_url"].(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func resolvePath(p, entryDir string) string {
|
||||
if filepath.IsAbs(p) {
|
||||
return p
|
||||
}
|
||||
base := entryDir
|
||||
if base == "" {
|
||||
base, _ = os.Getwd()
|
||||
}
|
||||
return filepath.Join(base, p)
|
||||
}
|
||||
|
||||
func strOpt(m map[string]any, key, def string) string {
|
||||
if s, ok := m[key].(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func intFrom(v any, def int) int {
|
||||
switch t := v.(type) {
|
||||
case float64:
|
||||
return int(t)
|
||||
case int:
|
||||
return t
|
||||
case int64:
|
||||
return int(t)
|
||||
case string:
|
||||
var n int
|
||||
if _, err := fmt.Sscanf(t, "%d", &n); err == nil {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func cloneMap(m map[string]any) map[string]any {
|
||||
out := make(map[string]any, len(m))
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]any) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
for i := 0; i < len(keys); i++ {
|
||||
for j := i + 1; j < len(keys); j++ {
|
||||
if keys[j] < keys[i] {
|
||||
keys[i], keys[j] = keys[j], keys[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func unique(ss []string) []string {
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, s := range ss {
|
||||
if !seen[s] {
|
||||
seen[s] = true
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package httpx_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/httpx"
|
||||
)
|
||||
|
||||
func TestNewHandlerHomeRoutesStatic(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pub := filepath.Join(dir, "public")
|
||||
if err := os.MkdirAll(pub, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(pub, "hi.txt"), []byte("hello-static"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
page := map[string]any{"title": "Home", "intro": "<h1>Home</h1>"}
|
||||
a := app.New(map[string]any{"page": page})
|
||||
a, err := app.Static(a, pub, "/static")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
about := map[string]any{"title": "About", "intro": "<h1>About</h1>", "_route": "/about"}
|
||||
a, err = app.Route(a, "/about", about)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h, err := httpx.NewHandler(a, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Home") {
|
||||
t.Fatalf("home code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/static/hi.txt", nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("static code=%d", rec.Code)
|
||||
}
|
||||
b, _ := io.ReadAll(rec.Result().Body)
|
||||
if string(b) != "hello-static" {
|
||||
// httptest body is already in rec.Body
|
||||
if rec.Body.String() != "hello-static" {
|
||||
t.Fatalf("static body=%q", rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
// Package middleware ports declarative CORS/security/compress/JSON configure
|
||||
// from the Rust web plugin (配置即数据、装配即函数).
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/table"
|
||||
)
|
||||
|
||||
// Config is the runtime view of app["middleware"].
|
||||
type Config struct {
|
||||
CORS *CORSConfig
|
||||
Security [][2]string
|
||||
Compress bool
|
||||
BodyLimit *uint64
|
||||
AccessLog bool
|
||||
CacheControl string
|
||||
JSONRoutes []JSONRouteMount
|
||||
}
|
||||
|
||||
// CORSConfig matches middleware.cors map shape.
|
||||
type CORSConfig struct {
|
||||
AllowOrigins []string
|
||||
Methods []string
|
||||
Headers []string
|
||||
ExposeHeaders []string
|
||||
Credentials bool
|
||||
}
|
||||
|
||||
// JSONRouteMount is one JSON API route with leading-slash path.
|
||||
type JSONRouteMount struct {
|
||||
Path string
|
||||
Method string
|
||||
Table string
|
||||
Where any
|
||||
Order string
|
||||
Limit int64
|
||||
}
|
||||
|
||||
// Configure merges configure tables into app.middleware (Rust web_app_middleware).
|
||||
func Configure(appBag map[string]any, args map[string]any) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
mw, _ := out["middleware"].(map[string]any)
|
||||
if mw == nil {
|
||||
mw = map[string]any{}
|
||||
} else {
|
||||
mw = clone(mw)
|
||||
}
|
||||
|
||||
if v, ok := args["cors"]; ok && v != nil {
|
||||
mw["cors"] = CorsFromTable(v)
|
||||
}
|
||||
if v, ok := args["security"]; ok && v != nil {
|
||||
mw["security"] = SecurityFromTable(v)
|
||||
}
|
||||
if v, ok := args["compress"]; ok && v != nil {
|
||||
mw["compress"] = boolish(v)
|
||||
}
|
||||
if v, ok := first(args, "access_log", "访问日志"); ok {
|
||||
mw["access_log"] = boolish(v)
|
||||
}
|
||||
if v, ok := first(args, "cache_control", "缓存控制"); ok {
|
||||
if s := strings.TrimSpace(cellText(v)); s != "" {
|
||||
mw["cache_control"] = s
|
||||
}
|
||||
}
|
||||
if v, ok := args["body_limit"]; ok && v != nil {
|
||||
if n, ok := asUint64(v); ok && n > 0 {
|
||||
mw["body_limit"] = float64(n)
|
||||
}
|
||||
}
|
||||
if v, ok := first(args, "json_routes", "json"); ok && v != nil {
|
||||
mw["json_routes"] = JSONRoutesFromTable(v)
|
||||
}
|
||||
out["middleware"] = mw
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Parse reads app["middleware"] into Config (Rust middleware::parse).
|
||||
func Parse(app map[string]any) Config {
|
||||
var cfg Config
|
||||
obj, _ := app["middleware"].(map[string]any)
|
||||
if obj == nil {
|
||||
return cfg
|
||||
}
|
||||
if c, ok := obj["cors"].(map[string]any); ok {
|
||||
cfg.CORS = parseCORS(c)
|
||||
}
|
||||
if sec, ok := obj["security"].(map[string]any); ok {
|
||||
for k, v := range sec {
|
||||
s := cellText(v)
|
||||
if s != "" {
|
||||
cfg.Security = append(cfg.Security, [2]string{k, s})
|
||||
}
|
||||
}
|
||||
}
|
||||
cfg.Compress = boolish(obj["compress"])
|
||||
cfg.AccessLog = boolish(obj["access_log"])
|
||||
if s := strings.TrimSpace(cellText(obj["cache_control"])); s != "" {
|
||||
cfg.CacheControl = s
|
||||
}
|
||||
if n, ok := asUint64(obj["body_limit"]); ok && n > 0 {
|
||||
cfg.BodyLimit = &n
|
||||
}
|
||||
if routes, ok := obj["json_routes"].(map[string]any); ok {
|
||||
for path, spec := range routes {
|
||||
if r, ok := parseJSONRoute(path, spec); ok {
|
||||
cfg.JSONRoutes = append(cfg.JSONRoutes, r)
|
||||
}
|
||||
}
|
||||
sortJSONRoutes(cfg.JSONRoutes)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func parseCORS(obj map[string]any) *CORSConfig {
|
||||
return &CORSConfig{
|
||||
AllowOrigins: strList(obj["allow_origins"]),
|
||||
Methods: strList(obj["methods"]),
|
||||
Headers: strList(obj["headers"]),
|
||||
ExposeHeaders: strList(obj["expose_headers"]),
|
||||
Credentials: boolish(obj["credentials"]),
|
||||
}
|
||||
}
|
||||
|
||||
func parseJSONRoute(path string, spec any) (JSONRouteMount, bool) {
|
||||
m, ok := spec.(map[string]any)
|
||||
if !ok {
|
||||
return JSONRouteMount{}, false
|
||||
}
|
||||
tableName := strings.TrimSpace(cellText(m["table"]))
|
||||
if tableName == "" {
|
||||
return JSONRouteMount{}, false
|
||||
}
|
||||
method := strings.ToUpper(strings.TrimSpace(cellText(m["method"])))
|
||||
if method == "" {
|
||||
method = "GET"
|
||||
}
|
||||
limit := int64(200)
|
||||
if n, ok := asInt64(m["limit"]); ok {
|
||||
limit = n
|
||||
}
|
||||
order := strings.TrimSpace(cellText(m["order"]))
|
||||
p := "/" + strings.TrimPrefix(strings.TrimSpace(path), "/")
|
||||
r := JSONRouteMount{
|
||||
Path: p,
|
||||
Method: method,
|
||||
Table: tableName,
|
||||
Order: order,
|
||||
Limit: limit,
|
||||
}
|
||||
if w, ok := m["where"]; ok && w != nil {
|
||||
r.Where = w
|
||||
}
|
||||
return r, true
|
||||
}
|
||||
|
||||
func sortJSONRoutes(rs []JSONRouteMount) {
|
||||
for i := 0; i < len(rs); i++ {
|
||||
for j := i + 1; j < len(rs); j++ {
|
||||
if rs[j].Path < rs[i].Path {
|
||||
rs[i], rs[j] = rs[j], rs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CorsFromTable normalizes `|允许来源|方法|头|暴露头|凭证|` into middleware.cors.
|
||||
func CorsFromTable(tableV any) map[string]any {
|
||||
rows, _ := table.AsRows(tableV).([]any)
|
||||
var allowOrigins, methods, headers, expose []string
|
||||
credentials := false
|
||||
for _, row := range rows {
|
||||
m, ok := row.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
origin := col(m, "允许来源", "origin", "Origin")
|
||||
if origin != "" && origin != "*" {
|
||||
allowOrigins = append(allowOrigins, origin)
|
||||
}
|
||||
for _, part := range strings.Split(col(m, "方法", "methods", "Methods"), ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
methods = append(methods, part)
|
||||
}
|
||||
}
|
||||
for _, part := range strings.Split(col(m, "头", "headers", "Headers"), ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
headers = append(headers, part)
|
||||
}
|
||||
}
|
||||
for _, part := range strings.Split(col(m, "暴露头", "expose_headers", "Expose-Headers"), ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
expose = append(expose, part)
|
||||
}
|
||||
}
|
||||
cred := col(m, "凭证", "credentials", "Credentials")
|
||||
if strings.EqualFold(cred, "true") || cred == "1" || strings.EqualFold(cred, "yes") {
|
||||
credentials = true
|
||||
}
|
||||
}
|
||||
return map[string]any{
|
||||
"allow_origins": toAnyList(allowOrigins),
|
||||
"methods": toAnyList(methods),
|
||||
"headers": toAnyList(headers),
|
||||
"expose_headers": toAnyList(expose),
|
||||
"credentials": credentials,
|
||||
}
|
||||
}
|
||||
|
||||
// SecurityFromTable normalizes `|头|值|` into middleware.security map.
|
||||
func SecurityFromTable(tableV any) map[string]any {
|
||||
rows, _ := table.AsRows(tableV).([]any)
|
||||
out := map[string]any{}
|
||||
for _, row := range rows {
|
||||
m, ok := row.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
k := col(m, "头", "header", "Header", "name")
|
||||
v := col(m, "值", "value", "Value")
|
||||
if k != "" {
|
||||
out[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// JSONRoutesFromTable normalizes `|路径|方法|表|条件|排序|上限|` into json_routes.
|
||||
func JSONRoutesFromTable(tableV any) map[string]any {
|
||||
rows, _ := table.AsRows(tableV).([]any)
|
||||
out := map[string]any{}
|
||||
for _, row := range rows {
|
||||
m, ok := row.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
path := strings.TrimPrefix(strings.TrimSpace(col(m, "路径", "path", "Path")), "/")
|
||||
if path == "" {
|
||||
continue
|
||||
}
|
||||
method := strings.ToUpper(strings.TrimSpace(col(m, "方法", "method", "Method")))
|
||||
if method == "" {
|
||||
method = "GET"
|
||||
}
|
||||
tbl := col(m, "表", "table", "Table")
|
||||
if tbl == "" {
|
||||
continue
|
||||
}
|
||||
order := col(m, "排序", "order", "Order")
|
||||
limit := int64(200)
|
||||
if s := col(m, "上限", "limit", "Limit"); s != "" {
|
||||
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
spec := map[string]any{
|
||||
"method": method,
|
||||
"table": tbl,
|
||||
"limit": float64(limit),
|
||||
}
|
||||
if order != "" {
|
||||
spec["order"] = order
|
||||
}
|
||||
out[path] = spec
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func col(m map[string]any, keys ...string) string {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
return cellText(v)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func cellText(v any) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return t
|
||||
case bool:
|
||||
return strconv.FormatBool(t)
|
||||
case float64:
|
||||
if t == float64(int64(t)) {
|
||||
return strconv.FormatInt(int64(t), 10)
|
||||
}
|
||||
return strconv.FormatFloat(t, 'f', -1, 64)
|
||||
case int:
|
||||
return strconv.Itoa(t)
|
||||
case int64:
|
||||
return strconv.FormatInt(t, 10)
|
||||
default:
|
||||
return fmt.Sprint(t)
|
||||
}
|
||||
}
|
||||
|
||||
func boolish(v any) bool {
|
||||
switch t := v.(type) {
|
||||
case bool:
|
||||
return t
|
||||
case string:
|
||||
switch strings.TrimSpace(t) {
|
||||
case "true", "True", "1", "yes", "on", "真":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case float64:
|
||||
return t != 0
|
||||
case int:
|
||||
return t != 0
|
||||
case int64:
|
||||
return t != 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func asUint64(v any) (uint64, bool) {
|
||||
switch t := v.(type) {
|
||||
case float64:
|
||||
if t > 0 {
|
||||
return uint64(t), true
|
||||
}
|
||||
case int64:
|
||||
if t > 0 {
|
||||
return uint64(t), true
|
||||
}
|
||||
case int:
|
||||
if t > 0 {
|
||||
return uint64(t), true
|
||||
}
|
||||
case string:
|
||||
n, err := strconv.ParseUint(strings.TrimSpace(t), 10, 64)
|
||||
return n, err == nil && n > 0
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func asInt64(v any) (int64, bool) {
|
||||
switch t := v.(type) {
|
||||
case float64:
|
||||
return int64(t), true
|
||||
case int64:
|
||||
return t, true
|
||||
case int:
|
||||
return int64(t), true
|
||||
case string:
|
||||
n, err := strconv.ParseInt(strings.TrimSpace(t), 10, 64)
|
||||
return n, err == nil
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func strList(v any) []string {
|
||||
switch t := v.(type) {
|
||||
case []any:
|
||||
out := make([]string, 0, len(t))
|
||||
for _, x := range t {
|
||||
if s, ok := x.(string); ok {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
case []string:
|
||||
return append([]string{}, t...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func toAnyList(ss []string) []any {
|
||||
out := make([]any, len(ss))
|
||||
for i, s := range ss {
|
||||
out[i] = s
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func first(args map[string]any, keys ...string) (any, bool) {
|
||||
for _, k := range keys {
|
||||
if v, ok := args[k]; ok && v != nil {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func clone(m map[string]any) map[string]any {
|
||||
out := make(map[string]any, len(m))
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
)
|
||||
|
||||
func TestConfigureCORSOrigins(t *testing.T) {
|
||||
cors := map[string]any{
|
||||
"允许来源": []any{"https://a.example", "https://b.example"},
|
||||
"方法": []any{"GET,POST", "GET"},
|
||||
"头": []any{"Content-Type,Authorization", "Content-Type"},
|
||||
"暴露头": []any{"X-Total", ""},
|
||||
"凭证": []any{"true", ""},
|
||||
}
|
||||
sec := map[string]any{
|
||||
"头": []any{"X-Frame-Options", "X-Content-Type-Options", "Content-Security-Policy"},
|
||||
"值": []any{"DENY", "nosniff", "default-src 'self'"},
|
||||
}
|
||||
api := map[string]any{
|
||||
"路径": []any{"/api/posts", "/api/publish"},
|
||||
"方法": []any{"GET", "POST"},
|
||||
"表": []any{"articles", "articles"},
|
||||
"条件": []any{"", ""},
|
||||
"排序": []any{"-created_at", ""},
|
||||
"上限": []any{"20", "10"},
|
||||
}
|
||||
|
||||
a := app.New(map[string]any{"page": map[string]any{"title": "mw"}})
|
||||
out, err := middleware.Configure(a, map[string]any{
|
||||
"cors": cors,
|
||||
"security": sec,
|
||||
"compress": true,
|
||||
"body_limit": float64(1048576),
|
||||
"json": api,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mw, _ := out["middleware"].(map[string]any)
|
||||
if mw == nil {
|
||||
t.Fatal("missing middleware")
|
||||
}
|
||||
cobj, _ := mw["cors"].(map[string]any)
|
||||
origins, _ := cobj["allow_origins"].([]any)
|
||||
if len(origins) < 1 || origins[0] != "https://a.example" {
|
||||
t.Fatalf("allow_origins=%v", origins)
|
||||
}
|
||||
methods, _ := cobj["methods"].([]any)
|
||||
if len(methods) < 2 || methods[1] != "POST" {
|
||||
t.Fatalf("methods=%v", methods)
|
||||
}
|
||||
expose, _ := cobj["expose_headers"].([]any)
|
||||
if len(expose) < 1 || expose[0] != "X-Total" {
|
||||
t.Fatalf("expose=%v", expose)
|
||||
}
|
||||
if cred, _ := cobj["credentials"].(bool); !cred {
|
||||
t.Fatal("credentials want true")
|
||||
}
|
||||
|
||||
secmap, _ := mw["security"].(map[string]any)
|
||||
if secmap["X-Frame-Options"] != "DENY" {
|
||||
t.Fatalf("security frame=%v", secmap["X-Frame-Options"])
|
||||
}
|
||||
if mw["compress"] != true {
|
||||
t.Fatalf("compress=%v", mw["compress"])
|
||||
}
|
||||
if mw["body_limit"] != float64(1048576) {
|
||||
t.Fatalf("body_limit=%v", mw["body_limit"])
|
||||
}
|
||||
jmap, _ := mw["json_routes"].(map[string]any)
|
||||
posts, _ := jmap["api/posts"].(map[string]any)
|
||||
if posts["method"] != "GET" || posts["table"] != "articles" {
|
||||
t.Fatalf("posts=%v", posts)
|
||||
}
|
||||
if posts["order"] != "-created_at" || posts["limit"] != float64(20) {
|
||||
t.Fatalf("posts order/limit=%v", posts)
|
||||
}
|
||||
pub, _ := jmap["api/publish"].(map[string]any)
|
||||
if pub["method"] != "POST" {
|
||||
t.Fatalf("publish=%v", pub)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReadsBag(t *testing.T) {
|
||||
a := map[string]any{
|
||||
"middleware": map[string]any{
|
||||
"cors": map[string]any{
|
||||
"allow_origins": []any{"https://a.example"},
|
||||
"methods": []any{"GET"},
|
||||
"credentials": true,
|
||||
},
|
||||
"compress": true,
|
||||
"body_limit": float64(1000),
|
||||
"json_routes": map[string]any{
|
||||
"api/posts": map[string]any{"method": "GET", "table": "articles", "limit": float64(10)},
|
||||
},
|
||||
},
|
||||
}
|
||||
cfg := middleware.Parse(a)
|
||||
if cfg.CORS == nil || len(cfg.CORS.AllowOrigins) != 1 {
|
||||
t.Fatalf("cors=%v", cfg.CORS)
|
||||
}
|
||||
if !cfg.Compress || cfg.BodyLimit == nil || *cfg.BodyLimit != 1000 {
|
||||
t.Fatalf("cfg=%+v", cfg)
|
||||
}
|
||||
if len(cfg.JSONRoutes) != 1 || cfg.JSONRoutes[0].Path != "/api/posts" {
|
||||
t.Fatalf("json routes=%v", cfg.JSONRoutes)
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,9 @@ extern int web_form_render(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_form_submit(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_form_from_schema(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_mount_form(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_static(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_middleware(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_listen(char *args_json, char **out_json, char **err_msg);
|
||||
|
||||
static int register_core(void) {
|
||||
if (host_register((char *)"web_go_ready", (char *)"", web_go_ready) != 0) return 1;
|
||||
@@ -117,6 +120,9 @@ static int register_core(void) {
|
||||
if (host_register((char *)"web_app_new", (char *)"page,db,admin,host,port,admin_prefix,login_redirect,logout_redirect,shell_css,layout,asset_version", web_app_new) != 0) return 1;
|
||||
if (host_register((char *)"web_app_route", (char *)"app,path,page", web_app_route) != 0) return 1;
|
||||
if (host_register((char *)"web_app_mount_form", (char *)"app,id,form", web_app_mount_form) != 0) return 1;
|
||||
if (host_register((char *)"web_app_static", (char *)"app,dir,mount", web_app_static) != 0) return 1;
|
||||
if (host_register((char *)"web_app_middleware", (char *)"app,cors,security,compress,body_limit,json_routes,access_log,cache_control,proxy,invoke", web_app_middleware) != 0) return 1;
|
||||
if (host_register((char *)"web_listen", (char *)"app", web_listen) != 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user