From 32acd23f2663be48b1cfc08a6e2011d08a91c3b6 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 14 Aug 2023 09:51:36 +0200 Subject: [PATCH] server: fix message / action handling --- server/config.go | 22 +++++++++++++++------- server/server.go | 10 ++++++++-- tests/etc/etc.go | 6 ++++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/server/config.go b/server/config.go index 829d024..f2874d2 100644 --- a/server/config.go +++ b/server/config.go @@ -42,19 +42,27 @@ func FileServer(docRoot string) *fsSpec { return &fsSpec{docRoot} } -type mhSpec struct { - lib.Config +type actionSpec struct { + *action.BaseSpec proc msgProc } -func MsgHandler(pattern string, proc msgProc, rcvs ...string) *mhSpec { +type mhSpec struct { + lib.Config +} + +func (spec *mhSpec) AddActionProc( + pattern string, proc msgProc, receivers ...string) *mhSpec { if proc == nil { proc = Async } - //cfg := mhSpec{config.Dummy(), proc} - cfg := mhSpec{config.Default(""), proc} - cfg.AddAction(pattern, action.Base(action.Forward, rcvs...)) - return &cfg + spec.AddAction(pattern, &actionSpec{ + action.Base(action.Forward, receivers...), proc}) + return spec +} + +func MsgHandler() *mhSpec { + return &mhSpec{config.Default("")} } type msgProc func(lib.Context, lib.Message) (int, lib.Data) diff --git a/server/server.go b/server/server.go index e34cfdd..515da85 100644 --- a/server/server.go +++ b/server/server.go @@ -8,6 +8,7 @@ import ( lib "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/core" + "git.sr.ht/~cco/go-scopes/core/action" "git.sr.ht/~cco/go-scopes/core/message" "git.sr.ht/~cco/go-scopes/logging" "github.com/gin-gonic/gin" @@ -70,8 +71,13 @@ func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) { cctx := ctx.ChildContext(cfg) msg := message.New(head...).WithSender(cctx) // if gc.Request.Method == "POST" && data != "": msg.WithPayload(data) - core.HandleMessage(cctx, msg) - code, data := cfg.proc(cctx, msg) + logging.DebugM(cctx, msg).Msg("server.handleMessage") + var proc msgProc + for _, act := range action.Select(cctx, msg) { + act.Handle() + proc = act.Spec().(*actionSpec).proc + } + code, data := proc(cctx, msg) gc.JSON(code, data) } diff --git a/tests/etc/etc.go b/tests/etc/etc.go index b387fde..71cb82e 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -26,11 +26,13 @@ func Config() lib.Config { } app_c.AddAction("demo", action.Base(action.Forward, "test-receiver")) + //server_amh := server.MsgHandler() + //server_amh.AddActionProc("demo", server.Async, "test-receiver") server_c := b("server", server.Start( (&server.Cfg{Port: ovr("8123", SERVER_PORT)}). AddRoute("/docs", server.FileServer("html")). - AddRoute("/api", server.MsgHandler("demo", server.Async, "test-receiver")), - )) + AddRoute("/api", server.MsgHandler(). + AddActionProc("demo", server.Async, "test-receiver")))) test_client := &client.Cfg{ BaseCfg: b("test-client", core.Start).