Documentation
¶
Index ¶
- func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error)
- func BytesToPublicKey(pub []byte) (*rsa.PublicKey, error)
- func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error)
- func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) ([]byte, error)
- func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error)
- func GetLogEntry(r *http.Request) *slog.Logger
- func GetRequestIdLogger(r *http.Request) *slog.Logger
- func Identify() string
- func LogAllStatuses(r *http.Request)
- func LogEntrySetAttrs(r *http.Request, attrs ...any)
- func LogEntrySetField(r *http.Request, key string, value interface{})
- func LogEntrySetFields(r *http.Request, fields map[string]interface{})
- func LogHeaders(r *http.Request)
- func NewStructuredLogger(handler slog.Handler, onlyErrs bool) func(next http.Handler) http.Handler
- func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte
- func Protection(next http.Handler) http.Handler
- func PublicKeyToBytes(pub *rsa.PublicKey) ([]byte, error)
- func RealIPFromRequest(r *http.Request) string
- func Recoverer(next http.Handler) http.Handler
- func RequestForwardedHostProtoMiddleware(next http.Handler) http.Handler
- func RequestHost(r *http.Request) (host string)
- func RequestProto(r *http.Request) (proto string)
- func SignWithPrivateKey(msg []byte, priv *rsa.PrivateKey) ([]byte, error)
- func VerifyWithPublicKey(msg []byte, sig []byte, pubkey *rsa.PublicKey) error
- type ErrResponse
- type RateLimiter
- type RateLimiterConfig
- type StructuredLogger
- type StructuredLoggerEntry
- type TokenBucket
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToPrivateKey ¶
func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error)
BytesToPrivateKey converts PKCS#1 ASN.1 DER bytes to an RSA private key
func BytesToPublicKey ¶
BytesToPublicKey converts PKIX ASN.1 DER bytes to an RSA public key
func DecryptWithPrivateKey ¶
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error)
DecryptWithPrivateKey decrypts a message using RSA-OAEP with SHA512
func EncryptWithPublicKey ¶
EncryptWithPublicKey encrypts a message using RSA-OAEP with SHA512
func GenerateKeyPair ¶
GenerateKeyPair generates an RSA key pair of the given bit size
func GetRequestIdLogger ¶
GetRequestIdLogger returns a logger with request ID
func Identify ¶
func Identify() string
Identify returns the package name, file name and line number where panic occurred
func LogAllStatuses ¶
LogAllStatuses configures the logger to log all statuses, not just errors
func LogEntrySetAttrs ¶
LogEntrySetAttrs adds multiple attributes to the log entry
func LogEntrySetField ¶
LogEntrySetField adds a field to the log entry
func LogEntrySetFields ¶
LogEntrySetFields adds multiple fields to the log entry
func NewStructuredLogger ¶
StructuredLogger is a simple, but powerful implementation of a custom structured logger backed on log/slog. I encourage users to copy it, adapt it and make it their own. Also take a look at https://github.com/go-chi/httplog for a dedicated pkg based on this work, designed for context-based http routers. Example: logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) slog.SetDefault(logger) hlog := NewStructuredLogger(slog.Default().Handler(), true)
func PrivateKeyToBytes ¶
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte
PrivateKeyToBytes converts an RSA private key to PKCS#1 ASN.1 DER bytes
func Protection ¶
Protection adds security headers to responses
func PublicKeyToBytes ¶
PublicKeyToBytes converts an RSA public key to PKIX ASN.1 DER bytes
func RealIPFromRequest ¶
RealIPFromRequest returns the real client IP from request headers
func RequestForwardedHostProtoMiddleware ¶
RequestForwardedHostProtoMiddleware updates request host and protocol from forwarded headers
func RequestHost ¶
RequestHost returns the host from forwarded headers or fallback to request host
func RequestProto ¶
RequestProto returns the protocol from forwarded headers or fallback to request scheme
func SignWithPrivateKey ¶ added in v1.0.1
func SignWithPrivateKey(msg []byte, priv *rsa.PrivateKey) ([]byte, error)
SignWithPrivateKey signs a message using RSA PKCS#1v1.5 with SHA256
Types ¶
type ErrResponse ¶
type ErrResponse struct {
Err error `json:"-"` // Low-level runtime error
HTTPStatusCode int `json:"-"` // HTTP response status code
StatusText string `json:"status"` // User-level status message
AppCode int64 `json:"code,omitempty"` // Application-specific error code
ErrorText string `json:"error,omitempty"` // Application-level error message
}
ErrResponse represents an error response structure
func (*ErrResponse) Error ¶
func (e *ErrResponse) Error() string
Error implements the error interface
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
Render implements the render.Renderer interface
type RateLimiter ¶ added in v1.0.1
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a middleware for request rate limiting
func NewRateLimiter ¶ added in v1.0.1
func NewRateLimiter(config *RateLimiterConfig) (*RateLimiter, error)
NewRateLimiter creates a new RateLimiter instance
func (*RateLimiter) Middleware ¶ added in v1.0.1
func (rl *RateLimiter) Middleware(next http.Handler) http.Handler
Middleware returns a middleware function for request rate limiting
func (*RateLimiter) Stop ¶ added in v1.0.1
func (rl *RateLimiter) Stop()
Stop terminates background processes of the rate limiter
type RateLimiterConfig ¶ added in v1.0.1
type RateLimiterConfig struct {
UserRequestsPerSecond float64 // Requests per second limit per user
UserBurst int // Maximum burst requests allowed per user
GlobalRequestsPerSecond float64 // Global requests per second limit
GlobalBurst int // Global burst limit
CookieName string // Cookie name for user identification
CookieMaxAge int // Cookie lifetime in seconds
CleanupInterval time.Duration // Interval for cleaning up unused data
EncryptionKey []byte // Encryption key (32 bytes for AES-256)
}
RateLimiterConfig contains configuration for the rate limiter
type StructuredLogger ¶
StructuredLogger implements the LogFormatter interface
func (*StructuredLogger) NewLogEntry ¶
func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry
NewLogEntry creates a new log entry for each request
type StructuredLoggerEntry ¶
StructuredLoggerEntry represents a single request log entry
func (*StructuredLoggerEntry) Panic ¶
func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte)
Panic logs panic information
type TokenBucket ¶ added in v1.0.1
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket implements the token bucket algorithm for rate limiting
func NewTokenBucket ¶ added in v1.0.1
func NewTokenBucket(rate float64, capacity int) *TokenBucket
NewTokenBucket creates a new token bucket instance
func (*TokenBucket) Take ¶ added in v1.0.1
func (tb *TokenBucket) Take() bool
Take attempts to take a token from the bucket