First Commit

This commit is contained in:
2023-09-10 23:50:05 +04:00
commit 6d45d1c604
61 changed files with 19953 additions and 0 deletions

0
src/app/core/__init__.py Normal file
View File

40
src/app/core/config.py Normal file
View File

@@ -0,0 +1,40 @@
from pydantic import PostgresDsn
from pydantic_settings import BaseSettings
from pathlib import Path
class Settings(BaseSettings):
PROJECT_NAME: str = 'FastAPI Portfolio & Blog'
MAIN_PATH: Path = Path(__file__).resolve().parent.parent.parent # path to src folder
APP_PATH: Path = MAIN_PATH / 'app' # path to app folder
MEDIA_FOLDER: Path = Path('media') # name of media folder
MEDIA_PATH: Path = MAIN_PATH / MEDIA_FOLDER # path to media folder
FILE_FOLDERS: dict[str, Path] = {
'post_images': Path('post_images'),
}
SECRET_KEY: str
ACCESS_TOKEN_EXPIRE_MINUTES: int = 43200 # 30 days
POSTGRES_USER: str
POSTGRES_PASSWORD: str
POSTGRES_SERVER: str
POSTGRES_PORT: int = 5432
POSTGRES_DB: str
def get_postgres_dsn(self, _async: bool=False) -> PostgresDsn:
scheme = 'postgresql+asyncpg' if _async else 'postgresql'
return PostgresDsn.build(
scheme=scheme,
username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD,
host=self.POSTGRES_SERVER,
port=self.POSTGRES_PORT,
path=self.POSTGRES_DB
)
settings = Settings()

39
src/app/core/security.py Normal file
View File

@@ -0,0 +1,39 @@
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)