Middleware Catalogue
The catalogue is a set of ready-made middleware packages that cover the common
needs of every production API — authentication, validation, password hashing,
audit logging, caching, CORS, and so on. Each one is an ordinary
maniflex.MiddlewareFunc you register on the appropriate pipeline step, with the
same scoping options as any other middleware.
The packages live under maniflex/middleware/. Most are part of the root module,
so they need no extra require to use. Only the two with heavy third-party
dependencies are broken out into their own Go modules — middleware/db/redis
(the Redis rate-limit/cache backend) and middleware/service/bcrypt (password
hashing) — so a project pulls those dependencies in only when it uses them:
| Package | Step | What it ships |
|---|---|---|
middleware/auth | Auth | JWT, API key, role gates, public-read helpers |
middleware/body | Deserialize / Validate | body size limits, unknown-field stripping, type coercion |
middleware/idempotency | Deserialize | idempotency-key replay for safely retried POSTs |
middleware/validate | Validate | uniqueness, regex, cross-field rules, numeric precision, date ranges, conditional required |
middleware/workflow | Validate | state-machine transitions with role-gated guards |
middleware/service | Service / DB-After | password hashing, slugify, derived fields |
middleware/db | DB | tenancy, forced filters, rate limiting, audit log, cache invalidation |
middleware/response | Response | CORS, caching, transforms, redaction, envelopes, metrics |
middleware/openapi | OpenAPI.Generate | security schemes, servers, titles, custom extensions |
How to use the catalogue
Import the package you need and register the returned middleware on the matching pipeline step:
import (
"github.com/xaleel/maniflex/middleware/auth"
"github.com/xaleel/maniflex/middleware/service"
)
server.Pipeline.Auth.Register(
auth.JWTAuth("my-signing-secret", auth.JWTOptions{Issuer: "my-app"}),
)
server.Pipeline.Service.Register(
service.HashField("password", svcbcrypt.Hasher()), // svcbcrypt = middleware/service/bcrypt
maniflex.ForModel("User"),
)
Each middleware factory returns a maniflex.MiddlewareFunc, so the standard
options — ForModel, ForOperation, AtPosition, WithName — apply
verbatim.
Composition
Catalogue middleware is designed to be composable. The expected stack for a typical REST API is roughly:
- Auth —
JWTAuthorAPIKeyAuthpopulatesctx.Auth;RequireRolegates sensitive operations. - Body —
MaxBodySizeandStripUnknownFieldsshape input early. - Validate — built-in tag rules plus
UniqueFieldand friends. - Service —
HashField,SetField,SlugifyField, then any custom business logic, then themaniflex/eventshelpers (events.Emit/events.Webhook/events.SendEmail) on the After side. - DB —
TenancyorForceFilterenforces row-level scoping;AuditLogandInvalidaterun After. - Response —
CORSHeaders,Cache,RedactField, thenLogging/Metricson the After side.
Mix and match freely; nothing in the catalogue is required.
Writing your own
The catalogue is just an applied form of Writing Middleware.
If a built-in does not match your needs, write your own — the contract is the
same func(ctx *maniflex.ServerContext, next func() error) error signature.