init
This commit is contained in:
commit
ef105e1cac
22 changed files with 3001 additions and 0 deletions
83
internal/config/config.go
Normal file
83
internal/config/config.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTPAddr string
|
||||
DatabaseURL string
|
||||
SessionKey string
|
||||
AppEnv string
|
||||
AdminEmail string
|
||||
AdminPass string
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
if err := loadDotEnv(".env"); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warning: load .env: %v\n", err)
|
||||
}
|
||||
|
||||
return Config{
|
||||
HTTPAddr: getenv("HTTP_ADDR", ":8080"),
|
||||
DatabaseURL: getenv("DATABASE_URL", "postgres://postgres:postgres@localhost:5433/superbanka?sslmode=disable"),
|
||||
SessionKey: getenv("SESSION_KEY", "dev-session-key-change-me"),
|
||||
AppEnv: getenv("APP_ENV", "development"),
|
||||
AdminEmail: getenv("ADMIN_EMAIL", "admin@superbanka.local"),
|
||||
AdminPass: getenv("ADMIN_PASSWORD", "admin123456"),
|
||||
}
|
||||
}
|
||||
|
||||
func getenv(key, fallback string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
func loadDotEnv(path string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
key, value, ok := strings.Cut(line, "=")
|
||||
if !ok {
|
||||
return fmt.Errorf("line %d: missing '='", lineNumber)
|
||||
}
|
||||
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.TrimSpace(value)
|
||||
value = strings.Trim(value, `"'`)
|
||||
if key == "" {
|
||||
return fmt.Errorf("line %d: empty key", lineNumber)
|
||||
}
|
||||
|
||||
if _, exists := os.LookupEnv(key); exists {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
return fmt.Errorf("line %d: set %s: %w", lineNumber, key, err)
|
||||
}
|
||||
}
|
||||
|
||||
return scanner.Err()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue