Skip to content

Commit 643d597

Browse files
authored
feat: implement caching (#40)
* chore(docker-compose): add redis * connect to redis and do something * refactor(usercache): build the redis key with string builder * refactor(usercache): change naming * feat(usersrv): add caching for CheckIfUserIsActivated * refactor(e2e): handle errors consistently while setting up dbs * refactor(usercache): set custom ttl for cache * fixup! refactor(usercache): set custom ttl for cache * feat(usersrv): actually cache things * fixup! feat(usersrv): actually cache things * refactor: update redis connector * fixup! refactor: update redis connector * refactor(e2e): connect to redis via it's own helper function * refactor(usersrv): make linter happy again * refactor(e2e): refactor, naming mainly * e2e: reformat * fix(e2e): stop redis on tests end * refactor(e2e): setup tests deps in less lines * refactor(e2e): renaming
1 parent 9d9e251 commit 643d597

File tree

10 files changed

+250
-47
lines changed

10 files changed

+250
-47
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ POSTGRES_DATABASE=onasty
2020
POSTGRESQL_DSN="postgres://$POSTGRES_USERNAME:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DATABASE?sslmode=disable"
2121
MIGRATION_DSN="postgres://$POSTGRES_USERNAME:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/$POSTGRES_DATABASE?sslmode=disable"
2222

23+
REDIS_ADDR="redis:6379"
24+
CACHE_USERS_TTL=1h
25+
2326
MAILGUN_FROM=onasty@mail.com
2427
MAILGUN_DOMAI='<domain>'
2528
MAILGUN_API_KEY='<token>'

cmd/server/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/olexsmir/onasty/internal/store/psql/userepo"
2424
"github.com/olexsmir/onasty/internal/store/psql/vertokrepo"
2525
"github.com/olexsmir/onasty/internal/store/psqlutil"
26+
"github.com/olexsmir/onasty/internal/store/rdb"
27+
"github.com/olexsmir/onasty/internal/store/rdb/usercache"
2628
httptransport "github.com/olexsmir/onasty/internal/transport/http"
2729
"github.com/olexsmir/onasty/internal/transport/http/httpserver"
2830
"github.com/olexsmir/onasty/internal/transport/http/ratelimit"
@@ -61,6 +63,11 @@ func run(ctx context.Context) error {
6163
return err
6264
}
6365

66+
redisDB, err := rdb.Connect(ctx, cfg.RedisAddr, cfg.RedisPassword, cfg.RedisDB)
67+
if err != nil {
68+
return err
69+
}
70+
6471
sha256Hasher := hasher.NewSHA256Hasher(cfg.PasswordSalt)
6572
jwtTokenizer := jwtutil.NewJWTUtil(cfg.JwtSigningKey, cfg.JwtAccessTokenTTL)
6673
mailGunMailer := mailer.NewMailgun(cfg.MailgunFrom, cfg.MailgunDomain, cfg.MailgunAPIKey)
@@ -69,13 +76,15 @@ func run(ctx context.Context) error {
6976
vertokrepo := vertokrepo.New(psqlDB)
7077

7178
userepo := userepo.New(psqlDB)
79+
usercache := usercache.New(redisDB, cfg.CacheUsersTTL)
7280
usersrv := usersrv.New(
7381
userepo,
7482
sessionrepo,
7583
vertokrepo,
7684
sha256Hasher,
7785
jwtTokenizer,
7886
mailGunMailer,
87+
usercache,
7988
cfg.JwtRefreshTokenTTL,
8089
cfg.VerificationTokenTTL,
8190
cfg.AppURL,
@@ -129,5 +138,9 @@ func run(ctx context.Context) error {
129138
return errors.Join(errors.New("failed to close postgres connection"), err)
130139
}
131140

141+
if err := redisDB.Close(); err != nil {
142+
return errors.Join(errors.New("failed to close redis connection"), err)
143+
}
144+
132145
return nil
133146
}

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ services:
2222
ports:
2323
- 5432:5432
2424

25+
redis:
26+
image: redis:7.4-alpine
27+
container_name: onasty-redis
28+
ports:
29+
- 6379:6379
30+
2531
prometheus:
2632
image: prom/prometheus
2733
container_name: onasty-prometheus

e2e/e2e_test.go

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,34 @@ import (
2525
"github.com/olexsmir/onasty/internal/store/psql/userepo"
2626
"github.com/olexsmir/onasty/internal/store/psql/vertokrepo"
2727
"github.com/olexsmir/onasty/internal/store/psqlutil"
28+
"github.com/olexsmir/onasty/internal/store/rdb"
29+
"github.com/olexsmir/onasty/internal/store/rdb/usercache"
2830
httptransport "github.com/olexsmir/onasty/internal/transport/http"
2931
"github.com/olexsmir/onasty/internal/transport/http/ratelimit"
32+
"github.com/redis/go-redis/v9"
3033
"github.com/stretchr/testify/require"
3134
"github.com/stretchr/testify/suite"
3235
"github.com/testcontainers/testcontainers-go"
33-
"github.com/testcontainers/testcontainers-go/modules/postgres"
36+
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
37+
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
3438
"github.com/testcontainers/testcontainers-go/wait"
3539

3640
_ "github.com/golang-migrate/migrate/v4/source/file"
3741
)
3842

3943
type (
40-
stopDBFunc func()
44+
stopFunc func()
4145
AppTestSuite struct {
4246
suite.Suite
4347

4448
ctx context.Context
4549
require *require.Assertions
4650

4751
postgresDB *psqlutil.DB
48-
stopPostgres stopDBFunc
52+
stopPostgres stopFunc
53+
54+
redisDB *rdb.DB
55+
stopRedis stopFunc
4956

5057
router http.Handler
5158
hasher hasher.Hasher
@@ -72,17 +79,15 @@ func (e *AppTestSuite) SetupSuite() {
7279
e.ctx = context.Background()
7380
e.require = e.Require()
7481

75-
db, stop, err := e.prepPostgres()
76-
e.require.NoError(err)
77-
78-
e.postgresDB = db
79-
e.stopPostgres = stop
82+
e.postgresDB, e.stopPostgres = e.prepPostgres()
83+
e.redisDB, e.stopRedis = e.prepRedis()
8084

8185
e.initDeps()
8286
}
8387

8488
func (e *AppTestSuite) TearDownSuite() {
8589
e.stopPostgres()
90+
e.stopRedis()
8691
}
8792

8893
// initDeps initializes the dependencies for the app
@@ -103,13 +108,15 @@ func (e *AppTestSuite) initDeps() {
103108
vertokrepo := vertokrepo.New(e.postgresDB)
104109

105110
userepo := userepo.New(e.postgresDB)
111+
usercache := usercache.New(e.redisDB, cfg.CacheUsersTTL)
106112
usersrv := usersrv.New(
107113
userepo,
108114
sessionrepo,
109115
vertokrepo,
110116
e.hasher,
111117
e.jwtTokenizer,
112118
e.mailer,
119+
usercache,
113120
cfg.JwtRefreshTokenTTL,
114121
cfg.VerificationTokenTTL,
115122
cfg.AppURL,
@@ -129,20 +136,17 @@ func (e *AppTestSuite) initDeps() {
129136
e.router = handler.Handler()
130137
}
131138

132-
func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopDBFunc, error) {
139+
func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopFunc) {
133140
dbCredential := "testing"
134-
postgresContainer, err := postgres.Run(e.ctx,
141+
postgresContainer, err := tcpostgres.Run(e.ctx,
135142
"postgres:16-alpine",
136-
postgres.WithUsername(dbCredential),
137-
postgres.WithPassword(dbCredential),
138-
postgres.WithDatabase(dbCredential),
143+
tcpostgres.WithUsername(dbCredential),
144+
tcpostgres.WithPassword(dbCredential),
145+
tcpostgres.WithDatabase(dbCredential),
139146
testcontainers.WithWaitStrategy(wait.ForListeningPort("5432/tcp")))
140147
e.require.NoError(err)
141148

142-
stop := func() {
143-
err = postgresContainer.Terminate(e.ctx)
144-
e.require.NoError(err)
145-
}
149+
stop := func() { e.require.NoError(postgresContainer.Terminate(e.ctx)) }
146150

147151
// connect to the db
148152
host, err := postgresContainer.Host(e.ctx)
@@ -151,17 +155,14 @@ func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopDBFunc, error) {
151155
port, err := postgresContainer.MappedPort(e.ctx, "5432/tcp")
152156
e.require.NoError(err)
153157

154-
db, err := psqlutil.Connect(
155-
e.ctx,
156-
fmt.Sprintf( //nolint:nosprintfhostport
157-
"postgres://%s:%s@%s:%s/%s",
158-
dbCredential,
159-
dbCredential,
160-
host,
161-
port.Port(),
162-
dbCredential,
163-
),
164-
)
158+
db, err := psqlutil.Connect(e.ctx, fmt.Sprintf( //nolint:nosprintfhostport
159+
"postgres://%s:%s@%s:%s/%s",
160+
dbCredential,
161+
dbCredential,
162+
host,
163+
port.Port(),
164+
dbCredential,
165+
))
165166
e.require.NoError(err)
166167

167168
// run migrations
@@ -175,10 +176,28 @@ func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopDBFunc, error) {
175176
)
176177
e.require.NoError(err)
177178

178-
err = m.Up()
179+
e.require.NoError(m.Up())
180+
e.require.NoError(driver.Close())
181+
182+
return db, stop
183+
}
184+
185+
func (e *AppTestSuite) prepRedis() (*rdb.DB, stopFunc) {
186+
redisContainer, err := tcredis.Run(e.ctx, "redis:7.4-alpine")
187+
e.require.NoError(err)
188+
189+
stop := func() { e.require.NoError(redisContainer.Terminate(e.ctx)) }
190+
191+
uri, err := redisContainer.ConnectionString(e.ctx)
192+
e.require.NoError(err)
193+
194+
connOpts, err := redis.ParseURL(uri)
195+
e.require.NoError(err)
196+
197+
redis, err := rdb.Connect(e.ctx, connOpts.Addr, connOpts.Password, connOpts.DB)
179198
e.require.NoError(err)
180199

181-
return db, stop, driver.Close()
200+
return redis, stop
182201
}
183202

184203
func (e *AppTestSuite) getConfig() *config.Config {
@@ -194,5 +213,6 @@ func (e *AppTestSuite) getConfig() *config.Config {
194213
LogShowLine: os.Getenv("LOG_SHOW_LINE") == "true",
195214
LogFormat: "text",
196215
LogLevel: "debug",
216+
CacheUsersTTL: time.Second,
197217
}
198218
}

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ require (
1212
github.com/jackc/pgx/v5 v5.7.1
1313
github.com/mailgun/mailgun-go/v4 v4.16.0
1414
github.com/prometheus/client_golang v1.20.5
15+
github.com/redis/go-redis/v9 v9.7.0
1516
github.com/stretchr/testify v1.9.0
16-
github.com/testcontainers/testcontainers-go v0.33.0
17-
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0
17+
github.com/testcontainers/testcontainers-go v0.34.0
18+
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0
19+
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0
1820
golang.org/x/time v0.7.0
1921
)
2022

@@ -32,8 +34,9 @@ require (
3234
github.com/cloudwego/iasm v0.2.0 // indirect
3335
github.com/containerd/log v0.1.0 // indirect
3436
github.com/containerd/platforms v0.2.1 // indirect
35-
github.com/cpuguy83/dockercfg v0.3.1 // indirect
37+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
3638
github.com/davecgh/go-spew v1.1.1 // indirect
39+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
3740
github.com/distribution/reference v0.6.0 // indirect
3841
github.com/docker/docker v27.2.0+incompatible // indirect
3942
github.com/docker/go-connections v0.5.0 // indirect

go.sum

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ github.com/ahmetb/go-linq v3.0.0+incompatible h1:qQkjjOXKrKOTy83X8OpRmnKflXKQIL/
1313
github.com/ahmetb/go-linq v3.0.0+incompatible/go.mod h1:PFffvbdbtw+QTB0WKRP0cNht7vnCfnGlEpak/DVg5cY=
1414
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1515
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
16+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
17+
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
18+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
19+
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
1620
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
1721
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
1822
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -33,14 +37,16 @@ github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpS
3337
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
3438
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
3539
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
36-
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
37-
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
40+
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
41+
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
3842
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
3943
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
4044
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4145
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4246
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4347
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
48+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
49+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
4450
github.com/dhui/dktest v0.4.3 h1:wquqUxAFdcUgabAVLvSCOKOlag5cIZuaOjYIBOWdsR0=
4551
github.com/dhui/dktest v0.4.3/go.mod h1:zNK8IwktWzQRm6I/l2Wjp7MakiyaFWv4G1hjmodmMTs=
4652
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
@@ -84,6 +90,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
8490
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
8591
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
8692
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
93+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
94+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
8795
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
8896
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
8997
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
@@ -256,6 +264,8 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
256264
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
257265
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
258266
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
267+
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
268+
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
259269
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
260270
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
261271
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
@@ -281,6 +291,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
281291
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
282292
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
283293
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
294+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
284295
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
285296
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
286297
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -293,10 +304,12 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
293304
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
294305
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
295306
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
296-
github.com/testcontainers/testcontainers-go v0.33.0 h1:zJS9PfXYT5O0ZFXM2xxXfk4J5UMw/kRiISng037Gxdw=
297-
github.com/testcontainers/testcontainers-go v0.33.0/go.mod h1:W80YpTa8D5C3Yy16icheD01UTDu+LmXIA2Keo+jWtT8=
298-
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0 h1:c+Gt+XLJjqFAejgX4hSpnHIpC9eAhvgI/TFWL/PbrFI=
299-
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0/go.mod h1:I4DazHBoWDyf69ByOIyt3OdNjefiUx372459txOpQ3o=
307+
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
308+
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
309+
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
310+
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
311+
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 h1:HkkKZPi6W2I+ywqplvnKOYRBKXQgpdxErBbdgx8F8nw=
312+
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0/go.mod h1:iUkbN75F4E8WC5C1MfHbGOHOuKU7gOJfHjtwMT8G9QE=
300313
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
301314
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
302315
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=

internal/config/config.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ import (
88
)
99

1010
type Config struct {
11-
AppEnv string
12-
AppURL string
13-
ServerPort string
11+
AppEnv string
12+
AppURL string
13+
ServerPort string
14+
1415
PostgresDSN string
1516
PasswordSalt string
1617

18+
RedisAddr string
19+
RedisPassword string
20+
RedisDB int
21+
22+
CacheUsersTTL time.Duration
23+
1724
JwtSigningKey string
1825
JwtAccessTokenTTL time.Duration
1926
JwtRefreshTokenTTL time.Duration
@@ -37,12 +44,19 @@ type Config struct {
3744

3845
func NewConfig() *Config {
3946
return &Config{
40-
AppEnv: getenvOrDefault("APP_ENV", "debug"),
41-
AppURL: getenvOrDefault("APP_URL", ""),
42-
ServerPort: getenvOrDefault("SERVER_PORT", "3000"),
47+
AppEnv: getenvOrDefault("APP_ENV", "debug"),
48+
AppURL: getenvOrDefault("APP_URL", ""),
49+
ServerPort: getenvOrDefault("SERVER_PORT", "3000"),
50+
4351
PostgresDSN: getenvOrDefault("POSTGRESQL_DSN", ""),
4452
PasswordSalt: getenvOrDefault("PASSWORD_SALT", ""),
4553

54+
RedisAddr: getenvOrDefault("REDIS_ADDR", ""),
55+
RedisPassword: getenvOrDefault("REDIS_PASSWORD", ""),
56+
RedisDB: mustGetenvOrDefaultInt(getenvOrDefault("REDIS_DB", "0"), 0),
57+
58+
CacheUsersTTL: mustParseDuration(getenvOrDefault("CACHE_USERS_TTL", "1h")),
59+
4660
JwtSigningKey: getenvOrDefault("JWT_SIGNING_KEY", ""),
4761
JwtAccessTokenTTL: mustParseDuration(
4862
getenvOrDefault("JWT_ACCESS_TOKEN_TTL", "15m"),

0 commit comments

Comments
 (0)