routing and calling RCell handler basically working
This commit is contained in:
parent
10908bbd61
commit
cbe209643c
3 changed files with 31 additions and 11 deletions
|
@ -14,7 +14,7 @@ type Cfg struct {
|
||||||
routes []routeCfg
|
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{
|
rcfg := routeCfg{
|
||||||
methods: methods,
|
methods: methods,
|
||||||
path: path,
|
path: path,
|
||||||
|
@ -24,7 +24,6 @@ func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) routeSpec
|
||||||
rcfg.methods = []string{"GET"}
|
rcfg.methods = []string{"GET"}
|
||||||
}
|
}
|
||||||
c.routes = append(c.routes, rcfg)
|
c.routes = append(c.routes, rcfg)
|
||||||
return spec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type routeCfg struct {
|
type routeCfg struct {
|
||||||
|
@ -39,16 +38,16 @@ type fsSpec struct {
|
||||||
docRoot string
|
docRoot string
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileServer(docRoot string) routeSpec {
|
func FileServer(docRoot string) *fsSpec {
|
||||||
return &fsSpec{docRoot}
|
return &fsSpec{docRoot}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RCellSpec struct {
|
type rcellSpec struct {
|
||||||
lib.Config
|
lib.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func RCellHandler() routeSpec {
|
func RCellHandler() *rcellSpec {
|
||||||
return &RCellSpec{config.Base("", core.Start)}
|
return &rcellSpec{config.Base("", core.Start)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler func(lib.Context, *gin.Context)
|
type Handler func(lib.Context, *gin.Context)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
lib "git.sr.ht/~cco/go-scopes"
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
|
@ -28,9 +29,13 @@ func Serve(ctx lib.Context) {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
r.Use(Logger(ctx))
|
r.Use(Logger(ctx))
|
||||||
r.GET("/*action", func(c *gin.Context) {
|
/*r.GET("/*action", func(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Hello World")
|
c.String(http.StatusOK, "Hello World")
|
||||||
})
|
})*/
|
||||||
|
cfg := ctx.Config().(*Cfg)
|
||||||
|
for _, rcfg := range cfg.routes {
|
||||||
|
setRoute(ctx, rcfg, r)
|
||||||
|
}
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: ":8123",
|
Addr: ":8123",
|
||||||
Handler: r,
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -32,12 +32,13 @@ func Config() lib.Config {
|
||||||
Port: ovr("8123", SERVER_PORT),
|
Port: ovr("8123", SERVER_PORT),
|
||||||
}
|
}
|
||||||
server_c.AddRoute("/docs", server.FileServer("html"))
|
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_rc.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
||||||
|
server_c.AddRoute("/api/*msg", server_rc)
|
||||||
|
|
||||||
test_client := &client.Cfg{
|
test_client := &client.Cfg{
|
||||||
BaseCfg: b("test-client", core.Start),
|
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))
|
test_client.AddAction("demo", action.Base(client.Send))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue