Refactor configuration management by migrating to a modular config system
- Removed the old `infra.Config` structure and replaced it with a new modular configuration system in `pkg/config`. - Introduced a `Config` struct in `cmd/web/config.go` to hold command-specific configurations. - Updated configuration loading functions to utilize the new `LoadConfig` method with generics for type safety. - Adjusted various initialization functions in `cmd/web/bootstrap.go` and `cmd/web/main.go` to reflect the new configuration structure. - Cleaned up the `config.yaml` by removing obsolete fields and ensuring it aligns with the new configuration schema.
This commit is contained in:
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"tm/infra"
|
||||
"tm/pkg/authorization"
|
||||
"tm/pkg/config"
|
||||
"tm/pkg/logger"
|
||||
"tm/pkg/mongo"
|
||||
"tm/pkg/redis"
|
||||
@@ -16,17 +16,17 @@ import (
|
||||
)
|
||||
|
||||
// Init Application Configuration
|
||||
func initConfig() infra.Config {
|
||||
config, err := infra.LoadConfig(".")
|
||||
func initConfig() Config {
|
||||
conf, err := config.LoadConfig(".", &Config{})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Failed to load config: %v", err))
|
||||
}
|
||||
|
||||
return *config
|
||||
return *conf
|
||||
}
|
||||
|
||||
// Init Logger Service
|
||||
func initLogger(conf infra.LoggingConfig) logger.Logger {
|
||||
func initLogger(conf config.LoggingConfig) logger.Logger {
|
||||
return logger.NewLogger(&logger.Config{
|
||||
Level: conf.Level,
|
||||
Format: conf.Format,
|
||||
@@ -42,7 +42,7 @@ func initLogger(conf infra.LoggingConfig) logger.Logger {
|
||||
}
|
||||
|
||||
// Init MongoDB Connection Manager
|
||||
func initMongoDB(conf infra.MongoConfig, log logger.Logger) *mongo.ConnectionManager {
|
||||
func initMongoDB(conf config.MongoConfig, log logger.Logger) *mongo.ConnectionManager {
|
||||
// Convert infra.MongoConfig to mongo.ConnectionConfig
|
||||
connectionConfig := mongo.ConnectionConfig{
|
||||
URI: conf.URI,
|
||||
@@ -75,7 +75,7 @@ func initMongoDB(conf infra.MongoConfig, log logger.Logger) *mongo.ConnectionMan
|
||||
}
|
||||
|
||||
// initHTTPServer initializes the Echo HTTP server with middleware
|
||||
func initHTTPServer(conf infra.Config, log logger.Logger) *echo.Echo {
|
||||
func initHTTPServer(conf Config, log logger.Logger) *echo.Echo {
|
||||
e := echo.New()
|
||||
|
||||
// Configure Echo
|
||||
@@ -129,7 +129,7 @@ func initHTTPServer(conf infra.Config, log logger.Logger) *echo.Echo {
|
||||
}
|
||||
|
||||
// Init Redis Connection Manager
|
||||
func initRedis(conf infra.RedisConfig, log logger.Logger) redis.Client {
|
||||
func initRedis(conf config.RedisConfig, log logger.Logger) redis.Client {
|
||||
connectionConfig := &redis.Config{
|
||||
Host: conf.Host,
|
||||
Port: conf.Port,
|
||||
@@ -158,7 +158,7 @@ func initRedis(conf infra.RedisConfig, log logger.Logger) redis.Client {
|
||||
}
|
||||
|
||||
// Init Authorization Service
|
||||
func initAuthorizationService(conf infra.JWTConfig, redisClient redis.Client, log logger.Logger) authorization.AuthorizationService {
|
||||
func initAuthorizationService(conf JWTConfig, redisClient redis.Client, log logger.Logger) authorization.AuthorizationService {
|
||||
authConfig := &authorization.AuthorizationConfig{
|
||||
AccessTokenSecret: conf.AccessSecret,
|
||||
RefreshTokenSecret: conf.RefreshSecret,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"tm/pkg/config"
|
||||
)
|
||||
|
||||
// Config holds configuration for the web command
|
||||
type Config struct {
|
||||
Server config.ServerConfig `mapstructure:"server"`
|
||||
Database config.DatabaseConfig `mapstructure:"database"`
|
||||
Cache config.CacheConfig `mapstructure:"cache"`
|
||||
Logging config.LoggingConfig `mapstructure:"logging"`
|
||||
RateLimit config.RateLimitConfig `mapstructure:"rate_limiting"`
|
||||
UserAuth AuthConfig `mapstructure:"user_authorization"`
|
||||
CustomerAuth AuthConfig `mapstructure:"customer_authorization"`
|
||||
}
|
||||
|
||||
type AuthConfig struct {
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
AccessSecret string `mapstructure:"access_secret"`
|
||||
RefreshSecret string `mapstructure:"refresh_secret"`
|
||||
AccessExpiresIn int `mapstructure:"access_expires_in"`
|
||||
RefreshExpiresIn int `mapstructure:"refresh_expires_in"`
|
||||
}
|
||||
@@ -20,22 +20,6 @@ cache:
|
||||
db: 0
|
||||
pool_size: 10
|
||||
|
||||
queue:
|
||||
rabbitmq:
|
||||
url: "amqp://guest:guest@localhost:5672/"
|
||||
exchange: "tender_exchange"
|
||||
queues:
|
||||
scraping: "scraping_queue"
|
||||
processing: "processing_queue"
|
||||
notifications: "notifications_queue"
|
||||
|
||||
search:
|
||||
elasticsearch:
|
||||
urls: ["http://localhost:9200"]
|
||||
username: ""
|
||||
password: ""
|
||||
index_prefix: "tender_"
|
||||
|
||||
user_authorization:
|
||||
jwt:
|
||||
access_secret: "your-super-secret-access-token-key-here-make-it-long-and-secure"
|
||||
@@ -50,18 +34,6 @@ customer_authorization:
|
||||
access_expires_in: 3600 # 1 hour in seconds
|
||||
refresh_expires_in: 2592000 # 30 days in seconds
|
||||
|
||||
ai:
|
||||
openai:
|
||||
api_key: ""
|
||||
model: "gpt-3.5-turbo"
|
||||
max_tokens: 1000
|
||||
|
||||
scraping:
|
||||
user_agent: "TenderBot/1.0"
|
||||
timeout: 30s
|
||||
max_retries: 3
|
||||
delay_between_requests: 1s
|
||||
|
||||
logging:
|
||||
level: "info"
|
||||
format: "json"
|
||||
|
||||
+2
-2
@@ -80,8 +80,8 @@ func main() {
|
||||
}()
|
||||
|
||||
// Initialize authorization service
|
||||
userAuthService := initAuthorizationService(conf.UserAuthorization.JWT, redisClient, logger)
|
||||
customerAuthService := initAuthorizationService(conf.CustomerAuthorization.JWT, redisClient, logger)
|
||||
userAuthService := initAuthorizationService(conf.UserAuth.JWT, redisClient, logger)
|
||||
customerAuthService := initAuthorizationService(conf.CustomerAuth.JWT, redisClient, logger)
|
||||
|
||||
// Initialize repositories with MongoDB connection manager
|
||||
userRepository := user.NewUserRepository(mongoManager, logger)
|
||||
|
||||
Reference in New Issue
Block a user