server: fix message / action handling

This commit is contained in:
Helmut Merz 2023-08-14 09:51:36 +02:00
parent 049998d5ec
commit 32acd23f26
3 changed files with 27 additions and 11 deletions

View file

@ -42,19 +42,27 @@ func FileServer(docRoot string) *fsSpec {
return &fsSpec{docRoot} return &fsSpec{docRoot}
} }
type mhSpec struct { type actionSpec struct {
lib.Config *action.BaseSpec
proc msgProc 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 { if proc == nil {
proc = Async proc = Async
} }
//cfg := mhSpec{config.Dummy(), proc} spec.AddAction(pattern, &actionSpec{
cfg := mhSpec{config.Default(""), proc} action.Base(action.Forward, receivers...), proc})
cfg.AddAction(pattern, action.Base(action.Forward, rcvs...)) return spec
return &cfg }
func MsgHandler() *mhSpec {
return &mhSpec{config.Default("")}
} }
type msgProc func(lib.Context, lib.Message) (int, lib.Data) type msgProc func(lib.Context, lib.Message) (int, lib.Data)

View file

@ -8,6 +8,7 @@ import (
lib "git.sr.ht/~cco/go-scopes" lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core" "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/core/message"
"git.sr.ht/~cco/go-scopes/logging" "git.sr.ht/~cco/go-scopes/logging"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -70,8 +71,13 @@ func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) {
cctx := ctx.ChildContext(cfg) cctx := ctx.ChildContext(cfg)
msg := message.New(head...).WithSender(cctx) msg := message.New(head...).WithSender(cctx)
// if gc.Request.Method == "POST" && data != "": msg.WithPayload(data) // if gc.Request.Method == "POST" && data != "": msg.WithPayload(data)
core.HandleMessage(cctx, msg) logging.DebugM(cctx, msg).Msg("server.handleMessage")
code, data := cfg.proc(cctx, msg) 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) gc.JSON(code, data)
} }

View file

@ -26,11 +26,13 @@ func Config() lib.Config {
} }
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver")) 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_c := b("server", server.Start(
(&server.Cfg{Port: ovr("8123", SERVER_PORT)}). (&server.Cfg{Port: ovr("8123", SERVER_PORT)}).
AddRoute("/docs", server.FileServer("html")). 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{ test_client := &client.Cfg{
BaseCfg: b("test-client", core.Start). BaseCfg: b("test-client", core.Start).