From cbe209643c75230b8ef85abfce87d468c64bf239 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 9 Aug 2023 19:15:47 +0200 Subject: [PATCH] routing and calling RCell handler basically working --- server/config.go | 11 +++++------ server/server.go | 26 +++++++++++++++++++++++--- tests/etc/etc.go | 5 +++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/server/config.go b/server/config.go index c9863e5..7aa5397 100644 --- a/server/config.go +++ b/server/config.go @@ -14,7 +14,7 @@ type Cfg struct { routes []routeCfg } -func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) routeSpec { +func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) { rcfg := routeCfg{ methods: methods, path: path, @@ -24,7 +24,6 @@ func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) routeSpec rcfg.methods = []string{"GET"} } c.routes = append(c.routes, rcfg) - return spec } type routeCfg struct { @@ -39,16 +38,16 @@ type fsSpec struct { docRoot string } -func FileServer(docRoot string) routeSpec { +func FileServer(docRoot string) *fsSpec { return &fsSpec{docRoot} } -type RCellSpec struct { +type rcellSpec struct { lib.Config } -func RCellHandler() routeSpec { - return &RCellSpec{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 84d186b..ced1e76 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "net/http" lib "git.sr.ht/~cco/go-scopes" @@ -28,9 +29,13 @@ func Serve(ctx lib.Context) { r := gin.New() r.Use(gin.Recovery()) r.Use(Logger(ctx)) - r.GET("/*action", func(c *gin.Context) { + /*r.GET("/*action", func(c *gin.Context) { c.String(http.StatusOK, "Hello World") - }) + })*/ + cfg := ctx.Config().(*Cfg) + for _, rcfg := range cfg.routes { + setRoute(ctx, rcfg, r) + } srv := &http.Server{ Addr: ":8123", Handler: r, @@ -43,4 +48,19 @@ func Serve(ctx lib.Context) { }) } -// route handlers +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) + }) + } +} + +// RCell (request / response handler cell) implementation + +func RCell(ctx lib.Context, spec *rcellSpec, gc *gin.Context) { + fmt.Println("*** RCell called") +} diff --git a/tests/etc/etc.go b/tests/etc/etc.go index c77b5ed..51348b2 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -32,12 +32,13 @@ func Config() lib.Config { Port: ovr("8123", SERVER_PORT), } server_c.AddRoute("/docs", server.FileServer("html")) - server_rc := server_c.AddRoute("/api", server.RCellHandler()).(*server.RCellSpec) + server_rc := server.RCellHandler() server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver")) + server_c.AddRoute("/api/*msg", server_rc) test_client := &client.Cfg{ BaseCfg: b("test-client", core.Start), - Url: ovr("http://localhost:8123", SERVER_URL), + Url: ovr("http://localhost:8123/api", SERVER_URL), } test_client.AddAction("demo", action.Base(client.Send))