App¶
The core application type.
Creating an App¶
Methods¶
Run¶
Start the server.
RunGraceful¶
Start with graceful shutdown support.
Handles SIGINT and SIGTERM signals, allowing in-flight requests to complete.
Use¶
Add global middleware.
OnError¶
Set custom error handler.
app.OnError(func(c *marten.Ctx, err error) {
log.Printf("Error: %v", err)
c.ServerError("something went wrong")
})
NotFound¶
Set custom 404 handler.
OnStart¶
Register a callback to run when the server starts.
app.OnStart(func() {
log.Println("Server starting...")
// Initialize connections, warm caches, etc.
})
OnShutdown¶
Register a callback to run when the server shuts down gracefully.
app.OnShutdown(func() {
log.Println("Server shutting down...")
// Close database connections, flush buffers, etc.
db.Close()
})
Group¶
Create a route group.
Route Methods¶
app.GET(path string, h Handler, mw ...Middleware)
app.POST(path string, h Handler, mw ...Middleware)
app.PUT(path string, h Handler, mw ...Middleware)
app.DELETE(path string, h Handler, mw ...Middleware)
app.PATCH(path string, h Handler, mw ...Middleware)
app.Handle(method, path string, h Handler, mw ...Middleware)
ServeHTTP¶
App implements http.Handler:
Use with custom server: