- 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.
35 lines
727 B
Go
35 lines
727 B
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"switchbot-api/internal/transport/http/response"
|
|
)
|
|
|
|
type ReadinessUsecase interface {
|
|
Check(ctx context.Context) error
|
|
}
|
|
|
|
type HealthHandler struct {
|
|
readiness ReadinessUsecase
|
|
}
|
|
|
|
func NewHealthHandler(readiness ReadinessUsecase) *HealthHandler {
|
|
return &HealthHandler{readiness: readiness}
|
|
}
|
|
|
|
func (h *HealthHandler) Healthz(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|
|
|
|
func (h *HealthHandler) Readyz(c *gin.Context) {
|
|
if err := h.readiness.Check(c.Request.Context()); err != nil {
|
|
response.WriteError(c, http.StatusServiceUnavailable, "service not ready")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|