Read database connection parameters from environment

This commit is contained in:
Ske 2018-08-22 19:50:32 +02:00
parent 3d2d6b0ea6
commit 780bc8a512
4 changed files with 17 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.env .env
.vscode/ .vscode/
.idea/ .idea/
venv/

View File

@ -11,6 +11,11 @@ services:
environment: environment:
- CLIENT_ID - CLIENT_ID
- TOKEN - TOKEN
- "DATABASE_USER=postgres"
- "DATABASE_PASS=postgres"
- "DATABASE_NAME=postgres"
- "DATABASE_HOST=db"
- "DATABASE_PORT=5432"
restart: always restart: always
api: api:
build: src/ build: src/

View File

@ -1,6 +1,7 @@
import asyncio import asyncio
import json import json
import logging import logging
import os
import time import time
from datetime import datetime from datetime import datetime
@ -111,7 +112,13 @@ class PluralKitBot:
async def run(self): async def run(self):
try: try:
self.logger.info("Connecting to database...") self.logger.info("Connecting to database...")
self.pool = await db.connect() self.pool = await db.connect(
os.environ["DATABASE_USER"],
os.environ["DATABASE_PASS"],
os.environ["DATABASE_NAME"],
os.environ["DATABASE_HOST"],
int(os.environ["DATABASE_PORT"])
)
self.logger.info("Attempting to create tables...") self.logger.info("Attempting to create tables...")
async with self.pool.acquire() as conn: async with self.pool.acquire() as conn:

View File

@ -11,10 +11,10 @@ from discord.utils import snowflake_time
from pluralkit import System, Member, stats from pluralkit import System, Member, stats
logger = logging.getLogger("pluralkit.db") logger = logging.getLogger("pluralkit.db")
async def connect(): async def connect(username, password, database, host, port):
while True: while True:
try: try:
return await asyncpg.create_pool(user="postgres", password="postgres", database="postgres", host="db") return await asyncpg.create_pool(user=username, password=password, database=database, host=host, port=port)
except (ConnectionError, asyncpg.exceptions.CannotConnectNowError): except (ConnectionError, asyncpg.exceptions.CannotConnectNowError):
pass pass