gofasta g controller
Generates a REST controller for a given resource with full CRUD endpoint handlers. Each handler includes Swagger annotation comments for automatic API documentation generation. The controller delegates all business logic to the service layer.
Usage
gofasta g controller <ResourceName> [flags]Flags
| Flag | Short | Default | Description |
|---|---|---|---|
--methods | -m | all | Comma-separated list of handlers to generate. Options: create, findAll, findByID, update, delete |
--auth | true | Include authentication middleware annotations |
Examples
Generate a controller with all CRUD handlers:
gofasta g controller ProductGenerate with only specific handlers:
gofasta g controller Product --methods create,findAll,findByIDGenerate without authentication annotations:
gofasta g controller Product --auth=falseWhat It Generates
Running gofasta g controller Product creates one file:
app/rest/controllers/product.controller.goGenerated Code
// app/rest/controllers/product.controller.go
package controllers
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"myapp/app/dtos"
svcInterfaces "myapp/app/services/interfaces"
)
type ProductController struct {
service svcInterfaces.ProductService
}
func NewProductController(service svcInterfaces.ProductService) *ProductController {
return &ProductController{service: service}
}
// Create godoc
// @Summary Create a new product
// @Description Create a new product with the provided data
// @Tags Products
// @Accept json
// @Produce json
// @Param body body dtos.CreateProductRequest true "Product data"
// @Success 201 {object} dtos.ProductResponse
// @Failure 400 {object} dtos.ErrorResponse
// @Failure 500 {object} dtos.ErrorResponse
// @Security BearerAuth
// @Router /api/v1/products [post]
func (c *ProductController) Create(ctx *gin.Context) {
var req dtos.CreateProductRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, dtos.ErrorResponse{Error: err.Error()})
return
}
result, err := c.service.Create(req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, dtos.ErrorResponse{Error: err.Error()})
return
}
ctx.JSON(http.StatusCreated, result)
}
// FindAll godoc
// @Summary List all products
// @Description Get a paginated list of products
// @Tags Products
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Success 200 {object} dtos.PaginatedProductResponse
// @Failure 500 {object} dtos.ErrorResponse
// @Security BearerAuth
// @Router /api/v1/products [get]
func (c *ProductController) FindAll(ctx *gin.Context) {
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
result, err := c.service.FindAll(page, limit)
if err != nil {
ctx.JSON(http.StatusInternalServerError, dtos.ErrorResponse{Error: err.Error()})
return
}
ctx.JSON(http.StatusOK, result)
}REST Endpoints
The generated controller handles these endpoints (when paired with generated routes):
| Method | Path | Handler | Description |
|---|---|---|---|
POST | /api/v1/products | Create | Create a new product |
GET | /api/v1/products | FindAll | List all products (paginated) |
GET | /api/v1/products/:id | FindByID | Get a single product |
PUT | /api/v1/products/:id | Update | Update a product |
DELETE | /api/v1/products/:id | Delete | Delete a product |
Architecture
The controller is the outermost layer in the application:
HTTP Request --> Controller --> Service --> Repository --> Database- Controllers handle HTTP request/response concerns only
- Request body parsing and validation happen in the controller
- All business logic is delegated to the service layer
- Swagger annotations enable automatic API documentation
Related
- gofasta g service — generate the service this controller calls
- gofasta g dto — generate the DTOs used in request/response
- gofasta g route — generate route definitions for this controller
- gofasta swagger — generate Swagger docs from controller annotations
Last updated on