-
Notifications
You must be signed in to change notification settings - Fork 1
/
toolkit.go
78 lines (67 loc) · 2.38 KB
/
toolkit.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
package toolkit
import (
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"github.com/fastbill/go-service-toolkit/v4/cache"
"github.com/fastbill/go-service-toolkit/v4/database"
"github.com/fastbill/go-service-toolkit/v4/envloader"
"github.com/fastbill/go-service-toolkit/v4/observance"
"github.com/fastbill/go-service-toolkit/v4/server"
)
// MustLoadEnvs checks and loads environment variables from the given folder.
func MustLoadEnvs(folderPath string) {
err := envloader.LoadEnvs(folderPath)
if err != nil {
panic(err)
}
}
// ObsConfig aliases observance.Config so it will not be necessary to import the observance package for the setup process.
type ObsConfig = observance.Config
// DBConfig aliases database.Config so it will not be necessary to import the database package for the setup process.
type DBConfig = database.Config
// MustNewObs creates a new observability instance.
// It includes the properties "Logger", a Logrus logger that fulfils the Logger interface
// and "Metrics", a Prometheus Client that fulfils the Measurer interface.
func MustNewObs(config ObsConfig) *observance.Obs {
obs, err := observance.NewObs(config)
if err != nil {
panic(err)
}
return obs
}
// MustNewCache creates a new REDIS cache client that fulfils the Cache interface.
func MustNewCache(host string, port string, prefix string) *cache.RedisClient {
redisCache, err := cache.NewRedis(host, port, prefix)
if err != nil {
panic(err)
}
return redisCache
}
// MustSetupDB creates a new GORM client.
func MustSetupDB(config DBConfig, logger observance.Logger) *gorm.DB {
db, err := database.SetupGORM(config, logger)
if err != nil {
panic(err)
}
return db
}
// MustEnsureDBMigrations checks which migration was the last one that was executed and performs all following migrations.
func MustEnsureDBMigrations(folderPath string, config DBConfig) {
err := database.EnsureMigrations(folderPath, config)
if err != nil {
panic(err)
}
}
// MustNewServer sets up a new Echo server.
func MustNewServer(obs *observance.Obs, CORSOrigins string, timeout ...string) (*echo.Echo, chan struct{}) {
echoServer, connectionsClosed, err := server.New(obs, CORSOrigins, timeout...)
if err != nil {
panic(err)
}
return echoServer, connectionsClosed
}
// CloseDatabase closes the database instance used by GORM.
// db.Close() was removed in GORM v2.
func CloseDatabase(db *gorm.DB) error {
return database.Close(db)
}