Init commit repo setup

This commit is contained in:
Lokesh Dugar
2024-07-23 14:16:26 +05:30
parent 72157608cd
commit 2012cd8940
29 changed files with 1012 additions and 1 deletions

35
configs/application.yml Normal file
View File

@@ -0,0 +1,35 @@
port: 9000
name: cybertron
env: local
metrics:
port: 4001
timezone: Asia/Kolkata
#DB config
db:
connection:
max:
lifetime: 3600s
idle:
time: 300s
connections:
max:
idle: 10
open: 300
username: postgres
password: admin
host: localhost
port: 5432
name: cybertron_dev
ssl:
mode: disable
#Prometheus config
prometheus:
app.name: cybertron
host: localhost
port: 4001
enabled: true
timeout: 10
flush.interval.in.ms: 200
histogram.buckets: 50.0,75.0,90.0,95.0,99.0

88
configs/config.go Normal file
View File

@@ -0,0 +1,88 @@
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))
}
}

61
configs/postgres.go Normal file
View File

@@ -0,0 +1,61 @@
package configs
import "fmt"
type Postgres struct {
maxIdleConnectionTime string
maxConnectionLifetime string
maxOpenConnections int
maxIdleConnections int
host string
name string
port int
userName string
password string
sSLMode string
}
func getPostgresConfig() Postgres {
return Postgres{
maxConnectionLifetime: getString("db.connection.max.lifetime", true),
maxIdleConnectionTime: getString("db.connection.max.idle.time", true),
maxIdleConnections: getInt("db.connections.max.idle", true),
maxOpenConnections: getInt("db.connections.max.open", true),
userName: getString("db.username", true),
password: getString("db.password", true),
name: getString("db.name", true),
host: getString("db.host", true),
port: getInt("db.port", true),
sSLMode: getString("db.ssl.mode", true),
}
}
func (p Postgres) GetPostgresUrl() string {
return fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=%s",
p.userName, p.password, p.host, p.port, p.name, p.sSLMode,
)
}
func (p Postgres) GetPostgresDsn() string {
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
p.host, p.port, p.userName, p.password, p.name, p.sSLMode,
)
}
func (p Postgres) GetMaxIdleConnectionTimeout() string {
return p.maxIdleConnectionTime
}
func (p Postgres) GetMaxConnectionLifetime() string {
return p.maxConnectionLifetime
}
func (p Postgres) GetMaxIdleConnections() int {
return p.maxIdleConnections
}
func (p Postgres) GetMaxOpenConnections() int {
return p.maxOpenConnections
}

31
configs/prometheus.go Normal file
View File

@@ -0,0 +1,31 @@
package configs
type Prometheus struct {
appName string
host string
port int
flushInterval int
timeout int
enabled bool
buckets []float64
}
func GetPrometheusConfig() *Prometheus {
return &Prometheus{
appName: getString("prometheus.api.name", false),
host: getString("prometheus.host", true),
port: getInt("prometheus.port", true),
enabled: getBool("prometheus.enabled", false),
timeout: getInt("prometheus.timeout", false),
flushInterval: getInt("prometheus.flush.interval.in.ms", false),
buckets: getFloatSlice("prometheus.histogram.buckets", true),
}
}
func (p *Prometheus) GetAppName() string {
return p.appName
}
func (p *Prometheus) GetBuckets() []float64 {
return p.buckets
}

63
configs/utils.go Normal file
View File

@@ -0,0 +1,63 @@
package configs
import (
"cybertron/pkg/log"
"strconv"
"strings"
"github.com/spf13/viper"
)
func getInt(key string, required bool) int {
if required {
checkKey(key)
}
return viper.GetInt(key)
}
func getString(key string, required bool) string {
if required {
checkKey(key)
}
return viper.GetString(key)
}
func getBool(key string, required bool) bool {
if required {
checkKey(key)
}
return viper.GetBool(key)
}
func getFloatSlice(key string, required bool) []float64 {
stringValues := getStringSlice(key, required)
var floatValues []float64
for _, val := range stringValues {
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Panic("config value is not float type, err : " + err.Error())
}
floatValues = append(floatValues, floatVal)
}
return floatValues
}
func checkKey(key string) {
if !viper.IsSet(key) {
log.Panic("Missing key: " + key)
}
}
func getStringSlice(key string, required bool) []string {
if required {
checkKey(key)
}
stringValue := viper.GetString(key)
return strings.Split(stringValue, ",")
}