Skip to Content

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

FlagDefaultDescription
--graphqlfalseAlso generate GraphQL schema and wire up the resolver
--gqlfalseAlias for --graphql

Examples

Generate a controller with all CRUD handlers:

gofasta g controller Product name:string price:float

Generate with GraphQL support:

gofasta g ctrl Product name:string price:float --graphql

What It Generates

Running gofasta g controller Product name:string price:float generates everything from the service generator plus:

FileDescription
app/rest/controllers/product.controller.goREST controller with CRUD handlers
app/rest/routes/product.routes.goRoute definitions

It also patches these existing files:

FileChange
app/di/container.goAdds ProductService and ProductController fields
app/di/wire.goAdds ProductSet to the Wire build
app/rest/routes/index.routes.goRegisters Product routes
cmd/serve.goWires ProductController into the route config

With --graphql, the controller generator additionally:

  • Generates a GraphQL schema file (.gql)
  • Patches resolver.go to 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):

MethodPathHandlerDescription
POST/api/v1/productsCreateCreate a new product
GET/api/v1/productsFindAllList all products (paginated)
GET/api/v1/products/{id}FindByIDGet a single product
PUT/api/v1/products/{id}UpdateUpdate a product
DELETE/api/v1/products/{id}DeleteDelete a product

Architecture

The controller is the outermost layer in the application:

HTTP Request --> Controller --> Service --> Repository --> Database
  • Controllers use standard net/http types (http.ResponseWriter, *http.Request)
  • Handler methods return error and are wrapped with httputil.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
Last updated on