server: provide message processor for fine-tuned handling of remote messages - start with Async()
This commit is contained in:
		
							parent
							
								
									d1732dd851
								
							
						
					
					
						commit
						1d7d1f3b1b
					
				
					 3 changed files with 27 additions and 11 deletions
				
			
		|  | @ -41,8 +41,18 @@ func FileServer(docRoot string) *fsSpec { | ||||||
| 	return &fsSpec{docRoot} | 	return &fsSpec{docRoot} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func MsgHandler(pattern string, rcvs ...string) lib.Config { | type mhSpec struct { | ||||||
| 	cfg := config.Base("", nil) | 	lib.Config | ||||||
| 	cfg.AddAction(pattern, action.Base(action.Forward, rcvs...)) | 	proc msgProc | ||||||
| 	return cfg |  | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func MsgHandler(pattern string, proc msgProc, rcvs ...string) *mhSpec { | ||||||
|  | 	if proc == nil { | ||||||
|  | 		proc = Async | ||||||
|  | 	} | ||||||
|  | 	cfg := mhSpec{config.Base("", nil), proc} | ||||||
|  | 	cfg.AddAction(pattern, action.Base(action.Forward, rcvs...)) | ||||||
|  | 	return &cfg | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type msgProc func(lib.Context, lib.Message) (int, Data) | ||||||
|  |  | ||||||
|  | @ -10,6 +10,8 @@ import ( | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | type Data = map[string]interface{} | ||||||
|  | 
 | ||||||
| type ServerState struct { | type ServerState struct { | ||||||
| 	server *http.Server | 	server *http.Server | ||||||
| } | } | ||||||
|  | @ -50,21 +52,25 @@ func setRoute(ctx lib.Context, rcfg routeCfg, r *gin.Engine) { | ||||||
| 	switch spec := rcfg.spec.(type) { | 	switch spec := rcfg.spec.(type) { | ||||||
| 	case *fsSpec: | 	case *fsSpec: | ||||||
| 		r.Static(rcfg.path, spec.docRoot) | 		r.Static(rcfg.path, spec.docRoot) | ||||||
| 	case lib.Config: | 	case *mhSpec: | ||||||
| 		r.Match(rcfg.methods, rcfg.path+"/*msg", func(c *gin.Context) { | 		r.Match(rcfg.methods, rcfg.path+"/*msg", func(c *gin.Context) { | ||||||
| 			handle(ctx, spec, c) | 			handleMsg(ctx, spec, c) | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // scopes standard request (= message) handler implementation | // scopes standard request (= message) handler implementation | ||||||
| 
 | 
 | ||||||
| func handle(ctx lib.Context, cfg lib.Config, gc *gin.Context) { | func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) { | ||||||
| 	head := strings.Split(gc.Param("msg"), "/")[1:] | 	head := strings.Split(gc.Param("msg"), "/")[1:] | ||||||
| 	cctx := ctx.ChildContext(cfg) | 	cctx := ctx.ChildContext(cfg) | ||||||
| 	msg := message.New(head...).WithSender(cctx) | 	msg := message.New(head...).WithSender(cctx) | ||||||
| 	//fmt.Printf("*** RCell called: msg: %+v, method: %s\n", msg, gc.Request.Method) | 	// if gc.Request.Method == "POST" && data != "": msg.WithPayload(data) | ||||||
| 	core.HandleMessage(cctx, msg) | 	core.HandleMessage(cctx, msg) | ||||||
| 	// if cfg.sync: wait for response message | 	code, data := cfg.proc(ctx, msg) | ||||||
| 	gc.String(http.StatusOK, "Hello World") | 	gc.JSON(code, data) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func Async(ctx lib.Context, msg lib.Message) (int, Data) { | ||||||
|  | 	return http.StatusOK, Data{"status": "OK"} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,7 +32,7 @@ func Config() lib.Config { | ||||||
| 		Port:    ovr("8123", SERVER_PORT), | 		Port:    ovr("8123", SERVER_PORT), | ||||||
| 	} | 	} | ||||||
| 	server_c.AddRoute("/docs", server.FileServer("html")) | 	server_c.AddRoute("/docs", server.FileServer("html")) | ||||||
| 	server_c.AddRoute("/api", server.MsgHandler("demo", "test-receiver")) | 	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