gofasta g controller
Generates a REST controller for a given resource with full CRUD endpoint handlers, along with all prerequisite layers (model, migration, repository, service, DTOs, Wire provider, and routes). The controller delegates all business logic to the service layer and uses go-chi/chi for HTTP routing with httputil.Handle() for error-returning handlers. The router is an opt-out default — see Swapping the router for alternatives.
Aliases: ctrl
Usage
gofasta g controller <Name> [field:type ...] [flags]Flags
| Flag | Default | Description |
|---|---|---|
--graphql | false | Also generate GraphQL schema and wire up the resolver |
--gql | false | Alias for --graphql |
Examples
Generate a controller with all CRUD handlers:
gofasta g controller Product name:string price:floatGenerate with GraphQL support:
gofasta g ctrl Product name:string price:float --graphqlWhat It Generates
Running gofasta g controller Product name:string price:float generates everything from the service generator plus:
| File | Description |
|---|---|
app/rest/controllers/product.controller.go | REST controller with CRUD handlers |
app/rest/routes/product.routes.go | Route definitions |
It also patches these existing files:
| File | Change |
|---|---|
app/di/container.go | Adds ProductService and ProductController fields |
app/di/wire.go | Adds ProductSet to the Wire build |
app/rest/routes/index.routes.go | Registers Product routes |
cmd/serve.go | Wires ProductController into the route config |
With --graphql, the controller generator additionally:
- Generates a GraphQL schema file (
.gql) - Patches
resolver.goto add the service dependency - Runs
gqlgen generate
Generated Code
// app/rest/controllers/product.controller.go
package controllers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
"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 handles POST /api/v1/products
func (c *ProductController) Create(w http.ResponseWriter, r *http.Request) error {
var req dtos.CreateProductRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return err
}
result, err := c.service.Create(req)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
return json.NewEncoder(w).Encode(result)
}
// FindAll handles GET /api/v1/products
func (c *ProductController) FindAll(w http.ResponseWriter, r *http.Request) error {
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
if page < 1 {
page = 1
}
if limit < 1 {
limit = 10
}
result, err := c.service.FindAll(page, limit)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(result)
}
// FindByID handles GET /api/v1/products/{id}
func (c *ProductController) FindByID(w http.ResponseWriter, r *http.Request) error {
id := chi.URLParam(r, "id")
result, err := c.service.FindByID(id)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(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 use standard
net/httptypes (http.ResponseWriter,*http.Request) - Handler methods return
errorand are wrapped withhttputil.Handle() - Route parameters are extracted via
chi.URLParam(r, "name") - Request validation uses
go-playground/validator - All business logic is delegated to the service layer
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 g scaffold — generate all layers at once