Files
Wu Clan 5e438c685d Refactor the backend architecture (#299)
* define the basic architecture

* Update script and deployment file locations

* Update the route registration

* Fix CI download dependencies

* Updated ruff to 0.3.3

* Update app subdirectory naming

* Update the model import

* fix pre-commit pdm lock

* Update the service directory naming

* Add CRUD method documents

* Fix the issue of circular import

* Update the README document

* Update the SQL statement for create tables

* Update docker scripts and documentation

* Fix docker scripts

* Update the backend README.md

* Add the security folder and move the redis client

* Update the configuration item

* Fix environment configuration reads

* Update the default configuration

* Updated README description

* Updated the user registration API

* Fix test cases

* Update the celery configuration

* Update and fix celery configuration

* Updated the celery structure

* Update celery tasks and api

* Add celery flower

* Update the import style

* Update contributors
2024-03-22 18:16:15 +08:00

154 lines
4.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import lru_cache
from typing import Literal
from pydantic import model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from backend.core.path_conf import BasePath
class Settings(BaseSettings):
"""Global Settings"""
model_config = SettingsConfigDict(env_file=f'{BasePath}/.env', env_file_encoding='utf-8', extra='ignore')
# Env Config
ENVIRONMENT: Literal['dev', 'pro']
# Env MySQL
MYSQL_HOST: str
MYSQL_PORT: int
MYSQL_USER: str
MYSQL_PASSWORD: str
# Env Redis
REDIS_HOST: str
REDIS_PORT: int
REDIS_PASSWORD: str
REDIS_DATABASE: int
# Env Token
TOKEN_SECRET_KEY: str # 密钥 secrets.token_urlsafe(32)
# Env Opera Log
OPERA_LOG_ENCRYPT_SECRET_KEY: str # 密钥 os.urandom(32), 需使用 bytes.hex() 方法转换为 str
# FastAPI
API_V1_STR: str = '/api/v1'
TITLE: str = 'FastAPI'
VERSION: str = '0.0.1'
DESCRIPTION: str = 'FastAPI Best Architecture'
DOCS_URL: str | None = f'{API_V1_STR}/docs'
REDOCS_URL: str | None = f'{API_V1_STR}/redocs'
OPENAPI_URL: str | None = f'{API_V1_STR}/openapi'
@model_validator(mode='before')
@classmethod
def validate_openapi_url(cls, values):
if values['ENVIRONMENT'] == 'pro':
values['OPENAPI_URL'] = None
return values
# Demo mode
# Only GET, OPTIONS requests are allowed
DEMO_MODE: bool = False
DEMO_MODE_EXCLUDE: set[tuple[str, str]] = {
('POST', f'{API_V1_STR}/auth/login'),
('POST', f'{API_V1_STR}/auth/logout'),
('GET', f'{API_V1_STR}/auth/captcha'),
}
# Uvicorn
UVICORN_HOST: str = '127.0.0.1'
UVICORN_PORT: int = 8000
UVICORN_RELOAD: bool = True
# Static Server
STATIC_FILES: bool = False
# Location Parse
LOCATION_PARSE: Literal['online', 'offline', 'false'] = 'offline'
# Limiter
LIMITER_REDIS_PREFIX: str = 'fba_limiter'
# DateTime
DATETIME_TIMEZONE: str = 'Asia/Shanghai'
DATETIME_FORMAT: str = '%Y-%m-%d %H:%M:%S'
# MySQL
MYSQL_ECHO: bool = False
MYSQL_DATABASE: str = 'fba'
MYSQL_CHARSET: str = 'utf8mb4'
# Redis
REDIS_TIMEOUT: int = 5
# Token
TOKEN_ALGORITHM: str = 'HS256' # 算法
TOKEN_EXPIRE_SECONDS: int = 60 * 60 * 24 * 1 # 过期时间,单位:秒
TOKEN_REFRESH_EXPIRE_SECONDS: int = 60 * 60 * 24 * 7 # 刷新过期时间,单位:秒
TOKEN_REDIS_PREFIX: str = 'fba_token'
TOKEN_REFRESH_REDIS_PREFIX: str = 'fba_refresh_token'
TOKEN_EXCLUDE: list[str] = [ # JWT / RBAC 白名单
f'{API_V1_STR}/auth/login',
]
# Log
LOG_STDOUT_FILENAME: str = 'fba_access.log'
LOG_STDERR_FILENAME: str = 'fba_error.log'
# Middleware
MIDDLEWARE_CORS: bool = True
MIDDLEWARE_GZIP: bool = True
MIDDLEWARE_ACCESS: bool = False
# RBAC Permission
PERMISSION_MODE: Literal['casbin', 'role-menu'] = 'casbin'
PERMISSION_REDIS_PREFIX: str = 'fba_permission'
# Casbin Auth
CASBIN_EXCLUDE: set[tuple[str, str]] = {
('POST', f'{API_V1_STR}/auth/logout'),
('POST', f'{API_V1_STR}/auth/token/new'),
}
# Role Menu Auth
ROLE_MENU_EXCLUDE: list[str] = [
'sys:monitor:redis',
'sys:monitor:server',
]
# Opera log
OPERA_LOG_EXCLUDE: list[str] = [
'/favicon.ico',
DOCS_URL,
REDOCS_URL,
OPENAPI_URL,
f'{API_V1_STR}/auth/login/swagger',
f'{API_V1_STR}/auth/github/callback',
]
OPERA_LOG_ENCRYPT: int = 1 # 0: AES (性能损耗); 1: md5; 2: ItsDangerous; 3: 不加密, others: 替换为 ******
OPERA_LOG_ENCRYPT_INCLUDE: list[str] = [
'password',
'old_password',
'new_password',
'confirm_password',
]
# Ip location
IP_LOCATION_REDIS_PREFIX: str = 'fba_ip_location'
IP_LOCATION_EXPIRE_SECONDS: int = 60 * 60 * 24 * 1 # 过期时间,单位:秒
@lru_cache
def get_settings() -> Settings:
"""获取全局配置"""
return Settings()
# 创建配置实例
settings = get_settings()