Skip to content

Commit

Permalink
Merge pull request #4 from Rpsl/sentinel
Browse files Browse the repository at this point in the history
Sentinel
  • Loading branch information
Rpsl authored Oct 7, 2023
2 parents ad6a337 + c423d53 commit 136707c
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ YANDEX_ENABLED=true
YANDEX_APIKEY=""

REDIS_SENTINEL=true
REDIS_SENTINEL_NAME="mymaster"
REDIS_SENTINEL_HOST="redis-sentinel"
REDIS_SENTINEL_PORT="26379"
REDIS_HOST="127.0.0.1"
REDIS_PORT="6379"
REDIS_AUTH="password"
REDIS_DB=0

APP_PORT="8080"
APP_MODE="dev" # "dev" or "prod"
14 changes: 14 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
YANDEX_ENABLED=true
YANDEX_APIKEY=""

REDIS_SENTINEL=true
REDIS_SENTINEL_NAME="mymaster"
REDIS_SENTINEL_HOST="redis-sentinel"
REDIS_SENTINEL_PORT="26379"
REDIS_HOST="127.0.0.1"
REDIS_PORT="6379"
REDIS_AUTH="password"
REDIS_DB=0

APP_PORT="8080"
APP_MODE="dev" # "dev" or "prod"
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21
- uses: actions/checkout@v3
- name: Run tests
run: make test
Expand All @@ -29,7 +29,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##
## Build
##
FROM golang:1.20-buster AS build
FROM golang:1.21-bullseye AS build

WORKDIR /app

Expand All @@ -15,7 +15,7 @@ RUN export CGO_ENABLED=0 && go build -ldflags "-s -w" -o /address-suggestion-pro
##
## Production
##
FROM golang:1.20-alpine
FROM golang:1.21-alpine

WORKDIR /app/

Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ build-docker:
build-clean:
rm -rf ./build

attach:
run-docker-dev:
docker-compose -f docker-compose.dev.yml up

run-docker-prod:
docker-compose -f docker-compose.dev.yml up -d

run:
docker run --rm -it -v $(shell pwd):/app/ --name address-suggestion-proxy --entrypoint 'sh' address-suggestion-proxy:local

test:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If you want to contribute but don't know where to start, here are some implement
Create `.env` file and edit settings

```shell
cp env.example .env
cp .env.example .env
```

You can start cache in docker and run application on your host:
Expand Down
21 changes: 13 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package config

type Config struct {
YandexAPIKey string `env:"YANDEX_APIKEY,default=''"`
YandexEnabled bool `env:"YANDEX_ENABLED,default=false"`
DadataEnabled bool `env:"DADATA_ENABLED,default=false"`
RedisSentinel bool `env:"REDIS_SENTINEL,default=false"`
RedisHost string `env:"REDIS_HOST,default=127.0.0.1"`
RedisPort string `env:"REDIS_PORT,default=6379"`
AppPort string `env:"APP_PORT,default=8080"`
AppMode string `env:"APP_MODE,default=prod"`
YandexAPIKey string `env:"YANDEX_APIKEY,default=''"`
YandexEnabled bool `env:"YANDEX_ENABLED,default=false"`
DadataEnabled bool `env:"DADATA_ENABLED,default=false"`
RedisSentinel bool `env:"REDIS_SENTINEL,default=false"`
RedisSentinelName string `env:"REDIS_SENTINEL_NAME,default='mymaster'"`
RedisSentinelHost string `env:"REDIS_SENTINEL_HOST,default='127.0.0.1'"`
RedisSentinelPort string `env:"REDIS_SENTINEL_PORT,default=26379"`
RedisHost string `env:"REDIS_HOST,default=127.0.0.1"`
RedisPort string `env:"REDIS_PORT,default=6379"`
RedisAuth string `env:"REDIS_AUTH,default=''"`
RedisDB int `env:"REDIS_DB,default=0"`
AppPort string `env:"APP_PORT,default=8080"`
AppMode string `env:"APP_MODE,default=prod"`
}
44 changes: 44 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3.8'
services:
app:
image: address-suggestion-proxy:local
restart: unless-stopped
ports:
- '8080:8080'
working_dir: /app
entrypoint: ["go", "run", "."]
volumes:
- .:/app
depends_on:
- redis
networks:
- backend
redis:
image: 'bitnami/redis:latest'
restart: always
environment:
- ALLOW_EMPTY_PASSWORD=no
- REDIS_PASSWORD=password
ports:
- '6379:6379'
volumes:
- suggestion-redis:/bitnami/redis/data
networks:
- backend
redis-sentinel:
image: 'bitnami/redis-sentinel:latest'
environment:
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=password
ports:
- '26379:26379'
depends_on:
- redis
networks:
- backend
volumes:
suggestion-redis:
driver: local
networks:
backend:
driver: bridge
32 changes: 24 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,36 @@ services:
- /app/address-suggestion-proxy
volumes:
- ./.env:/app/.env:ro
depends_on:
- redis-sentinel
networks:
backend:
cache:
image: redis:6.2-alpine
- backend
redis:
image: 'bitnami/redis:latest'
restart: always
environment:
- ALLOW_EMPTY_PASSWORD=no
- REDIS_PASSWORD=password
ports:
- '6379:6379'
command: redis-server --save 20 1 --loglevel warning
volumes:
- cache:/data
- suggestion-redis:/bitnami/redis/data
networks:
backend:
- backend
redis-sentinel:
image: 'bitnami/redis-sentinel:latest'
environment:
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=password
ports:
- '26379:26379'
depends_on:
- redis
networks:
- backend
volumes:
cache:
redis:
driver: local
networks:
backend:
backend:
driver: bridge
37 changes: 37 additions & 0 deletions internal/services/redis_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package services

import (
"address-suggesstion-proxy/config"
"context"
"fmt"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
)

func NewRedisClient(cfg *config.Config) *redis.Client {
addr := fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort)

if cfg.RedisSentinel {
log.Debugln("using redis-sentinel")

sentinel := redis.NewSentinelClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisSentinelHost, cfg.RedisSentinelPort),
})

masters, err := sentinel.GetMasterAddrByName(context.Background(), cfg.RedisSentinelName).Result()

if err != nil {
log.WithError(err).Fatal("failed to get redis sentinel master host")
}

addr = fmt.Sprintf("%s:%s", masters[0], masters[1])

log.Debugf(fmt.Sprintf("redis master from sentinel is: %s", addr))
}

return redis.NewClient(&redis.Options{
Addr: addr,
Password: cfg.RedisAuth,
DB: cfg.RedisDB,
})
}
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/joho/godotenv"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/router"
"github.com/redis/go-redis/v9"
"github.com/sethvargo/go-envconfig"
log "github.com/sirupsen/logrus"
"time"
Expand All @@ -34,12 +33,7 @@ func main() {
log.WithError(err).Fatal("failed to load configuration")
}

db := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort),
Password: "", // no password set
DB: 0, // use default DB
})

db := services.NewRedisClient(&cfg)
repo, _ := reposirories.NewRedisRepository(db)
cache := services.NewCacheService(repo)
prov := providers.NewProvidersContainer(&cfg)
Expand Down

0 comments on commit 136707c

Please sign in to comment.