-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
62 lines (50 loc) · 1.27 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
package config
import (
"fmt"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
var (
PORT = 0
BIND_ADDRESS = ""
DB_DRIVER = ""
DB_USER = ""
DB_PASS = ""
DB_NAME = ""
DB_PORT = 0
SSL_MODE = ""
TIMEZONE = ""
DB_URL = ""
)
func LoadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Http Server
PORT, err = strconv.Atoi(os.Getenv("API_PORT"))
if err != nil {
PORT = 8000
}
if os.Getenv("BIND_ADDRESS") == "" {
BIND_ADDRESS = "0.0.0.0"
} else {
BIND_ADDRESS = os.Getenv("BIND_ADDRESS")
}
// Database
DB_DRIVER = os.Getenv("DB_DRIVER")
DB_USER = os.Getenv("DB_USER")
DB_PASS = os.Getenv("DB_PASS")
DB_NAME = os.Getenv("DB_NAME")
DB_PORT, err = strconv.Atoi(os.Getenv("API_PORT"))
if err != nil {
DB_PORT = 5432
}
SSL_MODE = os.Getenv("SSL_MODE")
TIMEZONE = os.Getenv("TIMEZONE")
// DB_URL form the connection string for postgresql database
// "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
DB_URL = fmt.Sprintf("host=0.0.0.0 user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), os.Getenv("SSL_MODE"), os.Getenv("TIMEZONE"))
}