-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsql.py
More file actions
25 lines (19 loc) · 783 Bytes
/
Copy pathpsql.py
File metadata and controls
25 lines (19 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import asyncio
import os
from sanic import Blueprint
from windyquery import DB
db = DB()
psql = Blueprint(name='psql')
@psql.listener('before_server_start')
def start_db(app, loop):
service_name = os.getenv('DATABASE_SERVICE_NAME', '').upper().replace('-', '_')
asyncio.get_event_loop().run_until_complete(db.connect(os.getenv('DATABASE_NAME'), {
'host': os.getenv('{}_SERVICE_HOST'.format(service_name)),
'port': os.getenv('{}_SERVICE_PORT'.format(service_name)),
'database': os.getenv('DATABASE_NAME'),
'username': os.getenv('DATABASE_USER'),
'password': os.getenv('DATABASE_PASSWORD'),
}, default=True))
@psql.listener('after_server_stop')
def stop_db(app, loop):
asyncio.get_event_loop().run_until_complete(db.stop())