Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bcfw 26 bd2 #51

Merged
merged 7 commits into from
Dec 22, 2024
Merged
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: 5 additions & 0 deletions DB/migrations/000002_init_roles.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP ROLE admin;
DROP ROLE profile_service;
DROP ROLE community_service;
DROP ROLE post_service;
DROP ROLE message_service;
19 changes: 19 additions & 0 deletions DB/migrations/000002_init_roles.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE ROLE admin WITH LOGIN PASSWORD 'admin_password';
ALTER ROLE admin SUPERUSER;

CREATE ROLE profile_service WITH LOGIN PASSWORD 'profile_password';
GRANT CONNECT ON DATABASE mydbvk TO profile_service;
GRANT SELECT, INSERT, DELETE, UPDATE, REFERENCES, TRUNCATE, TRIGGER, MAINTAIN ON profile, friend TO profile_service;

CREATE ROLE community_service WITH LOGIN PASSWORD 'community_password';
GRANT CONNECT ON DATABASE mydbvk TO community_service;
GRANT USAGE ON SCHEMA public TO community_service;
GRANT SELECT, INSERT, DELETE, UPDATE, REFERENCES, TRUNCATE, TRIGGER, MAINTAIN ON community, community_profile, admin TO community_service;

CREATE ROLE post_service WITH LOGIN PASSWORD 'post_password';
GRANT CONNECT ON DATABASE mydbvk TO post_service;
GRANT SELECT, INSERT, DELETE, UPDATE, REFERENCES, TRUNCATE, TRIGGER, MAINTAIN ON post, comment, reaction TO post_service;

CREATE ROLE message_service WITH LOGIN PASSWORD 'message_password';
GRANT CONNECT ON DATABASE mydbvk TO message_service;
GRANT SELECT, INSERT, DELETE, UPDATE, REFERENCES, TRUNCATE, TRIGGER, MAINTAIN ON message TO message_service;
4 changes: 4 additions & 0 deletions conf.d/00memory.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
max_connections = 100
shared_buffers = 1GB
work_mem = 64MB
maintenance_work_mem = 512MB
3 changes: 3 additions & 0 deletions conf.d/01timeout.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
statement_timeout = '1min'
idle_in_transaction_session_timeout = '5min'
lock_timeout = '30s'
1 change: 1 addition & 0 deletions conf.d/02libs.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shared_preload_libraries = 'pg_stat_statements, auto_explain'
2 changes: 2 additions & 0 deletions conf.d/03auto_explain.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
auto_explain.log_min_duration = '100ms'
auto_explain.log_analyze = on
6 changes: 6 additions & 0 deletions conf.d/04pgbadger.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
log_min_duration_statement = '100ms'
log_line_prefix = '%t [%p]: [%l-1] db=%d, user=%u, app=%a '
log_destination = 'csvlog'
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
logging_collector = on
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ services:

db:
image: postgres:latest
command: -c config_file=/etc/postgresql/postgresql.conf
ports:
- "5432:5432"
environment:
Expand All @@ -110,6 +111,8 @@ services:
- DATABASE_HOST=${DB_HOST}
volumes:
- postgres_data:/var/lib/postgresql/vk_app_data
- ./postgresql.conf:/etc/postgresql/postgresql.conf
- ./conf.d:/etc/postgresql/conf.d
expose:
- 5432
healthcheck:
Expand Down
12 changes: 6 additions & 6 deletions internal/app/chat/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func GetServer(cfg *config.Config, chatMetrics *metrics.HttpMetrics) (*http.Serv
}

connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
cfg.MESSAGEDB.Host,
cfg.MESSAGEDB.Port,
cfg.MESSAGEDB.User,
cfg.MESSAGEDB.Pass,
cfg.MESSAGEDB.DBName,
cfg.MESSAGEDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down
12 changes: 6 additions & 6 deletions internal/app/community/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func GetServers(cfg *config.Config, grpcMetrics *metrics.GrpcMetrics, communityM
}

connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
cfg.COMMUNITYDB.Host,
cfg.COMMUNITYDB.Port,
cfg.COMMUNITYDB.User,
cfg.COMMUNITYDB.Pass,
cfg.COMMUNITYDB.DBName,
cfg.COMMUNITYDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down
30 changes: 14 additions & 16 deletions internal/app/post/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ func GetHTTPServer(cfg *config.Config, postMetric *metrics.HttpMetrics) (*http.S
ForceColors: true,
}

connStr := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.POSTDB.Host,
cfg.POSTDB.Port,
cfg.POSTDB.User,
cfg.POSTDB.Pass,
cfg.POSTDB.DBName,
cfg.POSTDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down Expand Up @@ -103,14 +102,13 @@ func GetGRPCServer(cfg *config.Config, grpcMetrics *metrics.GrpcMetrics) (*grpc.
ForceColors: true,
}

connStr := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.POSTDB.Host,
cfg.POSTDB.Port,
cfg.POSTDB.User,
cfg.POSTDB.Pass,
cfg.POSTDB.DBName,
cfg.POSTDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down
24 changes: 12 additions & 12 deletions internal/app/profile/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func GetHTTPServer(cfg *config.Config, metric *metrics.HttpMetrics) (*http.Serve
}

connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
cfg.PROFILEDB.Host,
cfg.PROFILEDB.Port,
cfg.PROFILEDB.User,
cfg.PROFILEDB.Pass,
cfg.PROFILEDB.DBName,
cfg.PROFILEDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down Expand Up @@ -98,12 +98,12 @@ func GetGRPCServer(cfg *config.Config, grpcMetrics *metrics.GrpcMetrics) (*grpc.
}

connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.User,
cfg.DB.Pass,
cfg.DB.DBName,
cfg.DB.SSLMode,
cfg.PROFILEDB.Host,
cfg.PROFILEDB.Port,
cfg.PROFILEDB.User,
cfg.PROFILEDB.Pass,
cfg.PROFILEDB.DBName,
cfg.PROFILEDB.SSLMode,
)

postgresDB, err := start_postgres.StartPostgres(connStr, logger)
Expand Down
35 changes: 31 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ type GRPCServer struct {
}

type Config struct {
DB DBConnect
PROFILEDB DBConnect
COMMUNITYDB DBConnect
POSTDB DBConnect
MESSAGEDB DBConnect
REDIS Redis
AUTH Server
FILE Server
Expand All @@ -59,11 +62,35 @@ func GetConfig(configFilePath string) (*Config, error) {
}

return &Config{
DB: DBConnect{
PROFILEDB: DBConnect{
Port: os.Getenv("DB_PORT"),
Host: os.Getenv("DB_HOST"),
User: os.Getenv("DB_USER"),
Pass: os.Getenv("DB_PASSWORD"),
User: os.Getenv("DB_PROFILE_USER"),
Pass: os.Getenv("DB_PROFILE_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
SSLMode: os.Getenv("DB_SSLMODE"),
},
COMMUNITYDB: DBConnect{
Port: os.Getenv("DB_PORT"),
Host: os.Getenv("DB_HOST"),
User: os.Getenv("DB_COMMUNITY_USER"),
Pass: os.Getenv("DB_COMMUNITY_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
SSLMode: os.Getenv("DB_SSLMODE"),
},
POSTDB: DBConnect{
Port: os.Getenv("DB_PORT"),
Host: os.Getenv("DB_HOST"),
User: os.Getenv("DB_POST_USER"),
Pass: os.Getenv("DB_POST_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
SSLMode: os.Getenv("DB_SSLMODE"),
},
MESSAGEDB: DBConnect{
Port: os.Getenv("DB_PORT"),
Host: os.Getenv("DB_HOST"),
User: os.Getenv("DB_MESSAGE_USER"),
Pass: os.Getenv("DB_MESSAGE_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
SSLMode: os.Getenv("DB_SSLMODE"),
},
Expand Down
Loading
Loading