server: put specific config in Start() (= factory) call
This commit is contained in:
parent
a8e266e039
commit
efb09c9019
3 changed files with 21 additions and 15 deletions
|
@ -7,13 +7,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
*config.BaseCfg
|
//*config.BaseCfg
|
||||||
Port string
|
Port string
|
||||||
Addr string
|
Addr string
|
||||||
routes []routeCfg
|
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{
|
rcfg := routeCfg{
|
||||||
methods: methods,
|
methods: methods,
|
||||||
path: path,
|
path: path,
|
||||||
|
@ -23,6 +23,7 @@ func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) {
|
||||||
rcfg.methods = []string{"GET", "POST"}
|
rcfg.methods = []string{"GET", "POST"}
|
||||||
}
|
}
|
||||||
c.routes = append(c.routes, rcfg)
|
c.routes = append(c.routes, rcfg)
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
type routeCfg struct {
|
type routeCfg struct {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
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/core"
|
"git.sr.ht/~cco/go-scopes/core"
|
||||||
"git.sr.ht/~cco/go-scopes/core/message"
|
"git.sr.ht/~cco/go-scopes/core/message"
|
||||||
"git.sr.ht/~cco/go-scopes/logging"
|
"git.sr.ht/~cco/go-scopes/logging"
|
||||||
|
@ -17,11 +18,13 @@ type ServerState struct {
|
||||||
server *http.Server
|
server *http.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(ctx lib.Context) {
|
func Start(cfg *Cfg) lib.Proc {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
return func(ctx lib.Context) {
|
||||||
lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
Serve(ctx)
|
lib.GetCfg[*config.BaseCfg](ctx).WithDoneHandler(HandleDone)
|
||||||
lib.RunCtx(ctx, core.Listen)
|
Serve(ctx, cfg)
|
||||||
|
lib.RunCtx(ctx, core.Listen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleDone(ctx lib.Context) bool {
|
func HandleDone(ctx lib.Context) bool {
|
||||||
|
@ -29,11 +32,10 @@ func HandleDone(ctx lib.Context) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func Serve(ctx lib.Context) {
|
func Serve(ctx lib.Context, cfg *Cfg) {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
r.Use(Logger(ctx))
|
r.Use(Logger(ctx))
|
||||||
cfg := ctx.Config().(*Cfg)
|
|
||||||
if cfg.Addr == "" {
|
if cfg.Addr == "" {
|
||||||
if cfg.Port == "" {
|
if cfg.Port == "" {
|
||||||
cfg.Port = "8123"
|
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) {
|
func Async(ctx lib.Context, msg lib.Message) (int, Data) {
|
||||||
return http.StatusOK, Data{"status": "OK"}
|
return http.StatusOK, Data{"status": "OK"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Sync(ctx lib.Context, msg lib.Message) (int, Data) {
|
||||||
|
return http.StatusOK, Data{"status": "OK"}
|
||||||
|
}
|
||||||
|
|
|
@ -27,12 +27,11 @@ func Config() lib.Config {
|
||||||
}
|
}
|
||||||
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
||||||
|
|
||||||
server_c := &server.Cfg{
|
server_c := b("server", server.Start(
|
||||||
BaseCfg: b("server", server.Start),
|
(&server.Cfg{Port: ovr("8123", SERVER_PORT)}).
|
||||||
Port: ovr("8123", SERVER_PORT),
|
AddRoute("/docs", server.FileServer("html")).
|
||||||
}
|
AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver")),
|
||||||
server_c.AddRoute("/docs", server.FileServer("html"))
|
))
|
||||||
server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver"))
|
|
||||||
|
|
||||||
test_client := &client.Cfg{
|
test_client := &client.Cfg{
|
||||||
BaseCfg: b("test-client", core.Start).
|
BaseCfg: b("test-client", core.Start).
|
||||||
|
|
Loading…
Add table
Reference in a new issue