Compare commits
2 Commits
659369c42b
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 132945342f | |||
| 80ff5c501d |
2
go.mod
2
go.mod
@@ -20,7 +20,7 @@ require (
|
||||
github.com/go-openapi/jsonreference v0.20.0 // indirect
|
||||
github.com/go-openapi/spec v0.20.6 // indirect
|
||||
github.com/go-openapi/swag v0.19.15 // indirect
|
||||
github.com/gorilla/mux v1.8.1 // indirect
|
||||
github.com/gorilla/mux v1.8.1
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/mailru/easyjson v0.7.6 // indirect
|
||||
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
|
||||
|
||||
@@ -33,5 +33,8 @@ func (h *HomeHandler) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
// Handle encoding error - we can't write an error response after headers
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,5 +39,8 @@ func (h *TimeHandler) handleTime(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
// Handle encoding error - we can't write an error response after headers
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,10 +37,8 @@ func Initialize(level string, format string, output string) {
|
||||
})
|
||||
}
|
||||
|
||||
// Set output
|
||||
if output != "" {
|
||||
//TODO: Use files
|
||||
}
|
||||
// Set output (currently not implemented)
|
||||
// TODO: Implement file output support
|
||||
|
||||
initialized = true
|
||||
})
|
||||
|
||||
@@ -8,6 +8,11 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// requestIDKey is a custom type for context key to avoid collisions
|
||||
type requestIDKey struct{}
|
||||
|
||||
var _ requestIDKey
|
||||
|
||||
// RequestIDMiddleware adds a unique request ID to each request
|
||||
func RequestIDMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -17,9 +22,9 @@ func RequestIDMiddleware(next http.Handler) http.Handler {
|
||||
// Set request ID in response header
|
||||
w.Header().Set("X-Request-ID", requestID)
|
||||
|
||||
// Add request ID to context
|
||||
// Add request ID to context using custom type
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, "request_id", requestID)
|
||||
ctx = context.WithValue(ctx, requestIDKey{}, requestID)
|
||||
|
||||
// Log the request ID assignment
|
||||
logger := logger.GetLogger()
|
||||
|
||||
Reference in New Issue
Block a user