From 9663f1824ac3535d478112c9010a7c102b3529ca Mon Sep 17 00:00:00 2001 From: D0rs4n <41237606+D0rs4n@users.noreply.github.com> Date: Mon, 28 Mar 2022 20:16:32 +0200 Subject: [PATCH] Add site as a project dependency This commit furthermore configures the aiohttp session to easily interact with the site api. --- bot/__main__.py | 17 +++++++++++++++++ bot/bot.py | 2 ++ docker-compose.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/bot/__main__.py b/bot/__main__.py index cc249a3..cf6bae9 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,10 +1,27 @@ import asyncio +import socket + +import aiohttp from bot.bot import bot from bot.constants import Client if not Client.in_ci: async def main() -> None: + bot._resolver = aiohttp.AsyncResolver() + + # Use AF_INET as its socket family to prevent HTTPS related problems both locally + # and in production. + bot._connector = aiohttp.TCPConnector( + resolver=bot._resolver, + family=socket.AF_INET, + ) + + # Client.login() will call HTTPClient.static_login() which will create a session using + # this connector attribute. + bot.http.connector = bot._connector + + bot.http_session = aiohttp.ClientSession(connector=bot._connector) """Entry Async method for starting the bot.""" async with bot: bot._guild_available = asyncio.Event() diff --git a/bot/bot.py b/bot/bot.py index 985a8ae..99717a5 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,6 +3,7 @@ import aiohttp import discord +import aiohttp from botcore.utils.extensions import walk_extensions from botcore.utils.logging import get_logger from botcore.utils.scheduling import create_task @@ -24,6 +25,7 @@ def __init__(self, **kwargs): self.http_session: Optional[aiohttp.ClientSession] = None self._guild_available: Optional[asyncio.Event] = None + self.http_session: Optional[aiohttp.ClientSession] = None async def login(self, *args, **kwargs) -> None: """On login, create an aiohttp client session to be used across the bot.""" diff --git a/docker-compose.yml b/docker-compose.yml index 7662b2d..1cd8fcb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,12 @@ version: "3.7" +x-logging: &logging + logging: + driver: "json-file" + options: + max-file: "5" + max-size: "10m" + x-restart-policy: &restart_policy restart: unless-stopped @@ -29,3 +36,40 @@ services: image: redis:latest ports: - "127.0.0.1:6379:6379" + + postgres: + <<: *logging + <<: *restart_policy + image: postgres:13-alpine + environment: + POSTGRES_DB: pysite + POSTGRES_PASSWORD: pysite + POSTGRES_USER: pysite + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U pysite" ] + interval: 2s + timeout: 1s + retries: 5 + + web: + <<: *logging + <<: *restart_policy + image: ghcr.io/python-discord/site:latest + command: [ "run", "--debug" ] + networks: + default: + aliases: + - api.web + - admin.web + - staff.web + ports: + - "127.0.0.1:8000:8000" + depends_on: + postgres: + condition: service_healthy + tty: true + environment: + DATABASE_URL: postgres://pysite:pysite@postgres:5432/pysite + METRICITY_DB_URL: postgres://pysite:pysite@postgres:5432/metricity + SECRET_KEY: suitable-for-development-only + STATIC_ROOT: /var/www/static