This commit is contained in:
nquidox 2025-07-06 17:59:18 +03:00
commit 995ea60f34
11 changed files with 449 additions and 0 deletions

46
config/config.go Normal file
View file

@ -0,0 +1,46 @@
package config
type Config struct {
AppConf AppConfig
DBConf DatabaseConfig
}
type AppConfig struct {
Host string
Port string
LogLvl string
ApiPrefix string
GinMode string
}
type DatabaseConfig struct {
Host string
Port string
User string
Password string
SSLMode string
DBName string
LogLevel string
}
func NewConfig() *Config {
return &Config{
AppConf: AppConfig{
Host: getEnv("APP_HOST", ""),
Port: getEnv("APP_PORT", ""),
LogLvl: getEnv("APP_LOGLVL", ""),
ApiPrefix: getEnv("APP_API_PREFIX", ""),
GinMode: getEnv("APP_GIN_MODE", ""),
},
DBConf: DatabaseConfig{
Host: getEnv("DB_HOST", ""),
Port: getEnv("DB_PORT", ""),
User: getEnv("DB_USER", ""),
Password: getEnv("DB_PASSWORD", ""),
SSLMode: getEnv("DB_SSLMODE", ""),
DBName: getEnv("DB_NAME", ""),
LogLevel: getEnv("DB_LOGLEVEL", ""),
},
}
}

24
config/getEnv.go Normal file
View file

@ -0,0 +1,24 @@
package config
import (
"os"
"strconv"
)
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
num, err := strconv.Atoi(value)
if err != nil {
return fallback
}
return num
}
return fallback
}

35
config/logging.go Normal file
View file

@ -0,0 +1,35 @@
package config
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"path"
"runtime"
)
func LogSetup(lvl string) {
l, err := log.ParseLevel(lvl)
if err != nil {
log.SetLevel(log.DebugLevel)
}
log.SetFormatter(
&log.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
},
},
)
if l == log.DebugLevel {
log.SetLevel(l)
log.SetReportCaller(true)
} else {
log.SetLevel(l)
}
log.SetOutput(os.Stdout)
}