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
5 changes: 4 additions & 1 deletion controller/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SERVER_HOST=localhost
SERVER_PORT=50051
MAX_MESSAGE_SIZE=4194304
MAX_MESSAGE_SIZE=4194304
REDIS_PASSWORD=mysecretpassword

@KonstantinKondratenko KonstantinKondratenko Jun 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему контроллер при обращаениях к БД не использует креды?
давайте их или уберем или будет нормальный запросы делать с кредами смысл делать креды а потом default on nopass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я подумал, что убрать креды -- плохая мысль -- работаем по паролю но честно по паролю

REDIS_USER=myuser
REDIS_USER_PASSWORD=myuserpassword
7 changes: 6 additions & 1 deletion controller/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_joho_godotenv",
"com_github_subosito_gotenv",
"com_github_pelletier_go_toml",
"com_github_stretchr_testify",
"org_golang_google_grpc",
"org_golang_google_protobuf",
)
# Добавляем Redis в use_repo
"com_github_redis_go_redis_v9",
"com_github_cespare_xxhash_v2",
"com_github_dgryski_go_rendezvous",
)
17 changes: 8 additions & 9 deletions controller/cmd/grpc_server/BUILD
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
load("@rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "grpc_server",
embed = [":grpc_server_lib"],
visibility = ["//visibility:public"],
data = [
"//internal/service/config",
],
Comment on lines -7 to -9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему убрали?

)

go_library(
name = "grpc_server_lib",
srcs = ["main.go"],
Expand All @@ -19,9 +10,17 @@ go_library(
"//internal/grpcserver",
"//internal/manager",
"//internal/service/service",
"//internal/service/storage",
"//pkg/proto/admin_service:admin_service_go_proto",
"//pkg/proto/communication:communication_go_proto",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//reflection",
"@com_github_subosito_gotenv//:go_default_library",
],
)

go_binary(
name = "grpc_server",
embed = [":grpc_server_lib"],
visibility = ["//visibility:public"],
)
105 changes: 104 additions & 1 deletion controller/cmd/grpc_server/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,115 @@
package main

import (
"fmt"
"log"
"net"
"strconv"
"os"
"time"

"github.com/moevm/grpc_server/internal/config"
"github.com/moevm/grpc_server/internal/grpcserver"
"github.com/moevm/grpc_server/internal/manager"
"github.com/moevm/grpc_server/internal/service/storage"
adminPb "github.com/moevm/grpc_server/pkg/proto/admin_service"
commPb "github.com/moevm/grpc_server/pkg/proto/communication"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func LoadConfigRedis() (storage.Config, error) {
cfg := storage.Config{}

addr, exist := os.LookupEnv("REDIS_ADDR")
if !exist {
return storage.Config{}, fmt.Errorf("REDIS_ADDR does not exist")
}
cfg.Addr = addr

password, exist := os.LookupEnv("REDIS_PASSWORD")
if !exist {
return storage.Config{}, fmt.Errorf("REDIS_PASSWORD does not exist")
}
cfg.Password = password

user, exist := os.LookupEnv("REDIS_USER")
if !exist {
return storage.Config{}, fmt.Errorf("REDIS_USER does not exist")
}
cfg.User = user


dbStr, exist := os.LookupEnv("REDIS_DB")
if exist {
db, err := strconv.Atoi(dbStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_DB must be integer: %w", err)
}
cfg.DB = db
} else {
cfg.DB = 0
}

retriesStr, exist := os.LookupEnv("REDIS_MAX_RETRIES")
if exist {
retries, err := strconv.Atoi(retriesStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_MAX_RETRIES must be integer: %w", err)
}
cfg.MaxRetries = retries
} else {
cfg.MaxRetries = 3
}

dialTimeoutStr, exist := os.LookupEnv("REDIS_DIAL_TIMEOUT")
if exist {
d, err := time.ParseDuration(dialTimeoutStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_DIAL_TIMEOUT invalid duration: %w", err)
}
cfg.DialTimeout = d
} else {
cfg.DialTimeout = 5 * time.Second
}

timeoutStr, exist := os.LookupEnv("REDIS_TIMEOUT")
if exist {
d, err := time.ParseDuration(timeoutStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_TIMEOUT invalid duration: %w", err)
}
cfg.Timeout = d
} else {
cfg.Timeout = 3 * time.Second
}


ttlSuccessStr, exist := os.LookupEnv("REDIS_TTL_SUCCESS")
if exist {
d, err := time.ParseDuration(ttlSuccessStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_TTL_SUCCESS invalid duration: %w", err)
}
cfg.TTLSuccess = d
} else {
cfg.TTLSuccess = 24 * time.Hour
}

ttlUnknownStr, exist := os.LookupEnv("REDIS_TTL_UNKNOWN")
if exist {
d, err := time.ParseDuration(ttlUnknownStr)
if err != nil {
return storage.Config{}, fmt.Errorf("REDIS_TTL_UNKNOWN invalid duration: %w", err)
}
cfg.TTLUnknown = d
} else {
cfg.TTLUnknown = 1 * time.Hour
}

return cfg, nil
}

func main() {
cfg := config.Load()
adminServer := grpcserver.NewAdminServer()
Expand All @@ -23,7 +120,13 @@ func main() {

adminServer.SetManager(mgr)

dataServer, err := grpcserver.NewDataServer(mgr, "internal/service/config/categories.json", "internal/service/config/providers.json")
configRedis, err := LoadConfigRedis()

if err != nil {
log.Fatalf("Error to load redis config: %v", err)
}

dataServer, err := grpcserver.NewDataServer(mgr, "internal/service/config/categories.json", "internal/service/config/providers.json", configRedis)

if err != nil {
log.Fatalf("Failed to create data server: %v", err)
Expand Down
50 changes: 50 additions & 0 deletions controller/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '3.9'

services:
redis:
image: redis:latest
container_name: redis_container
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
- REDIS_USER=${REDIS_USER}
- REDIS_USER_PASSWORD=${REDIS_USER_PASSWORD}
ports:
- "6379:6379"
volumes:
- ./redisdata:/data
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
command: >
sh -c '
mkdir -p /usr/local/etc/redis &&
echo "bind 0.0.0.0" > /usr/local/etc/redis/redis.conf &&
echo "requirepass $REDIS_PASSWORD" >> /usr/local/etc/redis/redis.conf &&
echo "appendonly yes" >> /usr/local/etc/redis/redis.conf &&
echo "appendfsync everysec" >> /usr/local/etc/redis/redis.conf &&
echo "user default on nopass ~* +@all" > /usr/local/etc/redis/users.acl &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему?
Это опасно, если мы хотим, чтобы разработкой пользовались, нужно ее аккуратно писать, а не оставлять такие дыры.

и сразу нормальный контракт взаимодействия описать

echo "user $REDIS_USER on >$REDIS_USER_PASSWORD ~* +@all" >> /usr/local/etc/redis/users.acl &&
redis-server /usr/local/etc/redis/redis.conf --aclfile /usr/local/etc/redis/users.acl
'
healthcheck:
test: ["CMD", "redis-cli", "-a", "$REDIS_PASSWORD", "ping"]
interval: 30s
timeout: 10s
retries: 5
restart: unless-stopped
tty: true
stdin_open: true

controller:
build:
dockerfile: Dockerfile.controller
environment:
- REDIS_ADDR=redis:6379

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему оно не в .env???
вынесите в .env файл

depends_on:
- redis

5 changes: 5 additions & 0 deletions controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ require (
)

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/redis/go-redis/v9 v9.18.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions controller/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.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-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand All @@ -12,12 +16,17 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/nacknime-official/fsm-telebot-redis-storage/v3 v3.0.0-20250404091544-e45a7caa1738/go.mod h1:VmEWTtxh9kff1N+ZcaoappPqjitkzRAGXWLogBwsdHM=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
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.18.0 h1:pMkxYPkEbMPwRdenAzUNyFNrDgHx9U+DrBabWNfSRQs=
github.com/redis/go-redis/v9 v9.18.0/go.mod h1:k3ufPphLU5YXwNTUcCRXGxUoF1fqxnhFQmscfkCoDA0=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
Expand All @@ -30,6 +39,8 @@ go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
Expand Down
9 changes: 5 additions & 4 deletions controller/internal/grpcserver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ go_library(
deps = [
"//internal/service/service",
"//internal/manager",
"//internal/service/storage",
"//pkg/proto/admin_service:admin_service_go_proto",
"//pkg/proto/communication:communication_go_proto",
"@com_github_redis_go_redis_v9//:go-redis",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_protobuf//types/known/emptypb",
"@org_golang_google_protobuf//types/known/emptypb",
"@org_golang_google_protobuf//proto:go_default_library",
]
)

],
)
Loading
Loading