feat(surf): AssembleRouter builds the router and handler once; the router stops the MCP endpoint it mounted (quick-261118-lbt)
- surf.AssembleRouter returns the router and the compiled handler from one build, so an mcp.enabled app is not built twice; Assemble keeps its signature and is implemented on top of it - serve uses AssembleRouter with a deferred Router.Close - pier.Pier.Close stops the session reaper and the Redis warm-up; the router that mounted the endpoint owns it - ExampleAssembleRouter, routing/testing docs and the surf/pier READMEs
This commit is contained in:
@@ -188,6 +188,30 @@ func ExampleBuildRouter() {
|
||||
// POST /api/blog/posts/{id}/comments acme.blog [throttle:60,1 acme.auth throttle:blog.comments body.limit:65536]
|
||||
}
|
||||
|
||||
func ExampleAssembleRouter() {
|
||||
app := backpack.New(nil)
|
||||
plugin := &BlogPlugin{}
|
||||
if err := plugin.Register(app); err != nil { // the runtime calls Register
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// One build: the route table and the handler share one router.
|
||||
r, h, err := surf.AssembleRouter(app, []party.Plugin{plugin})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer r.Close() // stops the router's in-process rate-limit store
|
||||
fmt.Println(len(r.Routes()), "routes")
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/blog/posts/7", nil))
|
||||
fmt.Println(rec.Code, strings.TrimSpace(rec.Body.String()))
|
||||
// Output:
|
||||
// 4 routes
|
||||
// 200 {"id":7}
|
||||
}
|
||||
|
||||
func ExampleClientIP() {
|
||||
cfg, err := compass.Open(compass.Options{Dir: "config", Env: "development", Environ: []string{}})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user