diff --git a/server/config.go b/server/config.go index 7aa5397..da8431e 100644 --- a/server/config.go +++ b/server/config.go @@ -4,7 +4,6 @@ import ( lib "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/core" - "github.com/gin-gonic/gin" ) type Cfg struct { @@ -21,7 +20,7 @@ func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) { spec: spec, } if methods == nil { - rcfg.methods = []string{"GET"} + rcfg.methods = []string{"GET", "POST"} } c.routes = append(c.routes, rcfg) } @@ -42,12 +41,6 @@ func FileServer(docRoot string) *fsSpec { return &fsSpec{docRoot} } -type rcellSpec struct { - lib.Config +func MsgHandler() lib.Config { + return config.Base("", core.Start) } - -func RCellHandler() *rcellSpec { - return &rcellSpec{config.Base("", core.Start)} -} - -type Handler func(lib.Context, *gin.Context) diff --git a/server/server.go b/server/server.go index 42716de..3e0883a 100644 --- a/server/server.go +++ b/server/server.go @@ -1,11 +1,12 @@ package server import ( - "fmt" "net/http" + "strings" lib "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/core" + "git.sr.ht/~cco/go-scopes/core/message" "github.com/gin-gonic/gin" ) @@ -14,7 +15,7 @@ type ServerState struct { } func Start(ctx lib.Context) { - //gin.SetMode(gin.ReleaseMode) + gin.SetMode(gin.ReleaseMode) lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone) Serve(ctx) lib.RunCtx(ctx, core.Listen) @@ -49,17 +50,21 @@ func setRoute(ctx lib.Context, rcfg routeCfg, r *gin.Engine) { switch spec := rcfg.spec.(type) { case *fsSpec: r.Static(rcfg.path, spec.docRoot) - case *rcellSpec: - r.Match(rcfg.methods, rcfg.path, func(c *gin.Context) { - RCell(ctx, spec, c) + case lib.Config: + r.Match(rcfg.methods, rcfg.path+"/*msg", func(c *gin.Context) { + handle(ctx, spec, c) }) } } -// RCell (request / response handler cell) implementation +// scopes standard request (= message) handler implementation -func RCell(ctx lib.Context, spec *rcellSpec, gc *gin.Context) { - msg := gc.Param("msg") - fmt.Printf("*** RCell called: msg: %s\n", msg) +func handle(ctx lib.Context, cfg lib.Config, gc *gin.Context) { + head := strings.Split(gc.Param("msg"), "/")[1:] + msg := message.New(head...) + //fmt.Printf("*** RCell called: msg: %+v, method: %s\n", msg, gc.Request.Method) + cctx := ctx.ChildContext(cfg) + core.HandleMessage(cctx, msg) + // cfg.Starter()(cctx) gc.String(http.StatusOK, "Hello World") } diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 51348b2..03f5034 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -32,9 +32,9 @@ func Config() lib.Config { Port: ovr("8123", SERVER_PORT), } server_c.AddRoute("/docs", server.FileServer("html")) - server_rc := server.RCellHandler() - server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver")) - server_c.AddRoute("/api/*msg", server_rc) + server_mh := server.MsgHandler() + server_mh.AddAction("demo", action.Base(action.Forward, "test-receiver")) + server_c.AddRoute("/api", server_mh) test_client := &client.Cfg{ BaseCfg: b("test-client", core.Start), diff --git a/tests/scopes_test.go b/tests/scopes_test.go index f5e0fd7..51d8de3 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -19,7 +19,7 @@ func TestScopesApp(tb *tbase.T) { t.Run("send", SendTest) t.Run("client", ClientTest) t.TearDownApp("") - t.AssertEqual(t.LogCheck("", true), 12) + t.AssertEqual(t.LogCheck("", true), 16) } func AppTest(t *testing.T) {