App
API reference for the Rivaas App package - a batteries-included web framework with integrated observability.
3 minute read
This is the API reference for the rivaas.dev/app package. For learning-focused documentation, see the App Guide.
Overview
The App package provides a high-level, opinionated framework built on top of the Rivaas router. It includes:
- Integrated observability (metrics, tracing, logging)
- Lifecycle management with hooks
- Graceful shutdown handling
- Health and debug endpoints
- OpenAPI spec generation
- Request binding and validation
Package Information
- Import Path:
rivaas.dev/app - Go Version: 1.25+
- License: Apache 2.0
Architecture
┌─────────────────────────────────────────┐
│ Application Layer │
│ (app package) │
│ │
│ • Configuration Management │
│ • Lifecycle Hooks │
│ • Observability Integration │
│ • Server Management │
│ • Request Binding/Validation │
└──────────────┬──────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Router Layer │
│ (router package) │
│ │
│ • HTTP Routing │
│ • Middleware Chain │
│ • Request Context │
│ • Path Parameters │
└──────────────┬──────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Standard Library │
│ (net/http) │
└─────────────────────────────────────────┘
Quick Reference
Core Types
- App - Main application type
- Context - Request context with app-level features
- HandlerFunc - Handler function type
Key Functions
Configuration
- Options - App-level configuration options
- Server Options - Server configuration
- Observability Options - Metrics, tracing, logging
- Health Options - Health endpoint configuration
- Debug Options - Debug endpoint configuration
API Reference
- API Reference - Core types and methods
- Context API - Context methods for request handling
- Lifecycle Hooks - Hook APIs and execution order
Resources
- Troubleshooting - Common issues and solutions
- pkg.go.dev - Full API documentation
App Type
The main application type that wraps the router with app-level features.
type App struct {
// contains filtered or unexported fields
}
Creating Apps
// Returns (*App, error) for error handling
a, err := app.New(
app.WithServiceName("my-api"),
app.WithServiceVersion("v1.0.0"),
)
if err != nil {
log.Fatal(err)
}
// Panics on error (like regexp.MustCompile)
a := app.MustNew(
app.WithServiceName("my-api"),
)
HTTP Methods
Register routes for HTTP methods:
a.GET(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.POST(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.PUT(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.DELETE(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.PATCH(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.HEAD(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.OPTIONS(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
a.Any(path string, handler HandlerFunc, opts ...RouteOption) *route.Route
Server Management
a.Start(ctx context.Context) error
a.StartTLS(ctx context.Context, certFile, keyFile string) error
a.StartMTLS(ctx context.Context, serverCert tls.Certificate, opts ...MTLSOption) error
Lifecycle Hooks
a.OnStart(fn func(context.Context) error)
a.OnReady(fn func())
a.OnShutdown(fn func(context.Context))
a.OnStop(fn func())
a.OnRoute(fn func(*route.Route))
See Lifecycle Hooks for details.
Context Type
Request context that extends router.Context with app-level features.
type Context struct {
*router.Context
// contains filtered or unexported fields
}
Request Binding
c.Bind(out any, opts ...BindOption) error
c.MustBind(out any, opts ...BindOption) bool
c.BindOnly(out any, opts ...BindOption) error
c.Validate(v any, opts ...validation.Option) error
Error Handling
c.Fail(err error)
c.FailStatus(status int, err error)
c.NotFound(err error)
c.BadRequest(err error)
c.Unauthorized(err error)
c.Forbidden(err error)
c.Conflict(err error)
c.Gone(err error)
c.UnprocessableEntity(err error)
c.TooManyRequests(err error)
c.InternalError(err error)
c.ServiceUnavailable(err error)
Logging
c.Logger() *slog.Logger
See Context API for complete reference.
HandlerFunc
Handler function type that receives an app Context.
type HandlerFunc func(*Context)
Example:
func handler(c *app.Context) {
c.JSON(http.StatusOK, data)
}
a.GET("/", handler)
Next Steps
- API Reference - Complete API documentation
- Options - Configuration options reference
- Troubleshooting - Common issues and solutions
- User Guide - Learn how to use the app package
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.