Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain(handler http.Handler, mws ...Middleware) http.Handler
Chain wraps the given handler in a middleware chain. The first element in the list is the outermost middleware: it receives the request first and sees the response last.
Chain(handler, A, B) is equivalent to A(B(handler)) → request flow: A → B → handler → response flow: handler → B → A
Types ¶
type BasicAuthConfig ¶ added in v0.2.0
type BasicAuthConfig struct {
// User is the username required for authentication.
User string `env:"ADMIN_USER"`
// Password is the password required for authentication.
Password string `env:"ADMIN_PASS"`
}
BasicAuthConfig defines the configuration for the BasicAuth middleware.
Struct tags are compatible with `github.com/meesooqa/go-cfg`
type CORSConfig ¶
type CORSConfig struct {
// AllowedOrigins is a list of permitted origins, or ["*"] to allow all.
// Empty by default (CORS is disabled)
AllowedOrigins []string `yaml:"allowed_origins"`
// AllowedMethods is a list of HTTP methods permitted in the preflight response
AllowedMethods []string `yaml:"allowed_methods" default:"GET,POST,PUT,PATCH,DELETE,OPTIONS"`
// AllowedHeaders is a list of headers permitted in the preflight response
AllowedHeaders []string `yaml:"allowed_headers" default:"Content-Type,Authorization"`
// AllowCredentials allows sending credentials (cookies, Authorization)
// in cross-origin requests. See the warning in CORS() —
// this is incompatible with AllowedOrigins: ["*"] per the specification
AllowCredentials bool `yaml:"allow_credentials" default:"false"`
// MaxAge specifies how long the browser can cache the preflight request response
MaxAge time.Duration `yaml:"max_age" default:"12h"`
}
CORSConfig defines the configuration for the CORS middleware. By default, AllowedOrigins is empty, meaning no CORS headers are added (a safe default: the browser applies the standard same-origin policy). To enable cross-origin requests, explicitly set AllowedOrigins
Struct tags are compatible with `github.com/meesooqa/go-cfg`
type Middleware ¶
Middleware is a function that wraps one http.Handler with another. It uses the standard Go signature, directly compatible with net/http without any custom abstraction layers, any existing third-party middleware following this pattern can be used seamlessly without adapters
func BasicAuth ¶ added in v0.2.0
func BasicAuth(cfg BasicAuthConfig) Middleware
BasicAuth is a middleware that protects routes using HTTP Basic Authentication. It verifies the request credentials against the provided configuration. If authentication fails, it returns a 401 Unauthorized response with the WWW-Authenticate header, prompting the browser for credentials.
func CORS ¶
func CORS(c CORSConfig) Middleware
CORS creates a middleware that adds CORS headers based on c. If c.AllowedOrigins is empty, the middleware is a no-op; it simply passes the request through without adding any headers.
An important nuance of the CORS specification: the combination of AllowedOrigins: ["*"] and AllowCredentials: true is prohibited by browsers; a server must not return Access-Control-Allow-Origin: * alongside Access-Control-Allow-Credentials: true. In this case, the middleware automatically falls back to "echoing" the specific request origin instead of using "*", ensuring the configuration remains functional rather than being silently rejected by the browser.
func Logging ¶
func Logging(logger *slog.Logger) Middleware
Logging creates a middleware that logs every request: method, path, response status, duration, and client address. If logger is nil, slog.Default() is used.
Additionally, the middleware attaches a request-scoped logger (enriched with a request_id) to the request's context.Context via lgr.NewContext. Controller handlers can retrieve it using lgr.FromContext(r.Context()), ensuring all their log entries are automatically tagged with the same request_id as the final "request completed" entry.
The request_id is extracted from the X-Request-Id header if provided by the client or proxy; otherwise, a random one is generated. In either case, it is returned in the X-Request-Id response header.