This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
env.go
92 lines (83 loc) · 2.35 KB
/
env.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
package baseproject
import (
"github.com/gorilla/securecookie"
"github.com/uberswe/golang-base-project/config"
"github.com/uberswe/golang-base-project/text"
"log"
"os"
"strconv"
)
func loadEnvVariables() (c config.Config) {
c.Port = ":8080"
if os.Getenv("PORT") != "" {
c.Port = os.Getenv("PORT")
}
c.BaseURL = "https://golangbase.com/"
if os.Getenv("BASE_URL") != "" {
c.BaseURL = os.Getenv("BASE_URL")
}
// A random secret will be generated when the application starts if no secret is provided. It is highly recommended providing a secret.
c.CookieSecret = string(securecookie.GenerateRandomKey(64))
if os.Getenv("COOKIE_SECRET") != "" {
c.CookieSecret = os.Getenv("COOKIE_SECRET")
}
c.Database = "sqlite"
if os.Getenv("DATABASE") != "" {
c.Database = os.Getenv("DATABASE")
}
if os.Getenv("DATABASE_NAME") != "" {
c.DatabaseName = os.Getenv("DATABASE_NAME")
}
if os.Getenv("DATABASE_HOST") != "" {
c.DatabaseHost = os.Getenv("DATABASE_HOST")
}
if os.Getenv("DATABASE_PORT") != "" {
c.DatabasePort = os.Getenv("DATABASE_PORT")
}
if os.Getenv("DATABASE_USERNAME") != "" {
c.DatabaseUsername = os.Getenv("DATABASE_USERNAME")
}
if os.Getenv("DATABASE_PASSWORD") != "" {
c.DatabasePassword = os.Getenv("DATABASE_PASSWORD")
}
if os.Getenv("SMTP_USERNAME") != "" {
c.SMTPUsername = os.Getenv("SMTP_USERNAME")
}
if os.Getenv("SMTP_PASSWORD") != "" {
c.SMTPPassword = os.Getenv("SMTP_PASSWORD")
}
if os.Getenv("SMTP_HOST") != "" {
c.SMTPHost = os.Getenv("SMTP_HOST")
}
if os.Getenv("SMTP_PORT") != "" {
c.SMTPPort = os.Getenv("SMTP_PORT")
}
if os.Getenv("SMTP_SENDER") != "" {
c.SMTPSender = os.Getenv("SMTP_SENDER")
}
c.RequestsPerMinute = 5
if os.Getenv("REQUESTS_PER_MINUTE") != "" {
i, err := strconv.Atoi(os.Getenv("REQUESTS_PER_MINUTE"))
if err != nil {
log.Fatalln(err)
return
}
c.RequestsPerMinute = i
}
// CacheParameter is added to the end of static file urls to prevent caching old versions
c.CacheParameter = text.RandomString(10)
if os.Getenv("CACHE_PARAMETER") != "" {
c.CacheParameter = os.Getenv("CACHE_PARAMETER")
}
// CacheMaxAge is how many seconds to cache static assets, 1 year by default
c.CacheMaxAge = 31536000
if os.Getenv("CACHE_MAX_AGE") != "" {
i, err := strconv.Atoi(os.Getenv("CACHE_MAX_AGE"))
if err != nil {
log.Fatalln(err)
return
}
c.CacheMaxAge = i
}
return c
}