Use config from .env and update api

This commit is contained in:
2026-01-24 18:09:24 +01:00
parent 1e02e659ab
commit 1c26770db0
11 changed files with 171 additions and 44 deletions

66
src/config/config.go Normal file
View File

@@ -0,0 +1,66 @@
package config
import (
"os"
"strconv"
"time"
)
type Config struct {
Server struct {
Port string
Mode string // gin.Mode: debug, release, test
}
JWT struct {
Secret string
}
Sync struct {
Interval time.Duration
}
Admin struct {
Email string
Password string
}
SheetsDirectory string
}
var AppConfig *Config
func Load() {
cfg := &Config{}
// Server configuration
cfg.Server.Port = getEnv("SERVER_PORT", ":8080")
cfg.Server.Mode = getEnv("GIN_MODE", "debug")
// JWT configuration
cfg.JWT.Secret = getEnv("JWT_SECRET", "sheetless-default-jwt-secret-please-change")
// Sync configuration
syncMinutes := getEnvInt("SYNC_INTERVAL_MINUTES", 1)
cfg.Sync.Interval = time.Duration(syncMinutes) * time.Minute
// Admin configuration
cfg.Admin.Email = getEnv("ADMIN_EMAIL", "admin@admin.com")
cfg.Admin.Password = getEnv("ADMIN_PASSWORD", "sheetless")
cfg.SheetsDirectory = getEnv("SHEETS_DIRECTORY", "./sheets_directory")
AppConfig = cfg
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getEnvInt(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}