Documentation
¶
Index ¶
- func BadRequest(w http.ResponseWriter, r *http.Request, msg string)
- func Idempotency(store IdempotencyStore, responseTTL time.Duration, opts ...IdempotencyOption) func(http.Handler) http.Handler
- func JSON(w http.ResponseWriter, status int, data any)
- func MapErrors(mapping map[error]int) func(http.Handler) http.Handler
- func NotFound(w http.ResponseWriter, r *http.Request, msg string)
- func Observability(serviceName string, log *slog.Logger) func(http.Handler) http.Handler
- func Serve(ctx context.Context, log *slog.Logger, cfg Config, handler http.Handler) error
- func WriteError(w http.ResponseWriter, r *http.Request, err error)
- type Client
- type ClientOption
- type Config
- type Error
- type IdempotencyConfig
- type IdempotencyOption
- type IdempotencyStore
- type MemoryStore
- func (m *MemoryStore) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (m *MemoryStore) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error
- func (m *MemoryStore) TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func (m *MemoryStore) Unlock(ctx context.Context, key string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
func BadRequest(w http.ResponseWriter, r *http.Request, msg string)
BadRequest writes a 400 Bad Request error response.
func Idempotency ¶ added in v0.1.3
func Idempotency(store IdempotencyStore, responseTTL time.Duration, opts ...IdempotencyOption) func(http.Handler) http.Handler
func JSON ¶
func JSON(w http.ResponseWriter, status int, data any)
JSON writes a serialized JSON response with the given HTTP status code.
func NotFound ¶
func NotFound(w http.ResponseWriter, r *http.Request, msg string)
NotFound writes a 404 Not Found error response.
func Observability ¶
Observability returns an HTTP middleware providing tracing, metrics, access logging, and error tracking.
func WriteError ¶
func WriteError(w http.ResponseWriter, r *http.Request, err error)
WriteError inspects the error chain, resolves matching HTTP status codes, updates OpenTelemetry span status on 5xx errors, and writes a standardized JSON error response.
Types ¶
type Client ¶
Client wraps http.Client with OpenTelemetry tracing instrumentation.
func NewClient ¶
func NewClient(opts ...ClientOption) *Client
NewClient constructs an HTTP client instrumented with OpenTelemetry tracing.
type ClientOption ¶
type ClientOption func(*clientConfig)
ClientOption configures an HTTP Client.
func WithClientTimeout ¶
func WithClientTimeout(d time.Duration) ClientOption
WithClientTimeout sets the overall request timeout.
func WithCustomTransport ¶
func WithCustomTransport(rt http.RoundTripper) ClientOption
WithCustomTransport sets a custom base http.RoundTripper.
type Config ¶
type Config struct {
Port string `env:"PORT" toml:"port" env-default:"8080"`
ReadTimeout time.Duration `env:"READ_TIMEOUT" toml:"read_timeout" env-default:"5s"`
WriteTimeout time.Duration `env:"WRITE_TIMEOUT" toml:"write_timeout" env-default:"10s"`
IdleTimeout time.Duration `env:"IDLE_TIMEOUT" toml:"idle_timeout" env-default:"15s"`
}
Config holds HTTP server configuration parameters.
type IdempotencyConfig ¶ added in v0.1.3
type IdempotencyOption ¶ added in v0.1.3
type IdempotencyOption func(*IdempotencyConfig)
func WithHeaderName ¶ added in v0.1.3
func WithHeaderName(name string) IdempotencyOption
func WithKeyPrefix ¶ added in v0.1.3
func WithKeyPrefix(prefix string) IdempotencyOption
func WithLockTTL ¶ added in v0.1.3
func WithLockTTL(d time.Duration) IdempotencyOption
type IdempotencyStore ¶ added in v0.1.3
type IdempotencyStore interface {
// Get retrieves the stored response envelope for key. If key is in-flight or absent, found returns false.
Get(ctx context.Context, key string) (data []byte, found bool, err error)
// TryLock attempts to acquire an in-flight lock for key with a given lock TTL.
TryLock(ctx context.Context, key string, ttl time.Duration) (acquired bool, err error)
// Set stores the completed response envelope for key with the given retention TTL.
Set(ctx context.Context, key string, data []byte, ttl time.Duration) error
// Unlock releases an in-flight lock or removes the key (e.g. on 5xx failure or panic).
Unlock(ctx context.Context, key string) error
}
IdempotencyStore defines the storage contract for idempotency locking and response caching.
type MemoryStore ¶ added in v0.1.3
type MemoryStore struct {
// contains filtered or unexported fields
}
func NewMemoryStore ¶ added in v0.1.3
func NewMemoryStore() *MemoryStore