-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Rpsl/sentinel
Sentinel
- Loading branch information
Showing
11 changed files
with
150 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters