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}
}
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)

View file

@ -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)
}

View file

@ -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).