Skip to content

Commit

Permalink
Migrate to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Roma7-7-7 committed Mar 10, 2024
1 parent f9d861d commit 37b404c
Show file tree
Hide file tree
Showing 27 changed files with 421 additions and 410 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ FROM alpine:3.19
EXPOSE 8080

VOLUME /app/config
VOLUME /app/db

COPY --from=build /app/bin/app /app

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

VERSION=0.1.0
VERSION=0.2.0

DB_URL ?= postgres://postgres:postgres@localhost:5432/clipboard-share?sslmode=disable

Expand Down Expand Up @@ -37,4 +37,4 @@ build-docker:
docker build -t clipboard-share-api:$(VERSION) .

run-docker:
docker run -p 8080:8080 -v $(shell pwd)/db:/app/db -v $(shell pwd)/configs:/app/config --name clipboard-share-api clipboard-share-api:$(VERSION)
docker run -p 8080:8080 -v $(shell pwd)/configs:/app/config --name clipboard-share-api clipboard-share-api:$(VERSION)
17 changes: 9 additions & 8 deletions configs/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
"jwt": {
"issuer": "clipboard-share",
"audience": ["http://localhost:8080", "https://localhost:8080"],
"expire_in_minutes": 44640,
"expire_in_minutes": 1440,
"secret": "secret"
},
"redis": {
"addr": "redis:6379",
"password": "",
"db": 0,
"timeout_millis": 100
},
"db": {
"bolt": {
"path": "./db/bolt.db"
},
"sql": {
"driver": "postgres",
"data_source": "postgres://postgres:postgres@postgres:5432/clipboard-share?sslmode=disable"
}
"driver": "postgres",
"data_source": "postgres://postgres:postgres@postgres:5432/clipboard-share?sslmode=disable"
}
}
10 changes: 8 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ services:
ports:
- "8080:8080"
volumes:
- ./db/:/app/db/
- ./configs/:/app/config/
web:
build: ./web
ports:
- "80:80"
postgres:
image: postgres:16.2
privileged: true
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -21,5 +19,13 @@ services:
- postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
redis:
image: redis:7.2-alpine
restart: always
ports:
- '6379:6379'
volumes:
- cache:/data
volumes:
postgres:
cache:
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ require (
github.com/golang-jwt/jwt/v5 v5.1.0
github.com/google/uuid v1.4.0
github.com/lib/pq v1.10.9
go.etcd.io/bbolt v1.3.8
github.com/redis/go-redis/v9 v9.5.1
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.19.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
Expand All @@ -16,10 +22,10 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand All @@ -28,7 +34,5 @@ go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
44 changes: 18 additions & 26 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"time"

"github.com/go-chi/chi/v5"
bolt "go.etcd.io/bbolt"
"github.com/redis/go-redis/v9"

"github.com/Roma7-7-7/shared-clipboard/internal/config"
"github.com/Roma7-7-7/shared-clipboard/internal/dal/local"
"github.com/Roma7-7-7/shared-clipboard/internal/dal/postgre"
"github.com/Roma7-7-7/shared-clipboard/internal/dal"
"github.com/Roma7-7-7/shared-clipboard/internal/domain"
"github.com/Roma7-7-7/shared-clipboard/internal/handle"
"github.com/Roma7-7-7/shared-clipboard/internal/handle/cookie"
Expand All @@ -31,35 +30,28 @@ type (

func NewApp(ctx context.Context, conf config.App, traced log.TracedLogger) (*App, error) {
traced.Infow(ctx, "Initializing SQL DB")
sqlDB, err := sql.Open(conf.DB.SQL.Driver, conf.DB.SQL.DataSource)
sqlDB, err := sql.Open(conf.DB.Driver, conf.DB.DataSource)
if err != nil {
return nil, fmt.Errorf("open sql db: %w", err)
}

traced.Infow(ctx, "Initializing Bolt DB")
boltDB, err := bolt.Open(conf.DB.Bolt.Path, 0600, nil)
if err != nil {
return nil, fmt.Errorf("open bolt db: %w", err)
}
redis := redis.NewClient(&redis.Options{
Addr: conf.Redis.Addr,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
ReadTimeout: time.Duration(conf.Redis.TimeoutMillis) * time.Millisecond,
WriteTimeout: time.Duration(conf.Redis.TimeoutMillis) * time.Millisecond,
})

traced.Infow(ctx, "Initializing repositories")
userRpo, err := postgre.NewUserRepository(sqlDB)
userRpo, err := dal.NewUserRepository(sqlDB)
if err != nil {
return nil, fmt.Errorf("create user repository: %w", err)
}
sessionRepo, err := postgre.NewSessionRepository(sqlDB)
sessionRepo, err := dal.NewSessionRepository(sqlDB)
if err != nil {
return nil, fmt.Errorf("create session repository: %w", err)
}
clipboardRepo, err := local.NewClipboardRepository(boltDB)
if err != nil {
return nil, fmt.Errorf("create clipboard repository: %w", err)
}
jwtRepo, err := local.NewJWTRepository(boltDB)
if err != nil {
return nil, fmt.Errorf("create jwt repository: %w", err)
}

traced.Infow(ctx, "Initializing services")
userService := domain.NewUserService(userRpo, traced)

Expand All @@ -71,12 +63,12 @@ func NewApp(ctx context.Context, conf config.App, traced log.TracedLogger) (*App

traced.Infow(ctx, "Creating router")
h, err := handle.NewRouter(ctx, handle.Dependencies{
Config: conf,
CookieProcessor: cookieProcessor,
UserService: userService,
JWTRepository: jwtRepo,
SessionService: sessionService,
ClipboardRepository: clipboardRepo,
Config: conf,
CookieProcessor: cookieProcessor,
UserService: userService,
JTIService: domain.NewJTIService(redis, traced),
SessionService: sessionService,
ClipboardService: domain.NewClipboardService(redis, traced),
}, traced)
if err != nil {
return nil, fmt.Errorf("create router: %w", err)
Expand Down
23 changes: 13 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
Cookie Cookie `json:"cookie"`
JWT JWT `json:"jwt"`
DB DB `json:"db"`
Redis Redis `json:"redis"`
}

Cookie struct {
Expand Down Expand Up @@ -43,14 +44,16 @@ type (
Path string `json:"path"`
}

SQL struct {
DB struct {
Driver string `json:"driver"`
DataSource string `json:"data_source"`
}

DB struct {
Bolt Bolt `json:"bolt"`
SQL SQL `json:"sql"`
Redis struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int `json:"db"`
TimeoutMillis int `json:"timeout_millis"`
}
)

Expand Down Expand Up @@ -88,14 +91,14 @@ func validateApp(app App) error {
if app.Port <= 0 || app.Port > 65535 {
return fmt.Errorf("invalid port: %d", app.Port)
}
if app.DB.Bolt.Path == "" {
res = append(res, "empty data path")
if app.Redis.Addr == "" {
res = append(res, "empty redis addr")
}
if app.DB.SQL.Driver == "" {
res = append(res, "empty postgre driver")
if app.DB.Driver == "" {
res = append(res, "empty DB driver")
}
if app.DB.SQL.DataSource == "" {
res = append(res, "empty postgre data source")
if app.DB.DataSource == "" {
res = append(res, "empty DB data source")
}

if len(res) != 0 {
Expand Down
4 changes: 4 additions & 0 deletions internal/dal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package dal

import "errors"

const (
pgConflictErrorCode = "23505"
)

var (
ErrNotFound = errors.New("not found")
ErrConflictUnique = errors.New("conflict unique")
Expand Down
76 changes: 0 additions & 76 deletions internal/dal/local/clipboard.go

This file was deleted.

Loading

0 comments on commit 37b404c

Please sign in to comment.