You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spiderman/pkg/config/config.go

134 lines
3.5 KiB

// Package config manages application configuration
package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
// Config holds all configuration for the application
type Config struct {
Server ServerConfig
Fabric FabricConfig
Log LogConfig
Environment EnvironmentConfig
}
// ServerConfig holds server configuration
type ServerConfig struct {
Host string
Port string
}
// FabricConfig holds Hyperledger Fabric configuration
type FabricConfig struct {
MSPID string
CryptoPath string
CertPath string
KeyPath string
TLSCertPath string
PeerEndpoint string
GatewayPeer string
ChannelName string
ChaincodeName string
}
// LogConfig holds logging configuration
type LogConfig struct {
Level string
Format string
}
// EnvironmentConfig holds environment-specific configuration
type EnvironmentConfig struct {
Environment string
APITimeout int
DBTimeout int
}
// LoadConfig loads configuration from environment variables and .env files
func LoadConfig() *Config {
// Try to load from config.env file first (for development)
if err := godotenv.Load("config.env"); err != nil {
// If config.env doesn't exist, try .env
if err := godotenv.Load(".env"); err != nil {
// If no .env files exist, that's okay - we'll use system environment variables
log.Printf("No .env file found, using system environment variables: %v", err)
}
}
// Build fabric paths
cryptoPath := getEnv("CRYPTO_PATH", "../../test-network/organizations/peerOrganizations/org1.example.com")
certPath := getEnv("CERT_PATH", "")
if certPath == "" {
certPath = cryptoPath + "/users/User1@org1.example.com/msp/signcerts"
}
keyPath := getEnv("KEY_PATH", "")
if keyPath == "" {
keyPath = cryptoPath + "/users/User1@org1.example.com/msp/keystore"
}
tlsCertPath := getEnv("TLS_CERT_PATH", "")
if tlsCertPath == "" {
tlsCertPath = cryptoPath + "/peers/peer0.org1.example.com/tls/ca.crt"
}
return &Config{
Server: ServerConfig{
Host: getEnv("HOST", "localhost"),
Port: getEnv("PORT", "8080"),
},
Fabric: FabricConfig{
MSPID: getEnv("MSP_ID", "Org1MSP"),
CryptoPath: cryptoPath,
CertPath: certPath,
KeyPath: keyPath,
TLSCertPath: tlsCertPath,
PeerEndpoint: getEnv("PEER_ENDPOINT", "dns:///localhost:7051"),
GatewayPeer: getEnv("GATEWAY_PEER", "peer0.org1.example.com"),
ChannelName: getEnv("CHANNEL_NAME", "mychannel"),
ChaincodeName: getEnv("CHAINCODE_NAME", "basic"),
},
Log: LogConfig{
Level: getEnv("LOG_LEVEL", "info"),
Format: getEnv("LOG_FORMAT", "text"),
},
Environment: EnvironmentConfig{
Environment: getEnv("ENVIRONMENT", "development"),
APITimeout: getEnvAsInt("API_TIMEOUT", 30),
DBTimeout: getEnvAsInt("DB_TIMEOUT", 10),
},
}
}
// getEnv gets an environment variable with a default value
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// getEnvAsInt gets an environment variable as integer with a default value
func getEnvAsInt(key string, defaultValue int) int {
valueStr := getEnv(key, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultValue
}
// IsProduction returns true if running in production environment
func (c *Config) IsProduction() bool {
return c.Environment.Environment == "production"
}
// IsDevelopment returns true if running in development environment
func (c *Config) IsDevelopment() bool {
return c.Environment.Environment == "development"
}