Handlers
A handler is a function or method with the signature func(web.Context) error. It reads HTTP input, calls the application service, and writes a response. In GoForj, a controller groups the handlers for a feature and exposes their routes through Routes().
For a complete endpoint with a service, test, and request verification, follow Your First Route.
Generate a Controller
From an HTTP-enabled project root, run make:controller with the name of the feature it handles.
forj make:controller reports --no-openUse forj admin make:controller reports to target the admin app.
The command creates internal/reports/controller.go, adds its constructor to app/wire/inject_http_controllers_app.go, and registers its routes in app/routes.go. For an additional app, the controller stays under internal/... and registration goes under the owning app's app/<name>/....
The Make Command Reference shows the full generated output. It also covers grouped package placement, removal, and output overrides.
Replace the starter handler with a call to your service. This example assumes a reports.Service with a Find method. For the full implementation pattern, see Your First Route.
package reports
import (
"net/http"
"github.com/goforj/web"
)
// Controller translates report HTTP requests into service calls.
type Controller struct {
service *Service
}
// NewController constructs the report HTTP adapter.
func NewController(service *Service) *Controller {
return &Controller{service: service}
}
// Routes declares the report endpoints owned by this controller.
func (c *Controller) Routes() []web.Route {
return []web.Route{
web.NewRoute(http.MethodGet, "/reports/:id", c.Show),
}
}
// Show returns one report by ID.
func (c *Controller) Show(ctx web.Context) error {
report, err := c.service.Find(ctx.Context(), ctx.Param("id"))
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, report)
}Return ctx.Redirect(http.StatusFound, target) to send a redirect. See URL Generation for building the target.
The generator registers the controller constructor. Add the service constructor to app/wire/inject_services_app.go.
// appSet provides application-level services and dependencies.
var appSet = wire.NewSet(
// existing framework and app providers...
reports.NewService,
)Verify the Result
List the registered routes.
forj route:listLook for /api/v1/reports/:id. Request the route from the running forj dev server to check the response. Use forj admin route:list for the admin app.
Responsibilities
Controllers should own:
- path parameters
- query parameters
- request binding
- request validation handoff
- service calls
- response shaping
- HTTP status decisions
Controllers should not own long-running business workflows, persistence details, queue worker behavior, or infrastructure construction.
Dependency Injection
Controllers are constructed through providers and Wire. The implementation above keeps its required service visible in NewController; follow that constructor-injection shape instead of reaching through global state. Model optional collaborators explicitly.
Request Context
Use ctx.Context() when passing cancellation and deadlines into services:
report, err := c.service.Generate(ctx.Context(), input)Use web.Context for HTTP-specific behavior such as params, binding, response helpers, request metadata, and response writing.
Next Steps
- Wiring Recipes shows the controller wiring flow.
- Requests and Validation explain the input boundary.
- Responses covers writing HTTP responses.
- Application Services explains where business behavior belongs.