/
postgres.go
99 lines (77 loc) · 2.44 KB
/
postgres.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 The Ledger Authors
//
// Licensed under the AGPL, Version 3.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.gnu.org/licenses/agpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgres
import (
"context"
"runtime"
"strings"
"time"
"github.com/danielnegri/tokenapi-go/errors"
"github.com/danielnegri/tokenapi-go/ledger"
"github.com/danielnegri/tokenapi-go/storage"
"github.com/danielnegri/tokenapi-go/valid"
"github.com/go-pg/pg/v10"
)
const (
DefaultURL = "postgres://localhost:5432/ledger"
)
type SecretToken struct {
tableName struct{} `pg:"secret_tokens,alias:tokens"`
Data ledger.Token `pg:"data,pk"`
}
type Postgres struct {
db *pg.DB
}
var _ storage.Storage = (*Postgres)(nil)
// Connect parses a database URL into options that can be used to connect to PostgreSQL.
func Connect(opt *pg.Options) (*Postgres, error) {
const op errors.Op = "storage/postgres.Connect"
if opt == nil {
return nil, errors.E(op, errors.Internal, "invalid database config")
}
if opt.MaxConnAge == 0 {
opt.MaxConnAge = 10 * time.Minute
}
if opt.PoolSize == 0 {
opt.PoolSize = runtime.NumCPU() * 2
}
db := pg.Connect(opt)
db.AddQueryHook(DebugHook{})
return &Postgres{db: db}, nil
}
func (p *Postgres) Insert(ctx context.Context, token ledger.Token) error {
const op errors.Op = "storage/postgres.Insert"
// This validation can be removed once it is enforce by database as well.
// Although, it's much slower.
if err := valid.Token(token); err != nil {
return err
}
if err := p.db.Insert(&SecretToken{Data: token}); err != nil {
if strings.Contains(err.Error(), "duplicate key value") {
return errors.E(op, token, errors.Duplicate)
}
if strings.Contains(err.Error(), "violates check constraint") {
return errors.E(op, token, errors.Invalid)
}
return errors.E(op, token, errors.Internal, err)
}
return nil
}
func (p *Postgres) Check(ctx context.Context) error {
const op errors.Op = "storage/postgres.Check"
if err := p.db.Ping(ctx); err != nil {
return errors.E(op, errors.Internal, err)
}
return nil
}