Skip to content

Commit

Permalink
feat: added roles for microservices in cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashLight committed Nov 29, 2024
1 parent b240895 commit 833390c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 47 deletions.
11 changes: 4 additions & 7 deletions DB/migrations/000002_init_roles.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ 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 ON profile, friend 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 TO community_service;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO community_service;
GRANT ALL PRIVILEGES ON community, community_profile, admin TO community_service;
GRANT ALL PRIVILEGES ON ALL TABLES IN 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 ON post, comment, reaction 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 ON message TO message_service;
GRANT SELECT, INSERT, DELETE, UPDATE, REFERENCES, TRUNCATE, TRIGGER, MAINTAIN ON message TO message_service;
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
24 changes: 12 additions & 12 deletions internal/app/post/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func GetHTTPServer(cfg *config.Config, postMetric *metrics.HttpMetrics) (*http.S
}

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.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 @@ -102,12 +102,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.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 @@ -58,11 +61,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

0 comments on commit 833390c

Please sign in to comment.