Docs · Go / Fiber / Production API
Fiber Production API Runbook
Fiber Production API Runbook
Fiber is useful when a Go service needs a concise routing and middleware model with a fast HTTP implementation. A production Fiber service still needs explicit ownership for process lifecycle, error behavior, request-scoped data, and dependency wiring.
Start with an application boundary
Keep application construction separate from process startup:
func NewApp(orders OrderService) *fiber.App {
app := fiber.New()
app.Use(recover.New())
app.Get("/health", healthHandler)
app.Post("/orders", createOrderHandler(orders))
return app
}
func main() {
app := NewApp(newOrderService())
log.Fatal(app.Listen(":" + portFromEnv()))
}
This makes the route tree testable without starting a long-running process and keeps environment parsing and dependency construction in one place.
Treat middleware order as behavior
Middleware is an ordered pipeline. Put cross-cutting concerns where their scope is obvious:
- request ID and access logging;
- recovery from unexpected panics;
- security headers and CORS policy;
- authentication and authorization;
- route handlers;
- centralized error mapping where the application needs it.
Do not assume that adding middleware is neutral. A body parser, authentication guard, or error handler can change the behavior observed by every route below it. Test the order that matters to the service.
Make errors observable and safe
Fiber handlers return errors. Establish one error policy rather than letting each route invent a response shape. Map expected domain errors to stable HTTP statuses and log unexpected errors with a request ID. Do not return stack traces, database errors, or secrets to clients.
The Recover middleware protects the process from a panic becoming an uncontrolled request failure. It is a safety boundary, not a substitute for validating input or handling expected errors.
Respect request-scoped context
Fiber exposes a request context optimized for the framework's execution model. Treat values read from the request context as request-scoped. Do not retain references to the context, request body, or mutable request data in a background goroutine after the handler returns. Copy the data needed by an asynchronous job and pass a separate cancellation policy to that job.
This rule is especially important for logging, queues, and database work that outlives the HTTP handler.
Shut down deliberately
Run app.Listen under process-level signal handling and call app.Shutdown()
when the platform sends SIGTERM. The shutdown path should stop accepting new
requests, allow in-flight work to finish within a deadline, and close external
resources.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
if err := app.Shutdown(); err != nil {
log.Printf("fiber shutdown failed: %v", err)
}
Use the shutdown method available in the Fiber version installed by the project. The important contract is a bounded, observable shutdown rather than the exact signal helper.
Verification checklist
go.modpins the Fiber major version used by the code.- Recovery, request IDs, authentication, and error mapping have explicit order.
- Every public route has input and authorization tests.
- Error responses are stable and do not disclose internals.
- Background work does not retain Fiber request objects.
- SIGTERM is tested against the deployed process.
- Readiness is separate from liveness and reflects dependency state.
References
Agent access
Choose a representation.
Both machine-readable links refer to this same published item and preserve its identity, relationships, freshness, and body.