-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
120 lines (105 loc) · 3.5 KB
/
config.go
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
package main
import (
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)
type Config struct {
ExternalIP string
InternalIP string
Ports []int
EnableSRTP bool
RTPPortMin int
RTPPortMax int
RecordingDir string
SupportedVendors []string
SupportedCodecs []string
DefaultVendor string
TLSCertFile string // Path to the TLS certificate file
TLSKeyFile string // Path to the TLS key file
TLSPort int // Port for SIP over TLS
EnableTLS bool // Enable or disable TLS
LogLevel logrus.Level // Log level for structured logging
}
var (
config Config
)
func loadConfig() {
// Load environment variables
if err := godotenv.Load(); err != nil {
logger.Fatalf("Error loading .env file: %v", err)
}
// Load general configuration
config.EnableSRTP = os.Getenv("ENABLE_SRTP") == "true"
config.ExternalIP = os.Getenv("EXTERNAL_IP")
config.InternalIP = os.Getenv("INTERNAL_IP")
config.SupportedCodecs = strings.Split(os.Getenv("SUPPORTED_CODECS"), ",")
config.SupportedVendors = strings.Split(os.Getenv("SUPPORTED_VENDORS"), ",")
config.DefaultVendor = os.Getenv("SPEECH_VENDOR")
// Validate SPEECH_VENDOR
if config.DefaultVendor == "" {
logger.Warn("SPEECH_VENDOR not set; using 'google' as default")
config.DefaultVendor = "google" // Set to 'google' as a default fallback
}
// Load SIP ports
ports := strings.Split(os.Getenv("PORTS"), ",")
for _, portStr := range ports {
port, err := strconv.Atoi(portStr)
if err != nil {
logger.Fatalf("Invalid port in PORTS: %v", err)
}
config.Ports = append(config.Ports, port)
}
// Load RTP port range with error handling
var err error
config.RTPPortMin, err = strconv.Atoi(os.Getenv("RTP_PORT_MIN"))
if err != nil || config.RTPPortMin <= 0 {
logger.Warn("Invalid or missing RTP_PORT_MIN; setting default to 10000")
config.RTPPortMin = 10000
}
config.RTPPortMax, err = strconv.Atoi(os.Getenv("RTP_PORT_MAX"))
if err != nil || config.RTPPortMax <= config.RTPPortMin {
logger.Warn("Invalid or missing RTP_PORT_MAX; setting default to 20000")
config.RTPPortMax = 20000
}
// Load recording directory
config.RecordingDir = os.Getenv("RECORDING_DIR")
if config.RecordingDir == "" {
logger.Fatal("RECORDING_DIR not set in .env file")
}
// Load TLS configuration
config.TLSCertFile = os.Getenv("TLS_CERT_FILE")
config.TLSKeyFile = os.Getenv("TLS_KEY_FILE")
if config.TLSCertFile == "" || config.TLSKeyFile == "" {
logger.Warn("TLS_CERT_FILE or TLS_KEY_FILE not set, TLS support will be disabled")
}
// Load TLS port (if specified)
tlsPortStr := os.Getenv("TLS_PORT")
if tlsPortStr != "" {
config.TLSPort, err = strconv.Atoi(tlsPortStr)
if err != nil {
logger.Warn("Invalid TLS_PORT specified; TLS support will be disabled")
config.TLSPort = 0
}
} else {
config.TLSPort = 0 // Default to 0 if not specified
}
// Enable or disable TLS based on the environment variable
config.EnableTLS = os.Getenv("ENABLE_TLS") == "true"
// Set the log level from the environment variable
logLevelStr := os.Getenv("LOG_LEVEL")
if logLevelStr == "" {
logLevelStr = "info" // Default to "info" level
}
level, err := logrus.ParseLevel(logLevelStr)
if err != nil {
logger.Warnf("Invalid LOG_LEVEL '%s', defaulting to 'info'", logLevelStr)
config.LogLevel = logrus.InfoLevel
} else {
config.LogLevel = level
}
logger.SetLevel(config.LogLevel)
logger.Infof("Log level set to %s", logger.GetLevel().String())
}