Files
cybertron/configs/config.go
2024-07-23 14:16:26 +05:30

89 lines
1.6 KiB
Go

package configs
import (
"cybertron/pkg/log"
"strings"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type AppConfig struct {
name string
env string
port int
metricsPort int
prometheus *Prometheus
postgres Postgres
timezone string
}
type MigConfig struct {
postgresConfig Postgres
}
var appConfig AppConfig
var migrationConfig MigConfig
func LoadConfig() {
readConfig()
appConfig = AppConfig{
name: getString("name", true),
env: getString("env", true),
port: getInt("port", true),
metricsPort: getInt("metrics.port", true),
prometheus: GetPrometheusConfig(),
postgres: getPostgresConfig(),
timezone: getString("timezone", true),
}
}
func LoadMigConfig() {
readConfig()
migrationConfig = MigConfig{postgresConfig: getPostgresConfig()}
}
func GetAppName() string {
return appConfig.name
}
func GetEnv() string {
return appConfig.env
}
func GetMetricsPort() int {
return appConfig.metricsPort
}
func GetPort() int {
return appConfig.port
}
func GetPostgresConfig() Postgres {
return appConfig.postgres
}
func GetPostgresMigConfig() Postgres {
return migrationConfig.postgresConfig
}
func GetTimezone() *string {
return &appConfig.timezone
}
func readConfig() {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.SetConfigFile("./configs/application.yml")
err := viper.ReadInConfig()
if err != nil {
log.Log.GetLog().Panic("Error while loading configuration", zap.Error(err))
}
}