Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (65 loc) · 2.29 KB
/
Copy pathmain.go
File metadata and controls
75 lines (65 loc) · 2.29 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
package main
import (
"log"
"net/http"
"os"
hosthttp "github.com/Bofry/host-http"
"github.com/Bofry/host-http/response"
"github.com/Bofry/host-http/response/failure"
)
// APIRequest demonstrates middleware chain usage
type APIRequest struct{}
// Get handles GET requests with full middleware processing
func (r *APIRequest) Get(w http.ResponseWriter, req *http.Request) {
response.JSON.Success(w, map[string]any{
"message": "Middleware chain working!",
"path": req.URL.Path,
"method": req.Method,
})
}
// RequestManager defines the routes
type RequestManager struct {
*APIRequest `url:"/api"`
}
func main() {
app := &App{}
// Print tracing configuration for demonstration
tracingEnabled := os.Getenv("TRACING_ENABLED")
serviceName := os.Getenv("SERVICE_NAME")
jaegerEndpoint := os.Getenv("JAEGER_ENDPOINT")
log.Printf("=== Tracing Configuration ===")
log.Printf("TRACING_ENABLED: %s (default: false)", tracingEnabled)
log.Printf("SERVICE_NAME: %s (default: host-http-service)", serviceName)
log.Printf("JAEGER_ENDPOINT: %s (default: http://localhost:14268/api/traces)", jaegerEndpoint)
log.Printf("===========================")
if tracingEnabled == "true" {
log.Printf("OpenTelemetry tracing enabled - traces will be sent to Jaeger")
} else {
log.Printf("Tracing disabled - using structured slog for observability")
}
// Demonstrate comprehensive middleware chain with tracing
hosthttp.Startup(app).
Middlewares(
// Add tracing middleware first to capture the entire request lifecycle
hosthttp.UseTracing(
hosthttp.WithSkipPaths("/health", "/metrics"),
hosthttp.WithCORSMerging(true), // Merge CORS preflight with actual requests
),
hosthttp.UseRequestManager(&RequestManager{}),
hosthttp.UseCorsHeader("X-API-Key", "X-Trace-ID"),
hosthttp.UseLogging(),
hosthttp.UseErrorHandler(func(w http.ResponseWriter, r *http.Request, err any) {
log.Printf("Error occurred: %v", err)
if fail, ok := err.(*failure.Failure); ok {
response.JSON.Failure(w, fail, http.StatusBadRequest)
} else {
response.JSON.Failure(w, map[string]any{
"message": "Internal Server Error",
"error": "Server error",
"trace_info": "Check logs or Jaeger for detailed trace information",
}, http.StatusInternalServerError)
}
}),
).
Run()
}