Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ jobs:
github_token: ${{ secrets.github_token }}
test:
runs-on: ubuntu-latest
container: golang:1.21-alpine3.18
container: golang:1.24.2-alpine3.21
services:
redis:
image: redis
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
steps:
- name: Install common dependencies
run: apk add --no-cache gcc libc-dev
- uses: actions/checkout@v4
- run: go test -v ./...
env:
REDIS_ADDR: redis:6379
DATABASE_ADDR: postgres:5432
DATABASE_USER: postgres
DATABASE_PASSWORD: postgres
DATABASE_DATABASE: postgres
DATABASE_SSL: false
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
vendor

# IDE integrations
.vscode
.vscode

config.yaml
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ redis:
```

### PostgreSQL
**Dependency:** `*pg.DB`
**Dependency:** `*bun.DB`
**Configuration:**
```yaml
database:
Expand All @@ -76,4 +76,6 @@ database:
password: ""
database: ""
pool: 10
ssl: true
path_to_ssl_root_cert: ""
```
69 changes: 69 additions & 0 deletions clients/bun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package clients

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)

type dbQueryHook struct {
logger *logrus.Entry
}

func (d *dbQueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
return ctx
}

func (d *dbQueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
d.logger.WithFields(logrus.Fields{
"query": string(event.Query),
"elapsed": time.Since(event.StartTime),
}).Info("query completed")
}

func NewPostgreSQL(config *viper.Viper, logger *logrus.Logger) *bun.DB {
config.SetDefault("database.pool", 10)
config.SetDefault("database.debug", false)
config.SetDefault("database.ssl", false)

dsn := fmt.Sprintf(
"postgres://%s:%s@%s/%s",
config.GetString("database.user"),
config.GetString("database.password"),
config.GetString("database.addr"),
config.GetString("database.database"),
)

if config.GetBool("database.ssl") {
dsn += "?sslmode=verify-ca"

if config.GetString("database.path_to_ssl_root_cert") != "" {
dsn += "&sslrootcert=" + config.GetString("database.path_to_ssl_root_cert")
}
} else {
dsn += "?sslmode=disable"
}

pgconn := pgdriver.NewConnector(pgdriver.WithDSN(dsn))

sqldb := sql.OpenDB(pgconn)
sqldb.SetMaxOpenConns(config.GetInt("database.pool"))

db := bun.NewDB(sqldb, pgdialect.New())

if config.GetBool("database.debug") {
logger := logger.WithField("module", "db")
db.AddQueryHook(&dbQueryHook{
logger: logger,
})
}

return db
}
37 changes: 37 additions & 0 deletions clients/bun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package clients

import (
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

func TestNewPostgreSQL(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

cfg := viper.New()
cfg.Set("database.addr", os.Getenv("DATABASE_ADDR"))
cfg.Set("database.user", os.Getenv("DATABASE_USER"))
cfg.Set("database.password", os.Getenv("DATABASE_PASSWORD"))
cfg.Set("database.database", os.Getenv("DATABASE_DATABASE"))
cfg.Set("database.ssl", os.Getenv("DATABASE_SSL"))
cfg.Set("database.path_to_ssl_root_cert", os.Getenv("PATH_TO_SSL_ROOT_CERT"))

logger := logrus.New()

db := NewPostgreSQL(cfg, logger)

var res string
err := db.QueryRow("SELECT 'hello'").Scan(&res)
if err != nil {
t.Error(err)
}

if res != "hello" {
t.Fail()
}
}
66 changes: 0 additions & 66 deletions clients/pg.go

This file was deleted.

27 changes: 14 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module github.com/cryptopay-dev/narada

go 1.21
go 1.24.2

require (
github.com/bsm/redislock v0.7.1
github.com/chapsuk/worker v1.0.0
github.com/getsentry/sentry-go v0.11.0
github.com/go-pg/pg/v10 v10.10.6
github.com/go-redis/redis/v8 v8.11.4
github.com/lib/pq v1.8.0
github.com/pkg/errors v0.9.1
github.com/pressly/goose v2.6.0+incompatible
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.10.0
github.com/uptrace/bun v1.2.11
github.com/uptrace/bun/dialect/pgdialect v1.2.11
github.com/uptrace/bun/driver/pgdriver v1.2.11
github.com/urfave/cli/v2 v2.3.0
go.uber.org/fx v1.14.2
gopkg.in/h2non/gock.v1 v1.0.15
Expand All @@ -28,7 +29,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -41,6 +41,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
Expand All @@ -50,20 +51,20 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/bufpool v0.1.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/dig v1.12.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
mellium.im/sasl v0.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mellium.im/sasl v0.3.2 // indirect
)
Loading