9119e2383e
- Introduced a new `config.yaml` file for managing server, database, cache, queue, AI, scraping, logging, and rate limiting configurations. - Updated `docker-compose.yml` to reflect the new server port (8081) and adjusted health check endpoints accordingly. - Modified `Dockerfile` to expose the new server port. - Updated `README.md` to reflect changes in server configuration and added documentation for the new configuration structure. - Added test scripts for server and Swagger documentation testing. - Refactored customer domain structure to align with new configuration settings and improve maintainability.
134 lines
3.7 KiB
Go
134 lines
3.7 KiB
Go
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
|
|
}
|