..
Viewing
config.go
123 lines (104 loc) • 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | package config
import (
"errors"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
)
type Config struct {
// Server settings
Port string
Environment string
SecretKey string
Debug bool
SecureCookie bool
// Mail server settings
MailServer struct {
IMAPHost string
IMAPPort int
SMTPHost string
SMTPPort int
UseStartTLS bool
}
// Authorization settings
AuthorizedDomains []string
}
// Load reads the environment variables and returns a Config struct
func Load() (*Config, error) {
// Load .env file if it exists
godotenv.Load()
config := &Config{}
// Server settings
config.Port = getEnv("PORT", "3000")
config.Environment = getEnv("ENV", "development")
config.SecretKey = getEnv("SECRET_KEY", "")
// Set Debug and SecureCookie based on environment
config.Debug = config.Environment == "development"
config.SecureCookie = config.Environment == "production"
// Mail server settings
config.MailServer.IMAPHost = getEnv("MAIL_IMAP_HOST", "")
config.MailServer.IMAPPort = getEnvAsInt("MAIL_IMAP_PORT", 993)
config.MailServer.SMTPHost = getEnv("MAIL_SMTP_HOST", "")
config.MailServer.SMTPPort = getEnvAsInt("MAIL_SMTP_PORT", 587)
config.MailServer.UseStartTLS = getEnvAsBool("MAIL_SMTP_USE_STARTTLS", true)
// Authorized domains
domains := getEnv("AUTHORIZED_DOMAINS", "")
if domains != "" {
config.AuthorizedDomains = splitAndTrim(domains)
}
// Validate required settings
if err := config.validate(); err != nil {
return nil, err
}
return config, nil
}
// validate checks if all required configuration is present
func (c *Config) validate() error {
if c.SecretKey == "" {
return errors.New("SECRET_KEY is required")
}
if c.MailServer.IMAPHost == "" {
return errors.New("MAIL_IMAP_HOST is required")
}
if c.MailServer.SMTPHost == "" {
return errors.New("MAIL_SMTP_HOST is required")
}
if len(c.AuthorizedDomains) == 0 {
return errors.New("AUTHORIZED_DOMAINS is required")
}
return nil
}
// Helper functions
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getEnvAsInt(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
func getEnvAsBool(key string, defaultValue bool) bool {
if value := os.Getenv(key); value != "" {
if boolValue, err := strconv.ParseBool(value); err == nil {
return boolValue
}
}
return defaultValue
}
func splitAndTrim(s string) []string {
parts := strings.Split(s, ",")
result := make([]string, 0, len(parts))
for _, part := range parts {
if trimmed := strings.TrimSpace(part); trimmed != "" {
result = append(result, trimmed)
}
}
return result
}
|
|