Nik Afiq 657b6aeb22 feat: implement initial application structure with health and hello endpoints
- 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.
2026-03-05 21:22:43 +09:00

41 lines
1.1 KiB
Go

package httptransport
import (
"log/slog"
"net/http"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel/trace"
"switchbot-api/internal/app/observability"
"switchbot-api/internal/transport/http/handlers"
"switchbot-api/internal/transport/http/middleware"
)
type Dependencies struct {
Logger *slog.Logger
Tracer trace.Tracer
HTTPMetrics *observability.HTTPMetrics
PrometheusHandler http.Handler
HealthHandler *handlers.HealthHandler
HelloHandler *handlers.HelloHandler
}
func NewRouter(deps Dependencies) *gin.Engine {
router := gin.New()
router.RedirectTrailingSlash = false
router.Use(middleware.RequestID())
router.Use(middleware.Tracing(deps.Tracer))
router.Use(middleware.Metrics(deps.HTTPMetrics))
router.Use(middleware.AccessLog(deps.Logger))
router.Use(middleware.Recovery(deps.Logger))
router.GET("/healthz", deps.HealthHandler.Healthz)
router.GET("/readyz", deps.HealthHandler.Readyz)
router.GET("/hello", deps.HelloHandler.Hello)
router.GET("/metrics", gin.WrapH(deps.PrometheusHandler))
return router
}