mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
IsProd bool
|
|
AppHost string
|
|
Version string
|
|
LogLevel string
|
|
AppPort int
|
|
DBHost string
|
|
DBUser string
|
|
DBPassword string
|
|
DBName string
|
|
DBPort int
|
|
JWTSecret string
|
|
JWTAccessExp int
|
|
JWTRefreshExp int
|
|
JWTResetPasswordExp int
|
|
JWTVerifyEmailExp int
|
|
RedisURL string
|
|
CORSAllowOrigins []string
|
|
CORSAllowMethods []string
|
|
CORSAllowHeaders []string
|
|
CORSExposeHeaders []string
|
|
CORSAllowCredentials bool
|
|
CORSMaxAge int
|
|
)
|
|
|
|
func init() {
|
|
loadConfig()
|
|
|
|
// server configuration
|
|
IsProd = viper.GetString("APP_ENV") == "prod"
|
|
AppHost = viper.GetString("APP_HOST")
|
|
AppPort = viper.GetInt("APP_PORT")
|
|
Version = viper.GetString("VERSION")
|
|
LogLevel = viper.GetString("LOG_LEVEL")
|
|
|
|
// database configuration
|
|
DBHost = viper.GetString("DB_HOST")
|
|
DBUser = viper.GetString("DB_USER")
|
|
DBPassword = viper.GetString("DB_PASSWORD")
|
|
DBName = viper.GetString("DB_NAME")
|
|
DBPort = viper.GetInt("DB_PORT")
|
|
|
|
// jwt configuration
|
|
JWTSecret = viper.GetString("JWT_SECRET")
|
|
JWTAccessExp = viper.GetInt("JWT_ACCESS_EXP_MINUTES")
|
|
JWTRefreshExp = viper.GetInt("JWT_REFRESH_EXP_DAYS")
|
|
JWTResetPasswordExp = viper.GetInt("JWT_RESET_PASSWORD_EXP_MINUTES")
|
|
JWTVerifyEmailExp = viper.GetInt("JWT_VERIFY_EMAIL_EXP_MINUTES")
|
|
|
|
//Cors
|
|
CORSAllowOrigins = parseList("CORS_ALLOW_ORIGINS")
|
|
CORSAllowMethods = parseListWithDefault("CORS_ALLOW_METHODS", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
|
CORSAllowHeaders = parseListWithDefault("CORS_ALLOW_HEADERS", "Content-Type,Authorization,X-Requested-With")
|
|
CORSExposeHeaders = parseList("CORS_EXPOSE_HEADERS")
|
|
CORSAllowCredentials = viper.GetBool("CORS_ALLOW_CREDENTIALS")
|
|
CORSMaxAge = viper.GetInt("CORS_MAX_AGE")
|
|
|
|
// Redis
|
|
RedisURL = viper.GetString("REDIS_URL")
|
|
}
|
|
|
|
func loadConfig() {
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetConfigFile(".env")
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
utils.Log.Info("Config file loaded from .env")
|
|
} else {
|
|
utils.Log.Warn("No .env file found, using environment variables only")
|
|
}
|
|
}
|
|
|
|
func parseList(key string) []string {
|
|
raw := strings.TrimSpace(viper.GetString(key))
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
if strings.HasPrefix(raw, "[") {
|
|
var arr []string
|
|
if json.Unmarshal([]byte(raw), &arr) == nil {
|
|
for i := range arr {
|
|
arr[i] = strings.TrimSpace(arr[i])
|
|
}
|
|
return arr
|
|
}
|
|
}
|
|
parts := strings.Split(raw, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func parseListWithDefault(key, def string) []string {
|
|
if v := parseList(key); len(v) > 0 {
|
|
return v
|
|
}
|
|
// fallback ke default CSV
|
|
parts := strings.Split(def, ",")
|
|
for i := range parts {
|
|
parts[i] = strings.TrimSpace(parts[i])
|
|
}
|
|
return parts
|
|
}
|