-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
67 lines (56 loc) · 1.44 KB
/
module.go
File metadata and controls
67 lines (56 loc) · 1.44 KB
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
package pgx
import (
"fmt"
"github.com/go-modulus/modulus/logger"
"github.com/go-modulus/modulus/module"
)
type ModuleConfig struct {
DSN string `env:"PGX_DSN" comment:"Use this variable to set the DSN for the PGX connection. It overwrites the other PG_* variables. Example: PGX_DSN=postgres://postgres:foobar@localhost:5432/test?sslmode=disable"`
ConnectionConfig *ConnectionConfig `env:",prefix=PG_"`
}
type ConnectionConfig struct {
Host string `env:"HOST, default=localhost"`
Port int `env:"PORT, default=5432"`
User string `env:"USER, default=postgres"`
Password string `env:"PASSWORD, default=foobar"`
Database string `env:"DB_NAME, default=test"`
SslMode string `env:"SSL_MODE, default=disable"`
}
func (c ConnectionConfig) Dsn() string {
return fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=%s",
c.User,
c.Password,
c.Host,
c.Port,
c.Database,
c.SslMode,
)
}
func (c ModuleConfig) Dsn() string {
if c.DSN != "" {
return c.DSN
}
if c.ConnectionConfig == nil {
return ""
}
return c.ConnectionConfig.Dsn()
}
func NewModule() *module.Module {
return module.NewModule("modulus/pgx").
AddProviders(
NewPgxPool,
).
AddDependencies(
logger.NewModule(),
).
InitConfig(ModuleConfig{})
}
func NewManifesto() module.Manifesto {
return module.NewManifesto(
NewModule(),
"github.com/go-modulus/pgx",
"A wrapper for the pgx package to integrate it into the Modulus framework.",
"1.0.0",
)
}