App
App is Zincβs main application type.
Create an appβ
app := zinc.New()
app := zinc.NewWithConfig(zinc.Config{StrictRouting: true})
Core responsibilitiesβ
App is responsible for:
- route registration
- middleware registration
- mounting sub-handlers
- lifecycle methods such as
Listen,Serve, andShutdown - route introspection and named route lookup
Common methodsβ
| Method | Purpose |
|---|---|
Get, Post, Put, Patch, Delete, Head, Options, Connect, Trace | Register routes |
Add, Match, All, Any | Register routes more generically |
Use, UsePrefix | Register middleware |
Group, Route | Create grouped route trees |
Mount, Wrap, WrapFunc | Integrate stdlib or external handlers |
Static, StaticFS, File, FileFS | Serve files and directories |
NotFound, RouteNotFound, MethodNotAllowed | Customize error routing |
Routes, FindRoute, RouteByName, URL | Inspect routes and generate URLs |
Listen, ListenTLS, Serve, Shutdown | Run and stop the app |
Route methods accept a handler chain. Middleware goes before the final handler.
app.Post("/posts", authUser, requireRole("editor"), createPost)
Get also accepts a literal string for quick static text responses.
app.Get("/health", "ok")
Named route registrationβ
Use Handle(RouteSpec) when you want route naming and reverse URL generation.
if err := app.Handle(zinc.RouteSpec{
Name: "users.show",
Method: zinc.MethodGet,
Path: "/users/:id",
Handler: showUser,
}); err != nil {
log.Fatal(err)
}
Introspectionβ
routes := app.Routes()
route, ok := app.FindRoute("GET", "/users/42")
named, ok := app.RouteByName("users.show")
url, err := app.URL("users.show", "42")
Routes() returns RouteInfo values containing:
NameMethodPathParamsMountedHandler
Advanced lifecycle helpersβ
Listen is the shortest way to start an app. With no address, it listens on :8080.
app.Listen()
app.Listen(":3000")
Most apps do not need these directly, but Zinc exposes:
AcquireContextReleaseContext
They are useful for adapters, framework integration, and low-level tests.
For normal applications, register routes and let Zinc manage context lifecycle.