package main import ( "fmt" "net/http" "time" "tm/infra" "tm/pkg/logger" "tm/pkg/mongo" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" echoSwagger "github.com/swaggo/echo-swagger" ) // Init Application Configuration func initConfig() infra.Config { config, err := infra.LoadConfig(".") if err != nil { panic(fmt.Sprintf("Failed to load config: %v", err)) } return *config } // Init Logger Service func initLogger(conf infra.LoggingConfig) logger.Logger { return logger.NewLogger(&logger.Config{ Level: conf.Level, Format: conf.Format, Output: conf.Output, File: logger.FileConfig{ Path: conf.File.Path, MaxSize: conf.File.MaxSize, MaxBackups: conf.File.MaxBackups, MaxAge: conf.File.MaxAge, Compress: conf.File.Compress, }, }) } // Init MongoDB Connection Manager func initMongoDB(conf infra.MongoConfig, log logger.Logger) *mongo.ConnectionManager { // Convert infra.MongoConfig to mongo.ConnectionConfig connectionConfig := mongo.ConnectionConfig{ URI: conf.URI, Database: conf.Name, MaxPoolSize: uint64(conf.MaxPoolSize), MinPoolSize: 5, // Default minimum pool size MaxConnIdleTime: 30 * time.Minute, // Default idle time ConnectTimeout: conf.Timeout, ServerSelectionTimeout: 5 * time.Second, // Default server selection timeout } // Create MongoDB connection manager connectionManager, err := mongo.NewConnectionManager(connectionConfig, log) if err != nil { log.Error("Failed to initialize MongoDB connection", map[string]interface{}{ "error": err.Error(), "uri": conf.URI, "db": conf.Name, }) panic(fmt.Sprintf("Failed to initialize MongoDB connection: %v", err)) } log.Info("MongoDB connection manager initialized successfully", map[string]interface{}{ "database": conf.Name, "uri": conf.URI, "pool_size": conf.MaxPoolSize, }) return connectionManager } // initHTTPServer initializes the Echo HTTP server with middleware func initHTTPServer(conf infra.Config, log logger.Logger) *echo.Echo { e := echo.New() // Configure Echo // e.HideBanner = true // e.HidePort = true // Add middleware e.Use(middleware.Recover()) e.Use(middleware.RequestID()) e.Use(middleware.Logger()) e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"*"}, // TODO: Configure properly for production AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions}, AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization}, })) // Add custom middleware for logging e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { start := time.Now() // Process request err := next(c) // Log request details duration := time.Since(start) log.Info("HTTP request processed", map[string]interface{}{ "method": c.Request().Method, "path": c.Request().URL.Path, "status": c.Response().Status, "duration": duration.String(), "user_agent": c.Request().UserAgent(), "remote_ip": c.RealIP(), }) return err } }) // Add health check endpoint e.GET("/health", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]interface{}{ "status": "healthy", "time": time.Now().Unix(), "version": "1.0.0", }) }) // Add Swagger documentation endpoint e.GET("/swagger/*", echoSwagger.WrapHandler) log.Info("HTTP server initialized successfully", map[string]interface{}{ "middleware": []string{"recover", "request_id", "logger", "cors", "custom_logging"}, }) return e }