Skip to content

Commit

Permalink
Merge pull request #114 from CSR-LC/feat/#8/separate_secrets
Browse files Browse the repository at this point in the history
Separate secrets from main config #8
  • Loading branch information
DaniilGo authored Sep 30, 2024
2 parents 59c5aff + 16d4989 commit d4a3642
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ jobs:
script: |
mkdir -p /var/www/csr/stage/
echo '${{ secrets.DEPLOY_CONFIG }}' > /var/www/csr/stage/config.json
sudo rm /etc/systemd/system/stage.csr.env
echo -e "JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}\n\
EMAIL_PASSWORD=${{ secrets.EMAIL_PASSWORD }}\n\
DB_USER=${{ secrets.DB_USER }}" > stage.csr.env
sudo mv stage.csr.env /etc/systemd/system/stage.csr.env
sudo systemctl daemon-reload && sudo service stage.csr stop
cp ~/csr /var/www/csr/stage/server
sudo service stage.csr start
3 changes: 0 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
"db": {
"host": "postgres",
"port": "5432",
"user": "csr",
"database": "csr",
"showSql": false
},
"JWTSecretKey": 123,
"email": {
"serverHost": "any",
"serverPort": 1,
"password": "any",
"senderFromAddress": "any",
"senderFromName": "any",
"confirmLinkExpiration": "15m",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Description=stage.csr
After=network.target

[Service]
EnvironmentFile=/etc/systemd/system/stage.csr.env
Type=simple
WorkingDirectory=/var/www/csr/stage
User=csr
Expand Down
13 changes: 12 additions & 1 deletion internal/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func GetAppConfig(additionalDirectories ...string) (*AppConfig, error) {
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read in config: %w", err)
}
bindEnvVars()

conf := getDefaultConfig()
if err := viper.Unmarshal(&conf); err != nil {
Expand All @@ -100,16 +101,18 @@ func GetAppConfig(additionalDirectories ...string) (*AppConfig, error) {

func getDefaultConfig() *AppConfig {
return &AppConfig{
JWTSecretKey: "default_value",
DB: DB{
Host: "localhost",
User: "csr",
Password: "password",
Database: "stage_csr",
},
Password: Password{
Length: 8,
ResetLinkExpiration: 15 * time.Minute,
},
Email: Email{
Password: "default_value",
SenderWebsiteUrl: "https://csr.golangforall.com/",
ConfirmLinkExpiration: 15 * time.Minute,
},
Expand All @@ -119,3 +122,11 @@ func getDefaultConfig() *AppConfig {
},
}
}

func bindEnvVars() {
viper.BindEnv("jwtsecretkey", "JWT_SECRET_KEY")
viper.BindEnv("email.password", "EMAIL_PASSWORD")
viper.BindEnv("db.user", "DB_USER")

viper.AutomaticEnv()
}

0 comments on commit d4a3642

Please sign in to comment.