Skip to Content

gofasta g route

Generates a route definition file that maps HTTP endpoints to controller handlers. The generated routes follow RESTful conventions with standard CRUD paths and include middleware configuration for authentication.

Usage

gofasta g route <ResourceName> [flags]

Flags

FlagShortDefaultDescription
--methods-mallComma-separated list of routes to generate. Options: create, findAll, findByID, update, delete
--prefix/api/v1URL prefix for the routes
--authtrueInclude authentication middleware

Examples

Generate routes for a Product resource:

gofasta g route Product

Generate only read routes:

gofasta g route Product --methods findAll,findByID

Generate routes with a custom prefix:

gofasta g route Product --prefix /api/v2

Generate routes without authentication middleware:

gofasta g route Product --auth=false

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/gin-gonic/gin" "myapp/app/rest/controllers" "myapp/app/rest/middlewares" ) func RegisterProductRoutes(router *gin.RouterGroup, controller *controllers.ProductController, authMiddleware *middlewares.AuthMiddleware) { products := router.Group("/products") products.Use(authMiddleware.Authenticate()) { products.POST("/", controller.Create) products.GET("/", controller.FindAll) products.GET("/:id", controller.FindByID) products.PUT("/:id", controller.Update) products.DELETE("/:id", 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(router *gin.Engine, container *di.Container) { v1 := router.Group("/api/v1") RegisterUserRoutes(v1, container.UserController, container.AuthMiddleware) RegisterAuthRoutes(v1, container.AuthController) RegisterProductRoutes(v1, container.ProductController, container.AuthMiddleware) // Add this line }

Generated Endpoints

MethodPathHandler
POST/api/v1/products/Create
GET/api/v1/products/FindAll
GET/api/v1/products/:idFindByID
PUT/api/v1/products/:idUpdate
DELETE/api/v1/products/:idDelete
Last updated on