mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2025-08-18 15:00:46 +08:00

* WIP: add rbac authorization * Perform pre-commit fixes * add rbac route whitelist * add init test data user role associations * Restore database table id naming to fix generic crud base * Add database section value uniqueness settings * Update the test directory to tests * Update route_name file name to health_check * Split user auth and user action interfaces * Fix conflict between merge and current branch * Add pymysql dependencies * Fix RBAC authentication method * Add the select serialisation tool * Fix missing return messages due to global exception handler slicing * Update the user interface with associated relationships * Add items to be completed * Perform pre-commit fixes * Add pre-made routers * Paging data return structure optimisation * Split user auth and user interface tests * Fix user register test data structure error * Fix duplicate named test classes
106 lines
2.6 KiB
Python
106 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from functools import lru_cache
|
|
|
|
from pydantic import BaseSettings, root_validator
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
# Env Config
|
|
ENVIRONMENT: str
|
|
|
|
# Env MySQL
|
|
DB_HOST: str
|
|
DB_PORT: int
|
|
DB_USER: str
|
|
DB_PASSWORD: str
|
|
|
|
# Env Redis
|
|
REDIS_HOST: str
|
|
REDIS_PORT: int
|
|
REDIS_PASSWORD: str
|
|
REDIS_DATABASE: int
|
|
|
|
# Env APScheduler Redis
|
|
APS_REDIS_HOST: str
|
|
APS_REDIS_PORT: int
|
|
APS_REDIS_PASSWORD: str
|
|
APS_REDIS_DATABASE: int
|
|
|
|
# Env Token
|
|
TOKEN_SECRET_KEY: str # 密钥 secrets.token_urlsafe(32))
|
|
|
|
# FastAPI
|
|
TITLE: str = 'FastAPI'
|
|
VERSION: str = '0.0.1'
|
|
DESCRIPTION: str = 'FastAPI Best Architecture'
|
|
DOCS_URL: str | None = '/v1/docs'
|
|
REDOCS_URL: str | None = '/v1/redocs'
|
|
OPENAPI_URL: str | None = '/v1/openapi'
|
|
|
|
@root_validator
|
|
def validator_api_url(cls, values):
|
|
if values['ENVIRONMENT'] == 'pro':
|
|
values['OPENAPI_URL'] = None
|
|
return values
|
|
|
|
# Uvicorn
|
|
UVICORN_HOST: str = '127.0.0.1'
|
|
UVICORN_PORT: int = 8000
|
|
UVICORN_RELOAD: bool = True
|
|
|
|
# Static Server
|
|
STATIC_FILES: bool = False
|
|
|
|
# MySQL
|
|
DB_ECHO: bool = True
|
|
DB_DATABASE: str = 'fba'
|
|
DB_CHARSET: str = 'utf8mb4'
|
|
|
|
# Redis
|
|
REDIS_TIMEOUT: int = 5
|
|
|
|
# APScheduler Redis
|
|
APS_REDIS_TIMEOUT: int = 10
|
|
|
|
# APScheduler Default
|
|
APS_COALESCE: bool = False # 是否合并运行
|
|
APS_MAX_INSTANCES: int = 3 # 最大实例数
|
|
APS_MISFIRE_GRACE_TIME: int = 60 # 任务错过执行时间后,最大容错时间,过期后不再执行,单位:秒
|
|
|
|
# Token
|
|
TOKEN_ALGORITHM: str = 'HS256' # 算法
|
|
TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1 # token 时效 60 * 24 * 1 = 1 天
|
|
TOKEN_URL_SWAGGER: str = '/v1/auth/users/swagger_login'
|
|
|
|
# Log
|
|
LOG_FILE_NAME: str = 'fba.log'
|
|
|
|
# Middleware
|
|
MIDDLEWARE_CORS: bool = True
|
|
MIDDLEWARE_GZIP: bool = True
|
|
MIDDLEWARE_ACCESS: bool = False
|
|
|
|
# Casbin
|
|
CASBIN_RBAC_MODEL_NAME: str = 'rbac_model.conf'
|
|
CASBIN_EXCLUDE: list[dict[str, str], dict[str, str]] = [
|
|
{'method': 'POST', 'path': '/api/v1/auth/users/swagger_login'},
|
|
{'method': 'POST', 'path': '/api/v1/auth/users/login'},
|
|
{'method': 'POST', 'path': '/api/v1/auth/users/register'},
|
|
{'method': 'POST', 'path': '/api/v1/auth/users/password/reset'},
|
|
]
|
|
|
|
class Config:
|
|
# https://docs.pydantic.dev/usage/settings/#dotenv-env-support
|
|
env_file = '.env'
|
|
env_file_encoding = 'utf-8'
|
|
|
|
|
|
@lru_cache
|
|
def get_settings():
|
|
"""读取配置优化写法"""
|
|
return Settings()
|
|
|
|
|
|
settings = get_settings()
|