server, work in progress: handling of remote (request) messages

This commit is contained in:
Helmut Merz 2023-08-10 09:36:12 +02:00
parent 7c3c88cfc2
commit 399b3576b0
4 changed files with 21 additions and 23 deletions

View file

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

View file

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

View file

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

View file

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