-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigLoader.go
66 lines (55 loc) · 1.53 KB
/
configLoader.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
package main
import (
"errors"
"fmt"
"github.com/joho/godotenv"
"os"
"strconv"
"time"
)
type Config struct {
dekanatDbDriverName string
kafkaHost string
secondaryDekanatDbDSN string
kafkaTimeout time.Duration
kafkaAttempts int
workerPoolSize int
}
func loadConfig(envFilename string) (Config, error) {
if envFilename != "" {
err := godotenv.Load(envFilename)
if err != nil {
return Config{}, errors.New(fmt.Sprintf("Error loading %s file: %s", envFilename, err))
}
}
kafkaTimeout, err := strconv.Atoi(os.Getenv("KAFKA_TIMEOUT"))
if kafkaTimeout == 0 || err != nil {
kafkaTimeout = 10
}
kafkaAttempts, err := strconv.Atoi(os.Getenv("KAFKA_ATTEMPTS"))
if kafkaAttempts == 0 || err != nil {
kafkaAttempts = 0
}
workerPoolSize, err := strconv.Atoi(os.Getenv("WORKER_POOL_SIZE"))
if workerPoolSize <= 0 || err != nil {
workerPoolSize = 4
}
config := Config{
dekanatDbDriverName: os.Getenv("DEKANAT_DB_DRIVER_NAME"),
secondaryDekanatDbDSN: os.Getenv("SECONDARY_DEKANAT_DB_DSN"),
kafkaHost: os.Getenv("KAFKA_HOST"),
kafkaTimeout: time.Second * time.Duration(kafkaTimeout),
kafkaAttempts: kafkaAttempts,
workerPoolSize: workerPoolSize,
}
if config.dekanatDbDriverName == "" {
config.dekanatDbDriverName = "firebirdsql"
}
if config.secondaryDekanatDbDSN == "" {
return Config{}, errors.New("empty SECONDARY_DEKANAT_DB_DSN")
}
if config.kafkaHost == "" {
return Config{}, errors.New("empty KAFKA_HOST")
}
return config, nil
}