-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
74 lines (74 loc) · 2.81 KB
/
Copy pathdoc.go
File metadata and controls
74 lines (74 loc) · 2.81 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
// Package dhttp provides HTTP utilities for microservices with integrated logging, tracing, and error handling.
//
// This package is designed for StreamingFast products and provides common HTTP handling patterns
// used across various microservices. It includes structured error responses, request validation,
// response writing utilities, and handler wrappers that automatically handle logging and tracing.
//
// # Handler Wrappers
//
// The package provides three main handler wrappers that simplify common HTTP patterns:
//
// - JSONHandler: Wraps functions that return JSON responses
// - RawHandler: Streams raw content from io.ReadCloser
// - DirectHandler: Provides direct response writer control with error handling
//
// Example using JSONHandler:
//
// http.Handle("/api/users", dhttp.JSONHandler(func(r *http.Request) (any, error) {
// // Your logic here
// return users, nil
// }))
//
// # Request Processing
//
// Extract and validate request data using ExtractRequest for URL/query parameters
// or ExtractJSONRequest for JSON payloads:
//
// type UserRequest struct {
// ID int `schema:"id"`
// Name string `schema:"name"`
// }
//
// var req UserRequest
// if err := dhttp.ExtractRequest(ctx, r, &req, dhttp.NoValidation); err != nil {
// // Handle validation error
// }
//
// # Response Writing
//
// Write responses with proper headers, logging, and tracing:
//
// dhttp.WriteJSON(ctx, w, responseData)
// dhttp.WriteError(ctx, w, err)
// dhttp.WriteText(ctx, w, "Hello World")
//
// # Error Handling
//
// Create structured HTTP errors with proper status codes:
//
// return dhttp.BadRequestError(ctx, nil, "invalid_input", "The request is invalid")
// return dhttp.NotFoundError(ctx, nil, "user_not_found", "User not found")
// return dhttp.InternalServerError(ctx, err, "database_error", "Database connection failed")
//
// The package provides pre-made error functions for all standard HTTP status codes:
//
// return dhttp.UnauthorizedError(ctx, nil, "auth_required", "Authentication required")
// return dhttp.ForbiddenError(ctx, nil, "access_denied", "Access denied")
// return dhttp.TooManyRequestsError(ctx, nil, "rate_limit", "Rate limit exceeded")
//
// See errors.go for the complete list of available HTTP error functions (4xx and 5xx).
//
// All HTTP errors are wrapped and can be easily used throughout your application.
// Error responses include trace IDs for debugging and follow a consistent JSON structure.
//
// # Utilities
//
// Additional utilities include:
//
// - RealIP: Extract the real client IP from requests (Google Cloud Load Balancer aware)
// - ForwardResponse: Forward HTTP responses from upstream services
// - EmptyBody: Return empty response bodies from JSON handlers
//
// The package integrates with OpenTelemetry for tracing and uses structured logging
// throughout all operations.
package dhttp