Skip to content

Commit

Permalink
feat: support specify pg sslmode
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Jul 1, 2022
1 parent 3ba8ac1 commit 4d62eab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions api-server/config/yatai_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type YataiPostgresqlConfigYaml struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
SSLMode string `yaml:"sslmode"`
}

type YataiS3ConfigYaml struct {
Expand Down Expand Up @@ -85,6 +86,10 @@ func PopulateYataiConfig() error {
if ok {
YataiConfig.Postgresql.Database = pgDatabase
}
pgSSLMode, ok := os.LookupEnv(consts.EnvPgSSLMode)
if ok {
YataiConfig.Postgresql.SSLMode = pgSSLMode
}
migrationDir, ok := os.LookupEnv(consts.EnvMigrationDir)
if ok {
YataiConfig.Server.MigrationDir = migrationDir
Expand Down
9 changes: 7 additions & 2 deletions api-server/services/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ func getPgHost() string {
// nolint: unparam
func getDBURI() (string, error) {
password := url.QueryEscape(config.YataiConfig.Postgresql.Password)
uri := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable",
sslMode := "disable"
if config.YataiConfig.Postgresql.SSLMode != "" {
sslMode = config.YataiConfig.Postgresql.SSLMode
}
uri := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
config.YataiConfig.Postgresql.User,
password,
getPgHost(),
config.YataiConfig.Postgresql.Port,
config.YataiConfig.Postgresql.Database)
config.YataiConfig.Postgresql.Database,
sslMode)
return uri, nil
}

Expand Down
1 change: 1 addition & 0 deletions common/consts/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
EnvPgUser = "PG_USER"
EnvPgPassword = "PG_PASSWORD"
EnvPgDatabase = "PG_DATABASE"
EnvPgSSLMode = "PG_SSLMODE"

EnvMigrationDir = "MIGRATION_DIR"
EnvSessionSecretKey = "SESSION_SECRET_KEY"
Expand Down

0 comments on commit 4d62eab

Please sign in to comment.