🚚 Refactor and simplify backend file structure (#609)
This commit is contained in:

committed by
GitHub

parent
a065f9c9e8
commit
73b2884057
0
src/backend/app/core/__init__.py
Normal file
0
src/backend/app/core/__init__.py
Normal file
5
src/backend/app/core/celery_app.py
Normal file
5
src/backend/app/core/celery_app.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from celery import Celery
|
||||
|
||||
celery_app = Celery("worker", broker="amqp://guest@queue//")
|
||||
|
||||
celery_app.conf.task_routes = {"app.worker.test_celery": "main-queue"}
|
89
src/backend/app/core/config.py
Normal file
89
src/backend/app/core/config.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import secrets
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
API_V1_STR: str = "/api/v1"
|
||||
SECRET_KEY: str = secrets.token_urlsafe(32)
|
||||
# 60 minutes * 24 hours * 8 days = 8 days
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
||||
SERVER_NAME: str
|
||||
SERVER_HOST: AnyHttpUrl
|
||||
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
|
||||
# e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
|
||||
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
|
||||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
||||
|
||||
@validator("BACKEND_CORS_ORIGINS", pre=True)
|
||||
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
|
||||
if isinstance(v, str) and not v.startswith("["):
|
||||
return [i.strip() for i in v.split(",")]
|
||||
elif isinstance(v, (list, str)):
|
||||
return v
|
||||
raise ValueError(v)
|
||||
|
||||
PROJECT_NAME: str
|
||||
SENTRY_DSN: Optional[HttpUrl] = None
|
||||
|
||||
@validator("SENTRY_DSN", pre=True)
|
||||
def sentry_dsn_can_be_blank(cls, v: str) -> Optional[str]:
|
||||
if len(v) == 0:
|
||||
return None
|
||||
return v
|
||||
|
||||
POSTGRES_SERVER: str
|
||||
POSTGRES_USER: str
|
||||
POSTGRES_PASSWORD: str
|
||||
POSTGRES_DB: str
|
||||
SQLALCHEMY_DATABASE_URI: Optional[PostgresDsn] = None
|
||||
|
||||
@validator("SQLALCHEMY_DATABASE_URI", pre=True)
|
||||
def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
|
||||
if isinstance(v, str):
|
||||
return v
|
||||
return PostgresDsn.build(
|
||||
scheme="postgresql+psycopg",
|
||||
user=values.get("POSTGRES_USER"),
|
||||
password=values.get("POSTGRES_PASSWORD"),
|
||||
host=values.get("POSTGRES_SERVER"),
|
||||
path=f"/{values.get('POSTGRES_DB') or ''}",
|
||||
)
|
||||
|
||||
SMTP_TLS: bool = True
|
||||
SMTP_PORT: Optional[int] = None
|
||||
SMTP_HOST: Optional[str] = None
|
||||
SMTP_USER: Optional[str] = None
|
||||
SMTP_PASSWORD: Optional[str] = None
|
||||
EMAILS_FROM_EMAIL: Optional[EmailStr] = None
|
||||
EMAILS_FROM_NAME: Optional[str] = None
|
||||
|
||||
@validator("EMAILS_FROM_NAME")
|
||||
def get_project_name(cls, v: Optional[str], values: Dict[str, Any]) -> str:
|
||||
if not v:
|
||||
return values["PROJECT_NAME"]
|
||||
return v
|
||||
|
||||
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
|
||||
EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build"
|
||||
EMAILS_ENABLED: bool = False
|
||||
|
||||
@validator("EMAILS_ENABLED", pre=True)
|
||||
def get_emails_enabled(cls, v: bool, values: Dict[str, Any]) -> bool:
|
||||
return bool(
|
||||
values.get("SMTP_HOST")
|
||||
and values.get("SMTP_PORT")
|
||||
and values.get("EMAILS_FROM_EMAIL")
|
||||
)
|
||||
|
||||
EMAIL_TEST_USER: EmailStr = "test@example.com" # type: ignore
|
||||
FIRST_SUPERUSER: EmailStr
|
||||
FIRST_SUPERUSER_PASSWORD: str
|
||||
USERS_OPEN_REGISTRATION: bool = False
|
||||
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
|
||||
|
||||
settings = Settings()
|
34
src/backend/app/core/security.py
Normal file
34
src/backend/app/core/security.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Union
|
||||
|
||||
from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
|
||||
def create_access_token(
|
||||
subject: Union[str, Any], expires_delta: timedelta = None
|
||||
) -> str:
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(
|
||||
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
)
|
||||
to_encode = {"exp": expire, "sub": str(subject)}
|
||||
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
Reference in New Issue
Block a user