From 10908bbd61a22b741dd5a9ce942cf02f8ca7e091 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 9 Aug 2023 17:06:22 +0200 Subject: [PATCH] server: first basic route config --- server/config.go | 24 +++++++++++++++++++++++- tests/etc/etc.go | 6 +++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/server/config.go b/server/config.go index a1c476d..c9863e5 100644 --- a/server/config.go +++ b/server/config.go @@ -3,6 +3,7 @@ package server 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" ) @@ -13,6 +14,19 @@ type Cfg struct { 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 { methods []string path string @@ -25,8 +39,16 @@ type fsSpec struct { docRoot string } -type rcellSpec struct { +func FileServer(docRoot string) routeSpec { + return &fsSpec{docRoot} +} + +type RCellSpec struct { lib.Config } +func RCellHandler() routeSpec { + return &RCellSpec{config.Base("", core.Start)} +} + type Handler func(lib.Context, *gin.Context) diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 5484273..c77b5ed 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -31,9 +31,9 @@ func Config() lib.Config { BaseCfg: b("server", server.Start), Port: ovr("8123", SERVER_PORT), } - // server_c.AddRoute("/docs", FileServer("html")) - // server_rc := server_c.AddRoute("/api", RCellHandler()) - // server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver")) + server_c.AddRoute("/docs", server.FileServer("html")) + server_rc := server_c.AddRoute("/api", server.RCellHandler()).(*server.RCellSpec) + server_rc.AddAction("demo", action.Base(action.Forward, "test-receiver")) test_client := &client.Cfg{ BaseCfg: b("test-client", core.Start),