-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
90 lines (81 loc) · 1.6 KB
/
conf.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
package config
import (
"log"
"os"
"sync"
"github.com/go-playground/validator/v10"
"github.com/mstgnz/cronjob/pkg"
"github.com/robfig/cron/v3"
)
var (
once sync.Once
mu sync.Mutex
instance *config
)
// context key type
type CKey string
type config struct {
DB *DB
ES *ES
Kraft *Kraft
Mail *pkg.Mail
Cron *cron.Cron
Cache *pkg.Cache
Log *Logger
Validador *validator.Validate
SecretKey string
QUERY map[string]string
Running int
Shutting bool
}
func App() *config {
once.Do(func() {
instance = &config{
DB: &DB{},
Cron: cron.New(),
Cache: pkg.NewCache(),
Log: &Logger{},
Validador: validator.New(),
// the secret key will change every time the application is restarted.
SecretKey: "asdf1234", //RandomString(8),
Mail: &pkg.Mail{
From: os.Getenv("MAIL_FROM"),
Name: os.Getenv("MAIL_FROM_NAME"),
Host: os.Getenv("MAIL_HOST"),
Port: os.Getenv("MAIL_PORT"),
User: os.Getenv("MAIL_USER"),
Pass: os.Getenv("MAIL_PASS"),
},
}
// Connect to Postgres DB
instance.DB.ConnectDatabase()
// Connect to Kafka Kraft
if kraft, err := newKafkaClient(); err != nil {
log.Println(err)
} else {
instance.Kraft = kraft
}
// Connect to Elastic Search
if es, err := newESClient(); err != nil {
log.Println(err)
} else {
instance.ES = es
}
})
return instance
}
func ShuttingWrapper(fn func()) {
if !App().Shutting {
fn()
}
}
func IncrementRunning() {
mu.Lock()
App().Running++
mu.Unlock()
}
func DecrementRunning() {
mu.Lock()
App().Running--
mu.Unlock()
}