first commit
This commit is contained in:
119
config/config.go
Normal file
119
config/config.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Env string // örn. development, production
|
||||
Port string
|
||||
DBUrl string
|
||||
DBPGUrl string
|
||||
JWTSecret string
|
||||
AppURL string // örn. https://api.example.com - e-posta doğrulama linkleri için kullanılır
|
||||
GoogleClientID string
|
||||
GoogleClientSecret string
|
||||
GithubClientID string
|
||||
GithubClientSecret string
|
||||
GoogleRedirectURL string
|
||||
GithubRedirectURL string
|
||||
ClientCallbackURL string
|
||||
OAuthRedirectURL string
|
||||
RedisUrl string
|
||||
AccessTokenExpireMinutes int
|
||||
RefreshTokenExpireDays int
|
||||
// E-posta Ayarları
|
||||
EmailHost string
|
||||
EmailPort string
|
||||
EmailHostUser string
|
||||
EmailHostPassword string
|
||||
EmailFrom string
|
||||
CorsDebug bool
|
||||
}
|
||||
|
||||
var AppConfig *Config
|
||||
|
||||
func LoadConfig() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
// Eğer proje kök dizininden çalıştırılmıyorsa (örn: cmd/app içinden), üst dizinleri kontrol et
|
||||
err = godotenv.Load(".env")
|
||||
//err = godotenv.Load("../../.env")
|
||||
}
|
||||
if err != nil {
|
||||
// Logger henüz hazır olmayabilir; varsa kullan
|
||||
if Logger != nil {
|
||||
Logger.Warn("Uyarı: .env dosyası yüklenirken hata oluştu, sistem ortam değişkenleriyle devam ediliyor", zapFieldsForEnvError(err)...)
|
||||
}
|
||||
}
|
||||
|
||||
AppConfig = &Config{
|
||||
Env: getEnv("APP_ENV", "development"),
|
||||
Port: getEnv("PORT", "8080"),
|
||||
DBUrl: getEnv("DB_URL", ""),
|
||||
DBPGUrl: getEnv("DB_URL_PG", ""),
|
||||
JWTSecret: getEnv("JWT_SECRET", "default_secret"),
|
||||
AppURL: getEnv("APP_URL", "http://localhost:8080"),
|
||||
GoogleClientID: getEnv("GOOGLE_CLIENT_ID", ""),
|
||||
GoogleClientSecret: getEnv("GOOGLE_CLIENT_SECRET", ""),
|
||||
GithubClientID: getEnv("GITHUB_CLIENT_ID", ""),
|
||||
GithubClientSecret: getEnv("GITHUB_CLIENT_SECRET", ""),
|
||||
GoogleRedirectURL: getEnv("GOOGLE_REDIRECT_URL", "http://localhost:8080/api/v1/auth/google/callback"),
|
||||
GithubRedirectURL: getEnv("GITHUB_REDIRECT_URL", "http://localhost:8080/api/v1/auth/github/callback"),
|
||||
ClientCallbackURL: getEnv("CLIENT_CALLBACK_URL", ""),
|
||||
OAuthRedirectURL: getEnv("OAUTH_REDIRECT_URL", ""),
|
||||
RedisUrl: getEnv("REDIS_URL", ""),
|
||||
AccessTokenExpireMinutes: getEnvAsInt("ACCESS_TOKEN_EXPIRE_MINUTES", 120), // Varsayılan 120 dakika
|
||||
RefreshTokenExpireDays: getEnvAsInt("REFRESH_TOKEN_EXPIRE_DAYS", 30), // Varsayılan 30 gün
|
||||
|
||||
// E-posta Varsayılanları
|
||||
EmailHost: getEnv("EMAIL_HOST", "localhost"),
|
||||
EmailPort: getEnv("EMAIL_PORT", "1025"),
|
||||
EmailHostUser: getEnv("EMAIL_HOST_USER", ""),
|
||||
EmailHostPassword: getEnv("EMAIL_HOST_PASSWORD", ""),
|
||||
EmailFrom: getEnv("EMAIL_FROM", "noreply@gauth.local"),
|
||||
CorsDebug: getEnvAsBool("CORS_DEBUG", false),
|
||||
}
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value, exists := os.LookupEnv(key); exists {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getEnvAsInt(key string, fallback int) int {
|
||||
valueStr := getEnv(key, "")
|
||||
if valueStr == "" {
|
||||
return fallback
|
||||
}
|
||||
value, err := strconv.Atoi(valueStr)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvAsBool(key string, fallback bool) bool {
|
||||
valueStr := strings.TrimSpace(getEnv(key, ""))
|
||||
if valueStr == "" {
|
||||
return fallback
|
||||
}
|
||||
value, err := strconv.ParseBool(valueStr)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func zapFieldsForEnvError(err error) []zap.Field {
|
||||
return []zap.Field{
|
||||
zap.String("error", err.Error()),
|
||||
}
|
||||
}
|
||||
39
config/loger.go
Normal file
39
config/loger.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var Logger *zap.Logger
|
||||
|
||||
func init() {
|
||||
// initialize default logger early so other packages can use it in their init functions
|
||||
LogerAres()
|
||||
}
|
||||
|
||||
func LogerAres() {
|
||||
logFile, err := os.Create("./info.log")
|
||||
if err != nil {
|
||||
// Fallback: sadece konsola yaz
|
||||
core := zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
||||
zapcore.AddSync(os.Stdout),
|
||||
zapcore.DebugLevel,
|
||||
)
|
||||
Logger = zap.New(core, zap.AddCaller())
|
||||
Logger.Warn("info.log açılamadı, sadece konsola yazılıyor", zap.Error(err))
|
||||
return
|
||||
}
|
||||
// Hem dosyaya hem konsola yaz (logları görebilirsin)
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||
multiOut := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(logFile))
|
||||
core := zapcore.NewCore(encoder, multiOut, zapcore.DebugLevel)
|
||||
Logger = zap.New(core, zap.AddCaller())
|
||||
Logger.Info("Logger başlatıldı (konsol + info.log)")
|
||||
}
|
||||
Reference in New Issue
Block a user