-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
99 lines (82 loc) · 2.96 KB
/
Copy pathmiddleware.go
File metadata and controls
99 lines (82 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package http
import (
"github.com/Bofry/host"
"github.com/Bofry/host-http/internal/middleware"
)
// UseErrorHandler creates error handling middleware with custom handler.
// The handler processes panics and errors during request processing.
func UseErrorHandler(handler ErrorHandler) host.Middleware {
if handler == nil {
panic("argument 'handler' cannot be nil")
}
return &middleware.ErrorHandlerMiddleware{
Handler: handler,
}
}
// UseLogging creates logging middleware with optional services.
// If no services provided, uses no-op logging. Multiple services are composed together.
func UseLogging(services ...LoggingService) host.Middleware {
var loggingService middleware.LoggingService
switch len(services) {
case 0:
loggingService = middleware.NoopLoggingServiceSingleton
case 1:
loggingService = services[0]
default:
// Multiple services - use first service for now
// Composite logging service can be implemented if needed
loggingService = services[0]
}
return &middleware.LoggingMiddleware{
LoggingService: loggingService,
}
}
// UseRequestManager creates request routing middleware with request manager.
// The manager handles automatic routing based on struct methods and tags.
func UseRequestManager(requestManager any) host.Middleware {
if requestManager == nil {
panic("argument 'requestManager' cannot be nil")
}
return &middleware.RequestManagerMiddleware{
RequestManager: requestManager,
}
}
// UseCorsHeader creates CORS handling middleware with optional custom headers.
// Enables cross-origin requests and adds specified headers to allowed list.
func UseCorsHeader(headers ...string) host.Middleware {
return &middleware.CorsMiddleware{
CustomHeaders: headers,
}
}
// UseTracing creates comprehensive tracing middleware with OpenTelemetry and slog fallback.
// Automatically traces HTTP requests with configurable options for skipping certain paths/methods.
func UseTracing(options ...TracingOption) host.Middleware {
tracingMiddleware := &middleware.TracingMiddleware{
MergeCORSTrace: true, // Default to merging CORS traces
}
// Apply options
for _, option := range options {
option(tracingMiddleware)
}
return tracingMiddleware
}
// TracingOption allows customization of tracing middleware behavior
type TracingOption func(*middleware.TracingMiddleware)
// WithSkipPaths configures paths to skip during tracing (e.g., health checks)
func WithSkipPaths(paths ...string) TracingOption {
return func(m *middleware.TracingMiddleware) {
m.SkipPaths = append(m.SkipPaths, paths...)
}
}
// WithSkipMethods configures HTTP methods to skip during tracing
func WithSkipMethods(methods ...string) TracingOption {
return func(m *middleware.TracingMiddleware) {
m.SkipMethods = append(m.SkipMethods, methods...)
}
}
// WithCORSMerging enables or disables merging of CORS preflight traces with actual requests
func WithCORSMerging(enable bool) TracingOption {
return func(m *middleware.TracingMiddleware) {
m.MergeCORSTrace = enable
}
}