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 ProductWhat It Generates
Running gofasta g route Product creates one file:
app/rest/routes/product.routes.goGenerated 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
| Method | Path | Handler |
|---|---|---|
POST | /api/v1/products | Create |
GET | /api/v1/products | FindAll |
GET | /api/v1/products/{id} | FindByID |
PUT | /api/v1/products/{id} | Update |
DELETE | /api/v1/products/{id} | Delete |
Related
- gofasta g controller — generate the controller these routes point to
- gofasta g scaffold — generate all layers with automatic route registration
Last updated on