Skip to content

Mount Arc in Chi

Your service already routes with Chi and you want Arc’s commands and queries beside your own handlers. Mount the Arc application at the root so Arc keeps its own paths, its custom QUERY method and its empty 404/405 responses.

Build and start Arc first, then hand the application to the router. This code is compiled and tested in the recipes module:

// NewRouter returns a Chi router that serves host routes and forwards every
// other request, unchanged, to the started Arc application.
func NewRouter(app http.Handler) chi.Router {
// Chi v5.3.1 and later route QUERY natively, so this is a no-op there.
// Earlier versions answer QUERY with Chi's own 405 unless it is registered
// before Mount. Registration is process-global.
chi.RegisterMethod("QUERY")
router := chi.NewRouter()
router.Get("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
router.Mount("/", app)
return router
}

Mount does not rewrite the request URL, so Arc sees the original path. Your own routes, such as /healthz, win for their exact method and path. Every other request reaches Arc, which answers unmapped paths with an empty 404 and a wrong method with an empty 405 and its own Allow header.

The test serves the router over real HTTP with Chi v5.3.2 and checks command execution, /validate without execution, GET and QUERY argument binding, HEAD, Arc’s empty 404/405, and that cancelling the client request cancels the query’s context. Direct SSE flushes two ordered results, direct WebSocket upgrades and sends two results, and disconnect and application shutdown join the source. Correlation IDs and QUERY’s no-store survive the host. It does not cover other Chi versions, hub multiplexing or Chi middleware you add.

Your host owns the listener and server timeouts. Initiate HTTP shutdown and Arc shutdown together, then join both with fresh bounded cleanup budgets. Do not wait for server.Shutdown before calling app.Shutdown: HTTP shutdown waits for active SSE handlers, while Arc shutdown cancels and joins their sources. Copy this helper into your host and check its returned error:

// ShutdownHost stops HTTP admission and Arc observations concurrently, then
// joins both. Each has a fresh five-second cleanup budget, not the canceled
// signal or request context. Business callbacks must honor cancellation.
func ShutdownHost(app *arc.Application, server *http.Server) error {
httpContext, cancelHTTP := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelHTTP()
arcContext, cancelArc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelArc()
httpStopped := make(chan error, 1)
go func() {
httpStopped <- server.Shutdown(httpContext)
}()
arcError := app.Shutdown(arcContext)
return errors.Join(arcError, <-httpStopped)
}

The mounted-host tests run this sequence with an active SSE subscription and check both the drained response and producer cancellation. Configure CORS and authentication outside Arc as shown in CORS and CSRF and JWT bearer tokens. See hosting for the embedding contract.