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:
Jakub Zych
2026-11-18 15:44:56 +01:00
parent d9e21ad019
commit 38f7f3a777
11 changed files with 257 additions and 16 deletions

View File

@@ -568,12 +568,34 @@ func (r *Router) wrap(rt route) (http.Handler, error) {
}
// Assemble registers plugin middleware and routes, then compiles ServeMux.
// It is AssembleRouter without the router, so the router's fallback
// MemoryStore is never closed; code that builds many routers (tests) uses
// AssembleRouter and Router.Close instead.
func Assemble(app *backpack.App, plugins []party.Plugin) (http.Handler, error) {
r, err := BuildRouter(app, plugins)
_, h, err := AssembleRouter(app, plugins)
if err != nil {
return nil, err
}
return r.compile()
return h, nil
}
// AssembleRouter builds the router once and compiles it: one build gives
// both the route table (Routes, AdminPrefix, Handler) and the handler serve
// uses. The MCP module publishes its endpoint once per app, so a caller that
// needs both must not build twice. The caller owns the router and calls
// Router.Close when the app stops; on an error nothing is returned and the
// router is already closed.
func AssembleRouter(app *backpack.App, plugins []party.Plugin) (*Router, http.Handler, error) {
r, err := BuildRouter(app, plugins)
if err != nil {
return nil, nil, err
}
h, err := r.compile()
if err != nil {
_ = r.Close()
return nil, nil, err
}
return r, h, nil
}
// BuildRouter registers plugin middleware and routes without compiling ServeMux,