Skip to content

Commit

Permalink
Add site as a project dependency
Browse files Browse the repository at this point in the history
This commit furthermore configures the aiohttp session to easily interact with the site api.
  • Loading branch information
D0rs4n committed Mar 28, 2022
1 parent 991471f commit 34bea00
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bot/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 2 additions & 0 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional

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
Expand All @@ -18,6 +19,7 @@ class SirRobin(commands.Bot):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._guild_available: Optional[asyncio.Event] = None
self.http_session: Optional[aiohttp.ClientSession] = None

async def add_cog(self, cog: commands.Cog, **kwargs) -> None:
"""
Expand Down
44 changes: 44 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

0 comments on commit 34bea00

Please sign in to comment.