forked from dbenson24/iris-pgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irispgx.go
52 lines (43 loc) · 872 Bytes
/
irispgx.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
package irispgx
import (
"fmt"
"github.com/jackc/pgx"
"github.com/kataras/iris/v12"
)
type errorHandler func(iris.Context, string)
type Middleware struct {
Config Config
Pool *pgx.ConnPool
}
func main() {
fmt.Println("vim-go")
}
func (m *Middleware) Serve(ctx iris.Context) {
if m.Config.AttachPool {
ctx.Values().Set(m.Config.PoolCtxKey, m.Pool)
}
if m.Config.AttachConn {
conn, err := m.Pool.Acquire()
if err != nil {
fmt.Println("Error acquiring pool in: ", ctx.Path())
} else {
fmt.Print("Setting the Connection")
ctx.Values().Set(m.Config.ConnCtxKey, conn)
}
}
ctx.Next()
//_, ok := ctx.Get("pgx").(pgx.Conn)
}
func New(cfg ...Config) *Middleware {
var c Config
if len(cfg) == 0 {
c = Config{}
} else {
c = cfg[0]
}
pool, _ := pgx.NewConnPool(c.ConnPoolConfig)
return &Middleware{
Config: c,
Pool: pool,
}
}