feat: use caarlos0/env for config
This commit is contained in:
parent
ae24924f39
commit
b2a4b33e7d
1 changed files with 26 additions and 55 deletions
|
|
@ -1,56 +1,26 @@
|
|||
package config
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"github.com/caarlos0/env/v11"
|
||||
)
|
||||
|
||||
// Config holds the application configuration
|
||||
type Config struct {
|
||||
ServerAddress string
|
||||
DatabaseURL string
|
||||
DatabaseName string
|
||||
CollectionName string
|
||||
LogLevel string
|
||||
Environment string // Added environment field
|
||||
Version string // Application version
|
||||
ServerAddress string `env:"MCP_REGISTRY_SERVER_ADDRESS" envDefault:":8080"`
|
||||
DatabaseURL string `env:"MCP_REGISTRY_DATABASE_URL" envDefault:"mongodb://localhost:27017"`
|
||||
DatabaseName string `env:"MCP_REGISTRY_DATABASE_NAME" envDefault:"mcp-registry"`
|
||||
CollectionName string `env:"MCP_REGISTRY_COLLECTION_NAME" envDefault:"servers_v2"`
|
||||
LogLevel string `env:"MCP_REGISTRY_LOG_LEVEL" envDefault:"info"`
|
||||
Environment string `env:"MCP_REGISTRY_ENVIRONMENT" envDefault:"production"`
|
||||
Version string `env:"MCP_REGISTRY_VERSION" envDefault:"dev"`
|
||||
}
|
||||
|
||||
// NewConfig creates a new configuration with default values
|
||||
func NewConfig() *Config {
|
||||
// Get environment from OS environment variable or default to "production"
|
||||
env := os.Getenv("APP_ENV")
|
||||
if env == "" {
|
||||
env = "production"
|
||||
}
|
||||
|
||||
// Get version from environment variable or default to "dev"
|
||||
version := os.Getenv("APP_VERSION")
|
||||
if version == "" {
|
||||
version = "dev"
|
||||
}
|
||||
|
||||
// Get database URL from environment variable or default to localhost
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
dbURL = "localhost:5432"
|
||||
}
|
||||
|
||||
// Get database name from environment variable or default to "mcp_registry"
|
||||
dbName := os.Getenv("DATABASE_NAME")
|
||||
if dbName == "" {
|
||||
dbName = "mcp-registry"
|
||||
}
|
||||
// Get collection name from environment variable or default to "entries"
|
||||
collectionName := os.Getenv("COLLECTION_NAME")
|
||||
if collectionName == "" {
|
||||
collectionName = "servers_v2"
|
||||
}
|
||||
|
||||
return &Config{
|
||||
ServerAddress: ":8080",
|
||||
DatabaseURL: dbURL,
|
||||
DatabaseName: dbName,
|
||||
CollectionName: collectionName,
|
||||
LogLevel: "info",
|
||||
Environment: env,
|
||||
Version: version,
|
||||
var cfg Config
|
||||
err := env.Parse(&cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &cfg
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue