Skip to content

Commit e8ad1fd

Browse files
authored
Merge pull request #22 from pamelafox/conn-str
Port Flask sample to use keyword/value style connection string
2 parents d4d87ba + 3b59f2d commit e8ad1fd

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

azureproject/production.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
77
CSRF_TRUSTED_ORIGINS = ['https://' + os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
88

9-
# Configure Postgres database; the full username for PostgreSQL flexible server is
10-
# username (not @sever-name).
9+
# Configure Postgres database based on connection string of the libpq Keyword/Value form
10+
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
11+
conn_str = os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']
12+
conn_str_params = {pair.split('=')[0]: pair.split('=')[1] for pair in conn_str.split(' ')}
13+
1114
DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
12-
dbuser=os.environ['DBUSER'],
13-
dbpass=os.environ['DBPASS'],
14-
dbhost=os.environ['DBHOST'] + ".postgres.database.azure.com",
15-
dbname=os.environ['DBNAME']
15+
dbuser=conn_str_params['user'],
16+
dbpass=conn_str_params['password'],
17+
dbhost=conn_str_params['host'],
18+
dbname=conn_str_params['dbname']
1619
)

infra/resources.bicep

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
106106
name: 'appsettings'
107107
properties: {
108108
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
109-
DBHOST: postgresServer.name
110-
DBNAME: flaskDatabase.name
111-
DBUSER: postgresServer.properties.administratorLogin
112-
DBPASS: databasePassword
109+
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${flaskDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
113110
SECRET_KEY: secretKey
114111
FLASK_DEBUG: 'False'
115112
}

0 commit comments

Comments
 (0)