routing and calling RCell handler basically working

This commit is contained in:
Helmut Merz 2023-08-09 19:15:47 +02:00
parent 10908bbd61
commit cbe209643c
3 changed files with 31 additions and 11 deletions

View file

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

View file

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

View file

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