SkilVaultSign inStart free

Catalogue / chi-fx-router-wiring

chi-fx-router-wiring

Older skill: source not recordedSignedGenerates Chi router files for Go services built as internal modules with Uber Fx dependency injection. Produces one router per REST resource in the module's http/chi/router directory, covering CRUD endpoints, verb-suffixed action routes, nested sub-resources, middleware-scoped route groups, and the…
---
name: chi-fx-router-wiring
description: >
  Generates Chi router files for Go services built as internal modules with
  Uber Fx dependency injection. Produces one router per REST resource in the
  module's http/chi/router directory, covering CRUD endpoints, verb-suffixed
  action routes, nested sub-resources, middleware-scoped route groups, and the
  annotated fx.Provide registration that mounts each router into the server's
  routes group. Use when creating or changing Go HTTP route registration,
  adding endpoints to a chi router, or wiring a new router into a module's
  fx.go file. Not for writing handler business logic, request parsing or
  response encoding, non-chi frameworks, or server and middleware
  configuration itself.
---

Chi Router Wiring with Fx

You are adding the HTTP transport piece of a modular Go backend: a small router object per resource that binds handler methods to URL paths, plus the Fx registration that makes the server actually run it.

This skill does not cover handler internals (parsing, validation, responses), server or middleware setup, or any specific vendor's wrapper package — substitute your own repository's chi server package, lint targets, and module path wherever the examples show placeholders.

Read the module first

Before writing anything, open the target module's fx.go and one existing router file. Copy the import paths and package style you find there so the new code matches. If the module has no router yet, follow the layout below.

Where the code goes

  • Router file: internal/modules/<module>/http/chi/router/<resource>_router.go
  • Registration: internal/modules/<module>/fx.go

Anatomy of a router file

Build exactly four parts, in this order:

  1. package router declaration.
  2. Imports — the project's chi wrapper and the module's handler package, and

the middleware package only when a route group needs it.

  1. A struct whose fields are handler pointers and nothing else.
  2. A pointer-returning constructor and the Setup method.

Example for a billing module's invoices resource (module path example.com/app in these examples — replace with the real one):

package router

import (
	"example.com/app/internal/modules/billing/http/chi/handler"
	"example.com/app/pkg/chi"
)

type InvoiceRouter struct {
	handler *handler.InvoiceHandler
}

func NewInvoiceRouter(h *handler.InvoiceHandler) *InvoiceRouter {
	return &InvoiceRouter{handler: h}
}

func (r *InvoiceRouter) Setup(server *chi.Server) {
	router := server.Router()
	router.Get("/api/v1/invoices", r.handler.ListInvoices)
	router.Get("/api/v1/invoices/{id}", r.handler.GetInvoice)
	router.Post("/api/v1/invoices", r.handler.CreateInvoice)
	router.Put("/api/v1/invoices/{id}", r.handler.UpdateInvoice)
	router.Delete("/api/v1/invoices/{id}", r.handler.DeleteInvoice)
}

The Setup signature must match character for character: Setup(server *chi.Server). This is what satisfies the wrapper's Route interface — a different name or parameter type means the server can never pick the router up. The first statement inside Setup always fetches the mux via server.Router().

Routes beyond basic CRUD

State-changing actions get their own path with a verb suffix, always registered with POST:

router.Post("/api/v1/invoices/{id}/send", r.handler.SendInvoice)
router.Post("/api/v1/invoices/{id}/void", r.handler.VoidInvoice)

Sub-resources hang off the parent's id. One id when listing or adding, two ids when targeting a specific child:

router.Get("/api/v1/invoices/{id}/lines", r.handler.ListInvoiceLines)
router.Post("/api/v1/invoices/{id}/lines", r.handler.AddInvoiceLine)
router.Delete("/api/v1/invoices/{invoiceId}/lines/{lineId}", r.handler.RemoveInvoiceLine)

Scoping middleware to a subset of routes

When only some routes need a middleware such as authentication, wrap them in a group. Routes outside the group stay untouched:

func (r *InvoiceRouter) Setup(server *chi.Server) {
	router := server.Router()
	router.Get("/api/v1/invoices", r.handler.ListInvoices)
	router.Get("/api/v1/invoices/{id}", r.handler.GetInvoice)
	router.Group(func(grp chi.Router) {
		grp.Use(middleware.RequireAuth)
		grp.Post("/api/v1/invoices", r.handler.CreateInvoice)
		grp.Delete("/api/v1/invoices/{id}", r.handler.DeleteInvoice)
	})
}

One router, two resources

If two resources belong together logically (say, orders and their shipments), give the router two handler fields through the constructor rather than inventing a second router file:

type FulfillmentRouter struct {
	orderHandler    *handler.OrderHandler
	shipmentHandler *handler.ShipmentHandler
}

func NewFulfillmentRouter(
	orderHandler *handler.OrderHandler,
	shipmentHandler *handler.ShipmentHandler,
) *FulfillmentRouter {
	return &FulfillmentRouter{
		orderHandler:    orderHandler,
		shipmentHandler: shipmentHandler,
	}
}

Its Setup then registers /api/v1/orders and /api/v1/shipments routes against the respective handlers.

Fx registration

The server discovers routers through a routes result group. A provider missing either annotation below compiles fine but never runs — its routes silently vanish. Always include both:

fx.Provide(
	fx.Annotate(
		router.NewInvoiceRouter,
		fx.As(new(chi.Route)),
		fx.ResultTags(`group:"routes"`),
	),
),

For several routers in one module, stack annotated constructors inside the same fx.Provide:

fx.Provide(
	fx.Annotate(
		router.NewInvoiceRouter,
		fx.As(new(chi.Route)),
		fx.ResultTags(`group:"routes"`),
	),
	fx.Annotate(
		router.NewWebhookRouter,
		fx.As(new(chi.Route)),
		fx.ResultTags(`group:"routes"`),
	),
),

Path conventions

| Element | Rule | Example | | ------------ | --------------------------------- | ------------------------------------ | | Version | every path starts with /api/v1/ | /api/v1/invoices | | Collection | plural noun | /api/v1/invoices | | Single item | {id} parameter | /api/v1/invoices/{id} | | Children | nested under parent id | /api/v1/invoices/{id}/lines | | Specific child | parent and child params | /api/v1/invoices/{iid}/lines/{lid} | | Action | verb suffix on the item path | /api/v1/invoices/{id}/send | | Bulk | bulk suffix on the collection | /api/v1/invoices/bulk |

Verb choices

| Method | Use for | | -------- | -------------------------------- | | GET | list a collection or fetch one | | POST | create, or trigger an action | | PUT | replace an item entirely | | PATCH | apply a partial update | | DELETE | remove an item |

Naming

| Thing | Pattern | Example | | ---------------- | ---------------- | ----------------- | | File | snake_case | invoice_router.go | | Struct | PascalCase | InvoiceRouter | | Constructor | New + struct | NewInvoiceRouter | | Handler bindings | verb + noun | ListInvoices, VoidInvoice |

Hard constraints

  • Never modify the Setup signature or skip the server.Router() call.
  • Never register a path that lacks the /api/v1/ prefix or names a singular

resource in a collection slot.

  • Never store anything in the router struct besides handler pointers — the

router routes; it does not compute.

  • Never add package-level helper functions to the router file; if you need

shared logic, use a private method on the struct.

  • Never write a Setup without its matching annotated fx.Provide entry in

the module's fx.go.

  • Do not add comments to the generated file, and import nothing beyond the chi

wrapper, the handler package, and (when grouping) middleware.

Step-by-step

  1. Inspect the module's fx.go and an existing router for import paths and

style.

  1. Create internal/modules/<module>/http/chi/router/<resource>_router.go.
  2. Write the struct, constructor, and Setup with every route the request

needs, following the tables above.

  1. Add the annotated provider to internal/modules/<module>/fx.go.
  2. Verify: run the repository's lint and static-analysis targets (for example

its lint make target) and fix everything they report; if the repo has none, at minimum build and vet the package.