Skip to content

Commit ed62667

Browse files
committed
Docker
1 parent 17778d9 commit ed62667

File tree

12 files changed

+909
-20
lines changed

12 files changed

+909
-20
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.env

.env.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ APP_DESCRIPTION=
44
APP_API_PREFIX=
55
APP_SECRET_KEY=
66

7+
POSTGRES_DB=
78
POSTGRES_HOST=
89
POSTGRES_PORT=
910
POSTGRES_USER=
1011
POSTGRES_PASSWORD=
11-
POSTGRES_DATABASE=
1212

1313
TELEGRAM_BOT_TOKEN=
1414
TELEGRAM_BOT_USERNAME=

Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.12-alpine as python-base
2+
ENV PYTHONDONTWRITEBYTECODE=1
3+
ENV PYTHONUNBUFFERED=1
4+
WORKDIR /code
5+
COPY requirements.txt /code/
6+
RUN pip install -r requirements.txt
7+
COPY . /code/

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ ref: blue isort ruff
2929
dev:
3030
poetry run fastapi dev app
3131

32+
.PHONY: build
33+
build:
34+
poetry export -f requirements.txt --output requirements.txt
35+
docker compose build
36+
3237
.PHONY: generate
3338
generate:
3439
poetry run alembic revision --autogenerate

app/api/deps.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
"""
44

55
from fastapi import Depends
6-
from fastapi.security import HTTPAuthorizationCredentials
76
from typing_extensions import Annotated
7+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
88

99
from app import models
1010
from app.core import exps, settings
1111
from app.core.db import Database, SessionLocal
12-
from app.core.security import JWTManager, oauth
12+
from app.core.security import JWTManager, TgAuth
13+
14+
15+
oauth = HTTPBearer()
1316

1417

1518
async def get_db() -> Database:
@@ -21,14 +24,14 @@ async def get_jwt_manager() -> JWTManager:
2124
return JWTManager(settings.APP_SECRET_KEY)
2225

2326

24-
async def get_oauth_telegram() -> oauth.Telegram:
25-
return oauth.Telegram(
27+
async def get_tg_auth() -> TgAuth:
28+
return TgAuth(
2629
settings.TELEGRAM_BOT_TOKEN, settings.TELEGRAM_BOT_USERNAME
2730
)
2831

2932

3033
async def get_current_user(
31-
credentials: Annotated[HTTPAuthorizationCredentials, oauth.oauth],
34+
credentials: Annotated[HTTPAuthorizationCredentials, oauth],
3235
jwt_manager: Annotated[JWTManager, Depends(get_jwt_manager)],
3336
db: Annotated[Database, Depends(get_db)],
3437
) -> models.User:

app/core/db.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def get_async_engine() -> AsyncEngine:
1414
engine: AsyncEngine = create_async_engine(
15-
settings.pg_dns.unicode_string(), echo=False, future=True
15+
settings.pg_dsn.unicode_string(), echo=False, future=True
1616
)
1717
return engine
1818

app/core/security/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from . import oauth
1+
from .tg_auth import TgAuth
22
from .jwt_manager import JWTManager
33

4-
__all__ = ['JWTManager', 'oauth']
4+
__all__ = ['JWTManager', 'TgAuth']

app/core/security/oauth.py renamed to app/core/security/tg_auth.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import hashlib
22
import hmac
33

4-
from fastapi.security import HTTPBearer
54

6-
oauth = HTTPBearer()
7-
8-
9-
class Telegram:
5+
class TgAuth:
106
def __init__(self, token: str, username: str):
117
self.token = token
128
self.username = username

app/core/settings.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ class Settings(BaseSettings):
1919
APP_API_PREFIX: str
2020

2121
# DATABASE
22+
POSTGRES_DB: str
2223
POSTGRES_HOST: str
2324
POSTGRES_PORT: int
2425
POSTGRES_USER: str
2526
POSTGRES_PASSWORD: str
26-
POSTGRES_DATABASE: str
2727

2828
@property
29-
def pg_dns(self) -> PostgresDsn:
30-
dns = PostgresDsn.build(
29+
def pg_dsn(self) -> PostgresDsn:
30+
dsn = PostgresDsn.build(
3131
scheme='postgresql+asyncpg',
3232
username=self.POSTGRES_USER,
3333
password=self.POSTGRES_PASSWORD,
3434
host=self.POSTGRES_HOST,
3535
port=self.POSTGRES_PORT,
36-
path=self.POSTGRES_DATABASE,
36+
path=self.POSTGRES_DB,
3737
)
38-
return dns
38+
return dsn
3939

4040
# Telegram
4141
TELEGRAM_BOT_TOKEN: str

docker-compose.yaml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: fastapi_template
2+
3+
services:
4+
backend:
5+
build: .
6+
command: sh -c "alembic upgrade head && fastapi run app --reload"
7+
volumes:
8+
- .:/code
9+
ports:
10+
- "8000:8000"
11+
env_file:
12+
- .env
13+
networks:
14+
- network
15+
depends_on:
16+
postgres:
17+
condition: service_healthy
18+
19+
postgres:
20+
image: postgres:15.1-alpine
21+
env_file:
22+
- .env
23+
volumes:
24+
- pg_data:/var/lib/postgresql/data
25+
expose:
26+
- 5432
27+
healthcheck:
28+
test: ["CMD-SHELL", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]
29+
interval: 10s
30+
timeout: 30s
31+
retries: 5
32+
start_period: 80s
33+
networks:
34+
- network
35+
36+
volumes:
37+
pg_data:
38+
39+
networks:
40+
network:
41+
name: network

migrations/env.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# target_metadata = mymodel.Base.metadata
2525
target_metadata = SQLModel.metadata
2626

27-
config.set_main_option('sqlalchemy.url', settings.pg_dns.unicode_string())
27+
config.set_main_option('sqlalchemy.url', settings.pg_dsn.unicode_string())
2828

2929

3030
# other values from the config, defined by the needs of env.py,

requirements.txt

+836
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)