-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
70 lines (58 loc) · 1.1 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
package goblockapi
import (
"os"
"github.com/agonist/goblockapi/evm"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type App struct {
Rpc *evm.Client
Rdb *redis.Client
Db *gorm.DB
}
func Init() *App {
loadEnv()
redis := setupRedis()
db := setupDb()
client := evm.New(os.Getenv("RPC_URL"))
app := &App{
Rpc: client,
Rdb: redis,
Db: db,
}
return app
}
func setupRedis() *redis.Client {
redis := redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_ADDR"),
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
return redis
}
func setupDb() *gorm.DB {
dsn := os.Getenv("DB_DSN")
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to the db")
}
err = db.AutoMigrate(&User{})
if err != nil {
panic("failed ti run migrations")
}
return db
}
func loadEnv() {
env := os.Getenv("APP_ENV")
if "" == env {
env = "development"
}
godotenv.Load(".env." + env + ".local")
if "test" != env {
godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load()
}