- Kompletna Go implementacija licencnog servera (19 Go fajlova) - Klijentski API: activate, deactivate, validate - Admin API: CRUD licence, stats, audit log - Admin dashboard: htmx + Go templates - RSA-2048 potpisivanje licencnih podataka - Rate limiting i API key autentifikacija - MySQL migracije i seed podaci (ESIR, ARV, LIGHT_TICKET) - Unit testovi: keygen, crypto, model, middleware (24 testa) - Dokumentacija: SPEC.md, ARCHITECTURE.md, SETUP.md, API.md, TESTING.md, README.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
Env string
|
|
DBHost string
|
|
DBPort string
|
|
DBName string
|
|
DBUser string
|
|
DBPass string
|
|
AdminAPIKey string
|
|
AdminPassword string
|
|
SessionSecret string
|
|
RSAPrivateKey string
|
|
RateLimitActivate string
|
|
RateLimitValidate string
|
|
LogLevel string
|
|
}
|
|
|
|
func Load() *Config {
|
|
loadEnvFile(".env")
|
|
|
|
return &Config{
|
|
Port: getEnv("APP_PORT", "8090"),
|
|
Env: getEnv("APP_ENV", "development"),
|
|
DBHost: getEnv("DB_HOST", "localhost"),
|
|
DBPort: getEnv("DB_PORT", "3306"),
|
|
DBName: getEnv("DB_NAME", "dal_license_db"),
|
|
DBUser: getEnv("DB_USER", "license"),
|
|
DBPass: getEnv("DB_PASS", ""),
|
|
AdminAPIKey: getEnv("ADMIN_API_KEY", ""),
|
|
AdminPassword: getEnv("ADMIN_PASSWORD", "admin123"),
|
|
SessionSecret: getEnv("SESSION_SECRET", "change-me"),
|
|
RSAPrivateKey: getEnv("RSA_PRIVATE_KEY_PATH", "./crypto/private.pem"),
|
|
RateLimitActivate: getEnv("RATE_LIMIT_ACTIVATE", "10"),
|
|
RateLimitValidate: getEnv("RATE_LIMIT_VALIDATE", "60"),
|
|
LogLevel: getEnv("LOG_LEVEL", "info"),
|
|
}
|
|
}
|
|
|
|
func (c *Config) DSN() string {
|
|
return c.DBUser + ":" + c.DBPass + "@tcp(" + c.DBHost + ":" + c.DBPort + ")/" + c.DBName + "?parseTime=true&charset=utf8mb4&multiStatements=true"
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func loadEnvFile(path string) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
parts := strings.SplitN(line, "=", 2)
|
|
if len(parts) == 2 {
|
|
key := strings.TrimSpace(parts[0])
|
|
val := strings.TrimSpace(parts[1])
|
|
if os.Getenv(key) == "" {
|
|
os.Setenv(key, val)
|
|
}
|
|
}
|
|
}
|
|
}
|