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:
@@ -115,14 +115,15 @@ func addComment(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.Sta
|
||||
|
||||
Routes ported from a `routes.php` sometimes overlap: `GET /shelves/items/{id}` with `id` numeric and `GET /shelves/{shelfId}/follow` with `shelfId` numeric both match `/shelves/items/follow` as patterns, so ServeMux alone would refuse to register them. surf registers such routes as an overlap family and tries them in registration order, checking literal segments and `Where` constraints, so declare them in the same order as the PHP file. A request no member matches gets the bare 404. Overlapping routes must use literal segments and single-segment `{name}` wildcards; two routes of one method with the same shape, a `{name...}` wildcard in a family, or a more general route that would take a member's requests still fail at boot. `surf.Router.Routes` and `route:list` keep listing every route on its own.
|
||||
|
||||
The generated application `main` wires surf in with `surf.ServeCommand` and `surf.RouteListCommand`; tests can call `surf.Assemble(app, plugins)` and drive the returned handler with `net/http/httptest`.
|
||||
The generated application `main` wires surf in with `surf.ServeCommand` and `surf.RouteListCommand`; tests call `surf.AssembleRouter(app, plugins)`, `defer r.Close()` on the returned router, read its route table and drive the returned handler with `net/http/httptest`.
|
||||
|
||||
## API reference
|
||||
|
||||
| Identifier | Description |
|
||||
|------------|-------------|
|
||||
| `surf.BuildRouter` | Registers built-in and plugin middleware, buckets, routes and the admin, and validates every route without compiling. |
|
||||
| `surf.Assemble` | `surf.BuildRouter` plus compilation into the final `http.Handler`. |
|
||||
| `surf.Assemble` | `surf.BuildRouter` plus compilation into the final `http.Handler`; implemented on `surf.AssembleRouter`. The router is not returned, so it cannot be closed and its fallback store keeps sweeping. |
|
||||
| `surf.AssembleRouter` | Builds once and returns the router (route table, admin prefix, `surf.Router.Handler`) and the compiled handler `serve` uses; the MCP endpoint publishes once per app, so a caller needing both must not build twice. The caller closes the router with `surf.Router.Close`; on an error it is already closed. |
|
||||
| `surf.Router` | The route builder; implements `pact.Router`. `surf.New` creates an empty one. |
|
||||
| `surf.Group` | A prefixed route collection with inherited middleware. |
|
||||
| `surf.Router.RegisterMiddleware` | Stores a named middleware; duplicates fail. |
|
||||
@@ -131,7 +132,7 @@ The generated application `main` wires surf in with `surf.ServeCommand` and `sur
|
||||
| `surf.Router.Handler` | Returns the compiled middleware chain of one registered route by method and pattern, for an in-process dispatch that must run the same guards, throttles, body limit, locale and recovery as an HTTP request. False for an unknown route or before `surf.BuildRouter` compiled the routes. |
|
||||
| `surf.Router.MountStream` | Registers one streaming route (raw, unbuffered, `http.Flusher` passed through) under the bound plugin with the given middleware names; a route another plugin registered for the method and path fails naming it. |
|
||||
| `surf.MCPBuildTag` | `summercms_mcp`: the build tag that links the MCP module; without it `mcp.enabled` true fails boot. |
|
||||
| `surf.Router.Close` | Releases what the router created itself (the fallback `surf.MemoryStore` of `surf.BuildRouter`), never a `surf.Store` published on the app; nil-safe and idempotent, and the compiled handler keeps serving. `surf.BuildRouter` closes the router on every error return; `serve` and `route:list` close theirs on exit. |
|
||||
| `surf.Router.Close` | Releases what the router created itself (the fallback `surf.MemoryStore` of `surf.BuildRouter`, and with MCP the `pier.Pier` endpoint it mounted, through `pier.Pier.Close`), never a `surf.Store` published on the app; nil-safe and idempotent, and the compiled handler keeps serving. `surf.BuildRouter` and `surf.AssembleRouter` close the router on every error return; `serve` and `route:list` close theirs on exit. |
|
||||
| `surf.Router.AdminPrefix` | The admin mount path and true when `surf.BuildRouter` mounted the cabana admin; false when no plugin registers an admin controller. |
|
||||
| `surf.RouteInfo` | Method, pattern, owning plugin, middleware and raw flag of one route. |
|
||||
| `surf.Use` | Builds a middleware name list for a group. |
|
||||
|
||||
Reference in New Issue
Block a user