mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 06:57:47 +01:00
feat(infrastructure): add basic project deployment
This commit is contained in:
7
backend/Dockerfile
Normal file
7
backend/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM python:3.11-slim
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY . .
|
||||
EXPOSE 8000
|
||||
CMD ["uvicorn", "app.app:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
@@ -1,11 +1,24 @@
|
||||
from fastapi import Depends, FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .db import User, create_db_and_tables
|
||||
from .schemas import UserCreate, UserRead, UserUpdate
|
||||
from .users import auth_backend, current_active_user, fastapi_users
|
||||
from .users import auth_backend, current_active_verified_user, fastapi_users
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# CORS for frontend dev server
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:5173",
|
||||
],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(
|
||||
fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"]
|
||||
)
|
||||
@@ -32,7 +45,7 @@ app.include_router(
|
||||
|
||||
|
||||
@app.get("/authenticated-route")
|
||||
async def authenticated_route(user: User = Depends(current_active_user)):
|
||||
async def authenticated_route(user: User = Depends(current_active_verified_user)):
|
||||
return {"message": f"Hello {user.email}!"}
|
||||
|
||||
|
||||
|
||||
47
backend/app/queue.py
Normal file
47
backend/app/queue.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Dict
|
||||
|
||||
import asyncio
|
||||
|
||||
RABBITMQ_URL = os.getenv("RABBITMQ_URL") or (
|
||||
f"amqp://{os.getenv('RABBITMQ_USERNAME', 'user')}:"
|
||||
f"{os.getenv('RABBITMQ_PASSWORD', 'bitnami123')}@"
|
||||
f"{os.getenv('RABBITMQ_HOST', 'localhost')}:"
|
||||
f"{os.getenv('RABBITMQ_PORT', '5672')}"
|
||||
)
|
||||
QUEUE_NAME = os.getenv("MAIL_QUEUE", "mail_queue")
|
||||
|
||||
|
||||
async def _publish_async(message: Dict[str, Any]) -> None:
|
||||
# Import locally to avoid hard dependency at import-time
|
||||
import aio_pika
|
||||
|
||||
connection = await aio_pika.connect_robust(RABBITMQ_URL)
|
||||
try:
|
||||
channel = await connection.channel()
|
||||
await channel.declare_queue(QUEUE_NAME, durable=True)
|
||||
body = json.dumps(message).encode("utf-8")
|
||||
await channel.default_exchange.publish(
|
||||
aio_pika.Message(body=body, delivery_mode=aio_pika.DeliveryMode.PERSISTENT),
|
||||
routing_key=QUEUE_NAME,
|
||||
)
|
||||
finally:
|
||||
await connection.close()
|
||||
|
||||
|
||||
def enqueue_email(to: str, subject: str, body: str) -> None:
|
||||
"""
|
||||
Enqueue an email to RabbitMQ. If RabbitMQ or aio_pika is not available,
|
||||
this function will raise ImportError/ConnectionError. The caller may
|
||||
implement fallback (e.g., direct send).
|
||||
"""
|
||||
message = {"type": "email", "to": to, "subject": subject, "body": body}
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
# Fire-and-forget task so we don't block the request path
|
||||
loop.create_task(_publish_async(message))
|
||||
except RuntimeError:
|
||||
# No running loop (e.g., called from sync context) – run a short loop
|
||||
asyncio.run(_publish_async(message))
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
@@ -12,7 +13,9 @@ from fastapi_users.db import SQLAlchemyUserDatabase
|
||||
|
||||
from .db import User, get_user_db
|
||||
|
||||
SECRET = "SECRET"
|
||||
SECRET = os.getenv("SECRET", "CHANGE_ME_SECRET")
|
||||
FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:5173")
|
||||
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
|
||||
|
||||
|
||||
class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
@@ -20,7 +23,8 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
verification_token_secret = SECRET
|
||||
|
||||
async def on_after_register(self, user: User, request: Optional[Request] = None):
|
||||
print(f"User {user.id} has registered.")
|
||||
# Ask FastAPI Users to generate a verification token and trigger the hook below
|
||||
await self.request_verify(user, request)
|
||||
|
||||
async def on_after_forgot_password(
|
||||
self, user: User, token: str, request: Optional[Request] = None
|
||||
@@ -30,7 +34,26 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
async def on_after_request_verify(
|
||||
self, user: User, token: str, request: Optional[Request] = None
|
||||
):
|
||||
print(f"Verification requested for user {user.id}. Verification token: {token}")
|
||||
# Build verification email and send through RabbitMQ (with direct SMTP fallback)
|
||||
verify_frontend_link = f"{FRONTEND_URL}/verify?token={token}"
|
||||
verify_backend_link = f"{BACKEND_URL}/auth/verify?token={token}"
|
||||
subject = "Ověření účtu"
|
||||
body = (
|
||||
"Ahoj,\n\n"
|
||||
"děkujeme za registraci. Prosíme, ověř svůj účet kliknutím na tento odkaz:\n"
|
||||
f"{verify_frontend_link}\n\n"
|
||||
"Pokud by odkaz nefungoval, můžeš použít i přímý odkaz na backend:\n"
|
||||
f"{verify_backend_link}\n\n"
|
||||
"Pokud jsi registraci neprováděl(a), tento email ignoruj.\n"
|
||||
)
|
||||
try:
|
||||
from .queue import enqueue_email
|
||||
enqueue_email(to=user.email, subject=subject, body=body)
|
||||
except Exception:
|
||||
# Fallback: if queue is unavailable, log the email content (dev fallback)
|
||||
print("[Email Fallback] To:", user.email)
|
||||
print("[Email Fallback] Subject:", subject)
|
||||
print("[Email Fallback] Body:\n", body)
|
||||
|
||||
|
||||
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)):
|
||||
@@ -53,3 +76,4 @@ auth_backend = AuthenticationBackend(
|
||||
fastapi_users = FastAPIUsers[User, uuid.UUID](get_user_manager, [auth_backend])
|
||||
|
||||
current_active_user = fastapi_users.current_user(active=True)
|
||||
current_active_verified_user = fastapi_users.current_user(active=True, verified=True)
|
||||
|
||||
44
backend/requirements.txt
Normal file
44
backend/requirements.txt
Normal file
@@ -0,0 +1,44 @@
|
||||
aio-pika==9.5.6
|
||||
aiormq==6.8.1
|
||||
aiosqlite==0.21.0
|
||||
annotated-types==0.7.0
|
||||
anyio==4.11.0
|
||||
argon2-cffi==23.1.0
|
||||
argon2-cffi-bindings==25.1.0
|
||||
asyncmy==0.2.9
|
||||
bcrypt==4.3.0
|
||||
cffi==2.0.0
|
||||
click==8.1.8
|
||||
cryptography==46.0.1
|
||||
dnspython==2.7.0
|
||||
email_validator==2.2.0
|
||||
exceptiongroup==1.3.0
|
||||
fastapi==0.117.1
|
||||
fastapi-users==14.0.1
|
||||
fastapi-users-db-sqlalchemy==7.0.0
|
||||
greenlet==3.2.4
|
||||
h11==0.16.0
|
||||
httptools==0.6.4
|
||||
idna==3.10
|
||||
makefun==1.16.0
|
||||
multidict==6.6.4
|
||||
pamqp==3.3.0
|
||||
propcache==0.3.2
|
||||
pwdlib==0.2.1
|
||||
pycparser==2.23
|
||||
pydantic==2.11.9
|
||||
pydantic_core==2.33.2
|
||||
PyJWT==2.10.1
|
||||
python-dotenv==1.1.1
|
||||
python-multipart==0.0.20
|
||||
PyYAML==6.0.2
|
||||
sniffio==1.3.1
|
||||
SQLAlchemy==2.0.43
|
||||
starlette==0.48.0
|
||||
typing-inspection==0.4.1
|
||||
typing_extensions==4.15.0
|
||||
uvicorn==0.37.0
|
||||
uvloop==0.21.0
|
||||
watchfiles==1.1.0
|
||||
websockets==15.0.1
|
||||
yarl==1.20.1
|
||||
0
backend/worker/__init__.py
Normal file
0
backend/worker/__init__.py
Normal file
57
backend/worker/email_worker.py
Normal file
57
backend/worker/email_worker.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Dict
|
||||
|
||||
|
||||
RABBITMQ_URL = os.getenv("RABBITMQ_URL") or (
|
||||
f"amqp://{os.getenv('RABBITMQ_USERNAME', 'user')}:"
|
||||
f"{os.getenv('RABBITMQ_PASSWORD', 'bitnami123')}@"
|
||||
f"{os.getenv('RABBITMQ_HOST', 'localhost')}:"
|
||||
f"{os.getenv('RABBITMQ_PORT', '5672')}"
|
||||
)
|
||||
QUEUE_NAME = os.getenv("MAIL_QUEUE", "mail_queue")
|
||||
|
||||
|
||||
async def handle_message(message_body: bytes) -> None:
|
||||
try:
|
||||
data: Dict[str, Any] = json.loads(message_body.decode("utf-8"))
|
||||
except Exception as e:
|
||||
print(f"[email_worker] Failed to decode message: {e}")
|
||||
return
|
||||
|
||||
if data.get("type") != "email":
|
||||
print(f"[email_worker] Unknown message type: {data}")
|
||||
return
|
||||
|
||||
to = data.get("to")
|
||||
subject = data.get("subject")
|
||||
body = data.get("body")
|
||||
if not (to and subject and body):
|
||||
print(f"[email_worker] Incomplete email message: {data}")
|
||||
return
|
||||
|
||||
try:
|
||||
await send_email(to=to, subject=subject, body=body)
|
||||
print(f"[email_worker] Sent email to {to}")
|
||||
except Exception as e:
|
||||
print(f"[email_worker] Error sending email to {to}: {e}")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
import aio_pika
|
||||
|
||||
print(f"[email_worker] Connecting to RabbitMQ at {RABBITMQ_URL}")
|
||||
connection = await aio_pika.connect_robust(RABBITMQ_URL)
|
||||
channel = await connection.channel()
|
||||
queue = await channel.declare_queue(QUEUE_NAME, durable=True)
|
||||
print(f"[email_worker] Waiting for messages in queue '{QUEUE_NAME}' ...")
|
||||
|
||||
async with queue.iterator() as queue_iter:
|
||||
async for message in queue_iter:
|
||||
async with message.process(requeue=False):
|
||||
await handle_message(message.body)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user