PostgreSQL

/app/lib/database/postgres.go (4.3 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package database

import (
"context"
"fmt"
"strconv"

_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"

"{{{ .Package }}}/app/lib/telemetry"
"{{{ .Package }}}/app/util"
)

const defaultPostgreSQLSchema = "public"

var typePostgres = &DBType{Key: "postgres", Title: "PostgreSQL", Quote: `"`, Placeholder: "$", SupportsReturning: true}

type PostgresParams struct {
Host string `json:"host"`
Port int `json:"port,omitempty"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
Database string `json:"database,omitempty"`
Schema string `json:"schema,omitempty"`
MaxConns int `json:"maxConns,omitempty"`
Debug bool `json:"debug,omitempty"`
}

func PostgresParamsFromEnv(key string, defaultUser string, prefix string) *PostgresParams {
h := localhost
if x := util.GetEnv(prefix + "db_host"); x != "" {
h = x
}
p := 0
if x := util.GetEnv(prefix + "db_port"); x != "" {
px, _ := strconv.ParseInt(x, 10, 32)
p = int(px)
}
u := defaultUser
if x := util.GetEnv(prefix + "db_user"); x != "" {
u = x
}
pw := defaultUser
if x := util.GetEnv(prefix + "db_password"); x != "" {
pw = x
}
d := key
if x := util.GetEnv(prefix + "db_database"); x != "" {
d = x
}
s := defaultPostgreSQLSchema
if x := util.GetEnv(prefix + "db_schema"); x != "" {
s = x
}
mc := 16
if x := util.GetEnv(prefix + "db_max_connections"); x != "" {
mcx, _ := strconv.ParseInt(x, 10, 32)
mc = int(mcx)
}
debug := false
if x := util.GetEnv(prefix + "db_debug"); x != "" {
debug = x != falseKey
}
return &PostgresParams{Host: h, Port: p, Username: u, Password: pw, Database: d, Schema: s, MaxConns: mc, Debug: debug}
}

func OpenPostgres(ctx context.Context, key string, prefix string, logger util.Logger) (*Service, error) {
if key == "" {
key = util.AppKey
}
envParams := PostgresParamsFromEnv(key, util.AppKey, prefix)
if util.GetEnv("db_ssl") == util.BoolTrue {
serviceParams, err := PostgresParamsFromService()
if err != nil {
return nil, err
}
return OpenPostgresDatabaseSSL(ctx, key, envParams, serviceParams, logger)
}
return OpenPostgresDatabase(ctx, key, envParams, logger)
}

func OpenDefaultPostgres(ctx context.Context, logger util.Logger) (*Service, error) {
return OpenPostgres(ctx, "", "", logger)
}

func OpenPostgresDatabase(ctx context.Context, key string, params *PostgresParams, logger util.Logger) (*Service, error) {
_, span, logger := telemetry.StartSpan(ctx, "database:open", logger)
defer span.Complete()
host := params.Host
if host == "" {
host = localhost
}
port := params.Port
if port == 0 {
port = 5432
}
sch := defaultPostgreSQLSchema
if params.Schema != "" {
sch = params.Schema
}

const template = "postgres://%s:%s@%s:%d/%s?search_path=%s&application_name=%s"
url := fmt.Sprintf(template, params.Username, params.Password, host, port, params.Database, sch, key)

db, err := sqlx.Open("pgx", url)
if err != nil {
return nil, errors.Wrap(err, "error opening database")
}

db.SetMaxOpenConns(params.MaxConns)
db.SetMaxIdleConns(0)

logger = logger.With("svc", "database", "db", key)

return NewService(typePostgres, key, params.Database, params.Schema, params.Username, params.Debug, db, logger)
}

func OpenPostgresDatabaseSSL(ctx context.Context, key string, ep *PostgresParams, sp *PostgresServiceParams, logger util.Logger) (*Service, error) {
_, span, logger := telemetry.StartSpan(ctx, "database:openssl", logger)
defer span.Complete()

dbname := sp.Database
if dbname == "" {
dbname = ep.Database
}
schema := sp.Schema
if schema == "" {
schema = ep.Schema
}
if schema == "" {
schema = defaultPostgreSQLSchema
}

const template = "postgres://%s:%d/%s?search_path=%s&application_name=%s&user=%s&sslmode=%s&sslcert=%s&sslrootcert=%s&sslkey=%s"
url := fmt.Sprintf(template, sp.Host, sp.Port, dbname, schema, key, sp.Username, sp.SSLMode, sp.SSLCert, sp.SSLRootCert, sp.SSLKey)

db, err := sqlx.Open("pgx", url)
if err != nil {
return nil, errors.Wrap(err, "error opening database")
}

db.SetMaxOpenConns(ep.MaxConns)
db.SetMaxIdleConns(0)

logger = logger.With("svc", "database", "db", key)

return NewService(typePostgres, key, dbname, ep.Schema, sp.Username, ep.Debug, db, logger)
}