server: first basic route config

This commit is contained in:
Helmut Merz 2023-08-09 17:06:22 +02:00
parent ca08bbb9c7
commit 10908bbd61
2 changed files with 26 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package server
import ( import (
lib "git.sr.ht/~cco/go-scopes" lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -13,6 +14,19 @@ type Cfg struct {
routes []routeCfg routes []routeCfg
} }
func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) routeSpec {
rcfg := routeCfg{
methods: methods,
path: path,
spec: spec,
}
if methods == nil {
rcfg.methods = []string{"GET"}
}
c.routes = append(c.routes, rcfg)
return spec
}
type routeCfg struct { type routeCfg struct {
methods []string methods []string
path string path string
@ -25,8 +39,16 @@ type fsSpec struct {
docRoot string docRoot string
} }
type rcellSpec struct { func FileServer(docRoot string) routeSpec {
return &fsSpec{docRoot}
}
type RCellSpec struct {
lib.Config lib.Config
} }
func RCellHandler() routeSpec {
return &RCellSpec{config.Base("", core.Start)}
}
type Handler func(lib.Context, *gin.Context) type Handler func(lib.Context, *gin.Context)

View file

@ -31,9 +31,9 @@ func Config() lib.Config {
BaseCfg: b("server", server.Start), BaseCfg: b("server", server.Start),
Port: ovr("8123", SERVER_PORT), Port: ovr("8123", SERVER_PORT),
} }
// server_c.AddRoute("/docs", FileServer("html")) server_c.AddRoute("/docs", server.FileServer("html"))
// server_rc := server_c.AddRoute("/api", RCellHandler()) server_rc := server_c.AddRoute("/api", server.RCellHandler()).(*server.RCellSpec)
// server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver")) server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver"))
test_client := &client.Cfg{ test_client := &client.Cfg{
BaseCfg: b("test-client", core.Start), BaseCfg: b("test-client", core.Start),