/
client.go
50 lines (39 loc) · 976 Bytes
/
client.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
package pgkit
import (
"context"
"os"
"github.com/go-pg/pg/v10"
"github.com/tsw303005/Dcard-URL-Shortener/pkg/logkit"
"go.uber.org/zap"
)
type PGConfig struct {
URL string `long:"url" env:"URL" description:"the URL of PostgresSQL" required:"true"`
}
type PGClient struct {
*pg.DB
closeFunc func()
}
func (c *PGClient) Close() error {
if c.closeFunc != nil {
c.closeFunc()
}
return c.DB.Close()
}
func NewPGClient(ctx context.Context, conf *PGConfig) *PGClient {
if url := os.ExpandEnv(conf.URL); url != "" {
conf.URL = url
}
logger := logkit.FromContext(ctx).With(zap.String("url", conf.URL))
opts, err := pg.ParseURL(conf.URL)
if err != nil {
logger.Fatal("failed to parse PostgresSQL url", zap.Error(err))
}
db := pg.Connect(opts).WithContext(ctx)
if err := db.Ping(ctx); err != nil {
logger.Fatal("failed to ping PostgresSQL", zap.Error(err))
}
logger.Info("create PostgresSQL client suceessfully")
return &PGClient{
DB: db,
}
}