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 {
 | 
			
		||||
	*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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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).
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue