-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
835 additions
and
2,742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Dockerfile-api | ||
FROM python:3.11.0-slim | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
COPY poetry.lock pyproject.toml /app/ | ||
|
||
RUN pip install poetry | ||
RUN poetry config virtualenvs.create false | ||
RUN poetry install --no-dev --without api | ||
|
||
CMD ["python", "/app/services/main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
import asyncpg | ||
from asyncpg import Record | ||
|
||
from api.business.factory import AbstractFactory, FactoryItem | ||
from api.config.database import DatabaseSettings, get_database_settings | ||
|
||
from loguru import logger | ||
|
||
# from psycopg2 import pool | ||
# from configs.settings import settings | ||
|
||
|
||
class Database(ABC): | ||
db_settings: DatabaseSettings | ||
|
||
def __init__(self): | ||
self.db_settings = get_database_settings() | ||
|
||
@abstractmethod | ||
async def query(self, query: str, values: Optional[list] = None): | ||
pass | ||
|
||
@abstractmethod | ||
async def connect(self): | ||
pass | ||
|
||
@abstractmethod | ||
async def close(self): | ||
pass | ||
|
||
|
||
class Postgres(Database): | ||
def __init__(self): | ||
super().__init__() | ||
self.user = self.db_settings.DATABASE_USER | ||
self.password = self.db_settings.DATABASE_PASSWORD | ||
self.host = self.db_settings.DATABASE_HOST | ||
self.port = self.db_settings.DATABASE_PORT | ||
self.database = self.db_settings.DATABASE_NAME | ||
self._cursor = None | ||
|
||
self._connection_pool = None | ||
self.con = None | ||
|
||
def close(self): | ||
if self._connection_pool: | ||
self._connection_pool.close() | ||
|
||
async def connect(self): | ||
if not self._connection_pool: | ||
try: | ||
self._connection_pool = await asyncpg.create_pool( | ||
min_size=1, | ||
max_size=10, | ||
command_timeout=60, | ||
host=self.host, | ||
port=self.port, | ||
user=self.user, | ||
password=self.password, | ||
database=self.database, | ||
) | ||
|
||
except Exception as e: | ||
print(e) | ||
|
||
async def query(self, query: str, values: Optional[list] = None) -> list[Record]: | ||
if not self._connection_pool: | ||
await self.connect() | ||
|
||
logger.debug(f"Query: {query}") | ||
|
||
if values: | ||
values = tuple(values) | ||
|
||
self.con = await self._connection_pool.acquire() | ||
try: | ||
cursor = await self.con.fetch(query, *values) | ||
logger.debug(f"Result: {cursor}") | ||
return cursor | ||
except Exception as e: | ||
print(e) | ||
finally: | ||
await self._connection_pool.release(self.con) | ||
|
||
|
||
class DatabaseFactory(AbstractFactory): | ||
_values = {"postgres": FactoryItem(name="postgres", factory_item=Postgres)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
from abc import abstractmethod | ||
from multiprocessing import Event | ||
|
||
from api.main import BASE_PATH | ||
import httpx | ||
import os | ||
|
||
class Service: | ||
@abstractmethod | ||
async def start(self): | ||
raise NotImplementedError | ||
|
||
class Service: | ||
client: httpx.AsyncClient | ||
|
||
def __init__(self): | ||
host = os.getenv("API_HOST", "http://localhost:8000") | ||
if host.endswith("/"): | ||
host = host[:-1] | ||
|
||
self.client = httpx.AsyncClient(base_url=f"{host}{BASE_PATH}", timeout=60) | ||
|
||
@abstractmethod | ||
async def start(self): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.