Skip to Content

gofasta g route

Generates a route definition file that maps HTTP endpoints to controller handlers using go-chi/chi  as the default router. The generated routes follow RESTful conventions with standard CRUD paths. The router is an opt-out default — see Swapping the router for alternatives.

Usage

gofasta g route <Name>

This command has no flags.

Examples

Generate routes for a Product resource:

gofasta g route Product

What It Generates

Running gofasta g route Product creates one file:

app/rest/routes/product.routes.go

Generated Code

// app/rest/routes/product.routes.go package routes import ( "github.com/go-chi/chi/v5" "myapp/app/rest/controllers" "myapp/pkg/httputil" ) func RegisterProductRoutes(r chi.Router, controller *controllers.ProductController) { r.Post("/products", httputil.Handle(controller.Create)) r.Get("/products", httputil.Handle(controller.FindAll)) r.Get("/products/{id}", httputil.Handle(controller.FindByID)) r.Put("/products/{id}", httputil.Handle(controller.Update)) r.Delete("/products/{id}", httputil.Handle(controller.Delete)) }

Route Registration

The generated route file is registered in app/rest/routes/index.routes.go. When using gofasta g scaffold, this registration is patched automatically. When generating routes standalone, you need to add the registration manually:

// app/rest/routes/index.routes.go func RegisterRoutes(r chi.Router, container *di.Container) { RegisterUserRoutes(r, container.UserController) RegisterAuthRoutes(r, container.AuthController) RegisterProductRoutes(r, container.ProductController) // Add this line }

Generated Endpoints

MethodPathHandler
POST/api/v1/productsCreate
GET/api/v1/productsFindAll
GET/api/v1/products/{id}FindByID
PUT/api/v1/products/{id}Update
DELETE/api/v1/products/{id}Delete
Last updated on