- Add bootstrap package to initialize application components including logger, tracer, and HTTP server. - Create config package to load runtime settings from environment variables. - Implement observability features including logging, metrics, and tracing. - Add health check and hello use cases with corresponding HTTP handlers. - Introduce middleware for request ID, access logging, metrics, and recovery. - Set up HTTP router with defined routes for health and hello endpoints. - Include tests for health and hello endpoints to ensure proper functionality. - Add OpenTelemetry collector configuration for trace exporting.
33 lines
806 B
Go
33 lines
806 B
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"switchbot-api/internal/transport/http/contextkeys"
|
|
)
|
|
|
|
type ErrorBody struct {
|
|
Error string `json:"error"`
|
|
TraceID string `json:"trace_id,omitempty"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
}
|
|
|
|
func WriteError(c *gin.Context, status int, message string) {
|
|
spanCtx := trace.SpanContextFromContext(c.Request.Context())
|
|
body := ErrorBody{Error: message}
|
|
if spanCtx.IsValid() {
|
|
body.TraceID = spanCtx.TraceID().String()
|
|
}
|
|
if requestID := contextkeys.RequestIDFromContext(c.Request.Context()); requestID != "" {
|
|
body.RequestID = requestID
|
|
}
|
|
c.JSON(status, body)
|
|
}
|
|
|
|
func WriteInternalError(c *gin.Context) {
|
|
WriteError(c, http.StatusInternalServerError, "internal server error")
|
|
}
|