diff --git a/server/config.go b/server/config.go index 095d265..a4bb292 100644 --- a/server/config.go +++ b/server/config.go @@ -7,13 +7,13 @@ import ( ) type Cfg struct { - *config.BaseCfg + //*config.BaseCfg Port string Addr string routes []routeCfg } -func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) { +func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) *Cfg { rcfg := routeCfg{ methods: methods, path: path, @@ -23,6 +23,7 @@ func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) { rcfg.methods = []string{"GET", "POST"} } c.routes = append(c.routes, rcfg) + return c } type routeCfg struct { diff --git a/server/server.go b/server/server.go index f709b30..3bb34db 100644 --- a/server/server.go +++ b/server/server.go @@ -5,6 +5,7 @@ import ( "strings" lib "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/core" "git.sr.ht/~cco/go-scopes/core/message" "git.sr.ht/~cco/go-scopes/logging" @@ -17,11 +18,13 @@ type ServerState struct { server *http.Server } -func Start(ctx lib.Context) { - gin.SetMode(gin.ReleaseMode) - lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone) - Serve(ctx) - lib.RunCtx(ctx, core.Listen) +func Start(cfg *Cfg) lib.Proc { + return func(ctx lib.Context) { + gin.SetMode(gin.ReleaseMode) + lib.GetCfg[*config.BaseCfg](ctx).WithDoneHandler(HandleDone) + Serve(ctx, cfg) + lib.RunCtx(ctx, core.Listen) + } } func HandleDone(ctx lib.Context) bool { @@ -29,11 +32,10 @@ func HandleDone(ctx lib.Context) bool { return false } -func Serve(ctx lib.Context) { +func Serve(ctx lib.Context, cfg *Cfg) { r := gin.New() r.Use(gin.Recovery()) r.Use(Logger(ctx)) - cfg := ctx.Config().(*Cfg) if cfg.Addr == "" { if cfg.Port == "" { cfg.Port = "8123" @@ -77,3 +79,7 @@ func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) { func Async(ctx lib.Context, msg lib.Message) (int, Data) { return http.StatusOK, Data{"status": "OK"} } + +func Sync(ctx lib.Context, msg lib.Message) (int, Data) { + return http.StatusOK, Data{"status": "OK"} +} diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 3465275..6e844be 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -27,12 +27,11 @@ func Config() lib.Config { } app_c.AddAction("demo", action.Base(action.Forward, "test-receiver")) - server_c := &server.Cfg{ - BaseCfg: b("server", server.Start), - Port: ovr("8123", SERVER_PORT), - } - server_c.AddRoute("/docs", server.FileServer("html")) - server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver")) + server_c := b("server", server.Start( + (&server.Cfg{Port: ovr("8123", SERVER_PORT)}). + AddRoute("/docs", server.FileServer("html")). + AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver")), + )) test_client := &client.Cfg{ BaseCfg: b("test-client", core.Start).