mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Update system config to dynamic (#447)
* Update system config to be dynamic * update the configuration interface * custom parameters are allowed * update apis * add more code * finish * fix bugs
This commit is contained in:
@@ -18,8 +18,8 @@ router.include_router(api_router, prefix='/apis', tags=['系统API'])
|
||||
router.include_router(casbin_router, prefix='/casbin', tags=['系统Casbin权限'])
|
||||
router.include_router(config_router, prefix='/configs', tags=['系统配置'])
|
||||
router.include_router(dept_router, prefix='/depts', tags=['系统部门'])
|
||||
router.include_router(dict_data_router, prefix='/dict_datas', tags=['系统字典数据'])
|
||||
router.include_router(dict_type_router, prefix='/dict_types', tags=['系统字典类型'])
|
||||
router.include_router(dict_data_router, prefix='/dict-datas', tags=['系统字典数据'])
|
||||
router.include_router(dict_type_router, prefix='/dict-types', tags=['系统字典类型'])
|
||||
router.include_router(menu_router, prefix='/menus', tags=['系统目录'])
|
||||
router.include_router(role_router, prefix='/roles', tags=['系统角色'])
|
||||
router.include_router(user_router, prefix='/users', tags=['系统用户'])
|
||||
|
||||
@@ -4,44 +4,126 @@ from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, Path, Query
|
||||
|
||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
||||
from backend.app.admin.schema.config import (
|
||||
CreateAnyConfigParam,
|
||||
GetAnyConfigListDetails,
|
||||
SaveConfigParam,
|
||||
UpdateAnyConfigParam,
|
||||
)
|
||||
from backend.app.admin.service.config_service import config_service
|
||||
from backend.common.pagination import DependsPagination, paging_data
|
||||
from backend.common.response.response_schema import ResponseModel, response_base
|
||||
from backend.common.security.jwt import DependsJwtAuth
|
||||
from backend.common.security.permission import RequestPermission
|
||||
from backend.common.security.rbac import DependsRBAC
|
||||
from backend.database.db_mysql import CurrentSession
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('', summary='获取系统配置详情', dependencies=[DependsJwtAuth])
|
||||
async def get_config() -> ResponseModel:
|
||||
config = await config_service.get()
|
||||
@router.get('/website', summary='获取网站配置信息', dependencies=[DependsJwtAuth])
|
||||
async def get_website_config() -> ResponseModel:
|
||||
config = await config_service.get_built_in_config('website')
|
||||
return response_base.success(data=config)
|
||||
|
||||
|
||||
@router.post(
|
||||
'/website',
|
||||
summary='保存网站配置信息',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:website:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def save_website_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||
await config_service.save_built_in_config(objs, 'website')
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@router.get('/protocol', summary='获取用户协议', dependencies=[DependsJwtAuth])
|
||||
async def get_protocol_config() -> ResponseModel:
|
||||
config = await config_service.get_built_in_config('protocol')
|
||||
return response_base.success(data=config)
|
||||
|
||||
|
||||
@router.post(
|
||||
'/protocol',
|
||||
summary='保存用户协议',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:protocol:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def save_protocol_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||
await config_service.save_built_in_config(objs, 'protocol')
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@router.get('/policy', summary='获取用户政策', dependencies=[DependsJwtAuth])
|
||||
async def get_policy_config() -> ResponseModel:
|
||||
config = await config_service.get_built_in_config('policy')
|
||||
return response_base.success(data=config)
|
||||
|
||||
|
||||
@router.post(
|
||||
'/policy',
|
||||
summary='保存用户政策',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:policy:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def save_policy_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||
await config_service.save_built_in_config(objs, 'policy')
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@router.get('/{pk}', summary='获取系统参数配置详情', dependencies=[DependsJwtAuth])
|
||||
async def get_config(pk: Annotated[int, Path(...)]) -> ResponseModel:
|
||||
config = await config_service.get(pk)
|
||||
return response_base.success(data=config)
|
||||
|
||||
|
||||
@router.get(
|
||||
'',
|
||||
summary='创建系统配置',
|
||||
summary='(模糊条件)分页获取所有系统参数配置',
|
||||
dependencies=[
|
||||
DependsJwtAuth,
|
||||
DependsPagination,
|
||||
],
|
||||
)
|
||||
async def get_pagination_config(
|
||||
db: CurrentSession,
|
||||
name: Annotated[str | None, Query()] = None,
|
||||
type: Annotated[str | None, Query()] = None,
|
||||
) -> ResponseModel:
|
||||
config_select = await config_service.get_select(name=name, type=type)
|
||||
page_data = await paging_data(db, config_select, GetAnyConfigListDetails)
|
||||
return response_base.success(data=page_data)
|
||||
|
||||
|
||||
@router.post(
|
||||
'',
|
||||
summary='创建系统参数配置',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def create_config(obj: CreateConfigParam) -> ResponseModel:
|
||||
async def create_config(obj: CreateAnyConfigParam) -> ResponseModel:
|
||||
await config_service.create(obj=obj)
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@router.put(
|
||||
'/{pk}',
|
||||
summary='更新系统配置',
|
||||
summary='更新系统参数配置',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:edit')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def update_config(pk: Annotated[int, Path(...)], obj: UpdateConfigParam) -> ResponseModel:
|
||||
async def update_config(pk: Annotated[int, Path(...)], obj: UpdateAnyConfigParam) -> ResponseModel:
|
||||
count = await config_service.update(pk=pk, obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
@@ -50,7 +132,7 @@ async def update_config(pk: Annotated[int, Path(...)], obj: UpdateConfigParam) -
|
||||
|
||||
@router.delete(
|
||||
'',
|
||||
summary='(批量)删除系统配置',
|
||||
summary='(批量)删除系统参数配置',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:config:del')),
|
||||
DependsRBAC,
|
||||
|
||||
@@ -31,7 +31,7 @@ class AdminSettings(BaseSettings):
|
||||
CAPTCHA_LOGIN_EXPIRE_SECONDS: int = 60 * 5 # 过期时间,单位:秒
|
||||
|
||||
# Config
|
||||
CONFIG_REDIS_KEY: str = 'fba:config'
|
||||
CONFIG_BUILT_IN_TYPES: list = ['website', 'protocol', 'policy']
|
||||
|
||||
|
||||
@lru_cache
|
||||
|
||||
@@ -2,35 +2,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import Select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy_crud_plus import CRUDPlus
|
||||
|
||||
from backend.app.admin.conf import admin_settings
|
||||
from backend.app.admin.model import Config
|
||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
||||
from backend.app.admin.schema.config import CreateAnyConfigParam, UpdateAnyConfigParam
|
||||
|
||||
|
||||
class CRUDConfig(CRUDPlus[Config]):
|
||||
async def get_one(self, db: AsyncSession) -> Config | None:
|
||||
async def get(self, db: AsyncSession, pk: int) -> Config | None:
|
||||
"""
|
||||
获取 Config
|
||||
获取系统参数配置
|
||||
|
||||
:param db:
|
||||
:param pk:
|
||||
:return:
|
||||
"""
|
||||
query = await db.execute(select(self.model).limit(1))
|
||||
return query.scalars().first()
|
||||
return await self.select_model_by_column(db, id=pk, type__not_in=admin_settings.CONFIG_BUILT_IN_TYPES)
|
||||
|
||||
async def get_all(self, db: AsyncSession) -> Sequence[Config]:
|
||||
async def get_by_type(self, db: AsyncSession, type: str) -> Sequence[Config]:
|
||||
"""
|
||||
获取所有 Config
|
||||
通过 type 获取内置系统配置
|
||||
|
||||
:param db:
|
||||
:param type:
|
||||
:return:
|
||||
"""
|
||||
return await self.select_models(db)
|
||||
return await self.select_models(db, type=type)
|
||||
|
||||
async def create(self, db: AsyncSession, obj_in: CreateConfigParam) -> None:
|
||||
async def get_by_key_and_type(self, db: AsyncSession, key: str, type: str) -> Config | None:
|
||||
"""
|
||||
通过 name 和 type 获取内置系统配置
|
||||
|
||||
:param db:
|
||||
:param key:
|
||||
:param type:
|
||||
:return:
|
||||
"""
|
||||
return await self.select_model_by_column(db, key=key, type=type)
|
||||
|
||||
async def get_by_key(self, db: AsyncSession, key: str, built_in: bool = False) -> Config | None:
|
||||
"""
|
||||
通过 key 获取系统配置参数
|
||||
|
||||
:param db:
|
||||
:param key:
|
||||
:param built_in:
|
||||
:return:
|
||||
"""
|
||||
filters = {'key': key}
|
||||
if not built_in:
|
||||
filters.update({'type__not_in': admin_settings.CONFIG_BUILT_IN_TYPES})
|
||||
return await self.select_model_by_column(db, **filters)
|
||||
|
||||
async def get_list(self, name: str = None, type: str = None) -> Select:
|
||||
"""
|
||||
获取系统参数配置列表
|
||||
|
||||
:param name:
|
||||
:param type:
|
||||
:return:
|
||||
"""
|
||||
filters = {'type__not_in': admin_settings.CONFIG_BUILT_IN_TYPES}
|
||||
if name is not None:
|
||||
filters.update(name__like=f'%{name}%')
|
||||
if type is not None:
|
||||
filters.update(type__like=f'%{type}%')
|
||||
return await self.select_order('created_time', 'desc', **filters)
|
||||
|
||||
async def create(self, db: AsyncSession, obj_in: CreateAnyConfigParam) -> None:
|
||||
"""
|
||||
创建 Config
|
||||
|
||||
@@ -40,7 +82,7 @@ class CRUDConfig(CRUDPlus[Config]):
|
||||
"""
|
||||
await self.create_model(db, obj_in)
|
||||
|
||||
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateConfigParam) -> int:
|
||||
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateAnyConfigParam) -> int:
|
||||
"""
|
||||
更新 Config
|
||||
|
||||
@@ -59,7 +101,9 @@ class CRUDConfig(CRUDPlus[Config]):
|
||||
:param pk:
|
||||
:return:
|
||||
"""
|
||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pk)
|
||||
return await self.delete_model_by_column(
|
||||
db, allow_multiple=True, id__in=pk, type__not_in=admin_settings.CONFIG_BUILT_IN_TYPES
|
||||
)
|
||||
|
||||
|
||||
config_dao: CRUDConfig = CRUDConfig(Config)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy import Boolean, String
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
@@ -13,16 +13,9 @@ class Config(Base):
|
||||
__tablename__ = 'sys_config'
|
||||
|
||||
id: Mapped[id_key] = mapped_column(init=False)
|
||||
login_title: Mapped[str] = mapped_column(String(20), default='登录 FBA', comment='登录页面标题')
|
||||
login_sub_title: Mapped[str] = mapped_column(
|
||||
String(50), default='fastapi_best_architecture', comment='登录页面子标题'
|
||||
)
|
||||
footer: Mapped[str] = mapped_column(String(50), default='FBA', comment='页脚标题')
|
||||
logo: Mapped[str] = mapped_column(LONGTEXT, default='Arco', comment='Logo')
|
||||
system_title: Mapped[str] = mapped_column(String(20), default='Arco', comment='系统标题')
|
||||
system_comment: Mapped[str] = mapped_column(
|
||||
LONGTEXT,
|
||||
default='基于 FastAPI 构建的前后端分离 RBAC 权限控制系统,采用独特的伪三层架构模型设计,'
|
||||
'内置 fastapi-admin 基本实现,并作为模板库免费开源',
|
||||
comment='系统描述',
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(20), comment='名称')
|
||||
type: Mapped[str | None] = mapped_column(String(20), server_default=None, comment='类型')
|
||||
key: Mapped[str] = mapped_column(String(50), unique=True, comment='键名')
|
||||
value: Mapped[str] = mapped_column(LONGTEXT, comment='键值')
|
||||
is_frontend: Mapped[str] = mapped_column(Boolean, default=False, comment='是否前端')
|
||||
remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注')
|
||||
|
||||
@@ -2,32 +2,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import ConfigDict, Field
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from backend.common.schema import SchemaBase
|
||||
|
||||
|
||||
class ConfigSchemaBase(SchemaBase):
|
||||
login_title: str = Field(default='登陆 FBA')
|
||||
login_sub_title: str = Field(default='fastapi_best_architecture')
|
||||
footer: str = Field(default='FBA')
|
||||
logo: str = Field(default='Arco')
|
||||
system_title: str = Field(default='Arco')
|
||||
system_comment: str = Field(
|
||||
default='基于 FastAPI 构建的前后端分离 RBAC 权限控制系统,采用独特的伪三层架构模型设计,'
|
||||
'内置 fastapi-admin 基本实现,并作为模板库免费开源'
|
||||
)
|
||||
class SaveConfigParam(SchemaBase):
|
||||
name: str
|
||||
key: str
|
||||
value: str
|
||||
|
||||
|
||||
class CreateConfigParam(ConfigSchemaBase):
|
||||
class AnyConfigSchemaBase(SchemaBase):
|
||||
name: str
|
||||
type: str | None
|
||||
key: str
|
||||
value: str
|
||||
is_frontend: bool
|
||||
remark: str | None
|
||||
|
||||
|
||||
class CreateAnyConfigParam(AnyConfigSchemaBase):
|
||||
pass
|
||||
|
||||
|
||||
class UpdateConfigParam(ConfigSchemaBase):
|
||||
class UpdateAnyConfigParam(AnyConfigSchemaBase):
|
||||
pass
|
||||
|
||||
|
||||
class GetConfigListDetails(ConfigSchemaBase):
|
||||
class GetAnyConfigListDetails(AnyConfigSchemaBase):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
|
||||
@@ -41,6 +41,9 @@ class ApiService:
|
||||
@staticmethod
|
||||
async def update(*, pk: int, obj: UpdateApiParam) -> int:
|
||||
async with async_db_session.begin() as db:
|
||||
api = await api_dao.get(db, pk)
|
||||
if not api:
|
||||
raise errors.NotFoundError(msg='接口不存在')
|
||||
count = await api_dao.update(db, pk, obj)
|
||||
return count
|
||||
|
||||
|
||||
@@ -1,56 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import Select
|
||||
|
||||
from backend.app.admin.conf import admin_settings
|
||||
from backend.app.admin.crud.crud_config import config_dao
|
||||
from backend.app.admin.model import Config
|
||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
||||
from backend.app.admin.schema.config import (
|
||||
CreateAnyConfigParam,
|
||||
SaveConfigParam,
|
||||
UpdateAnyConfigParam,
|
||||
)
|
||||
from backend.common.exception import errors
|
||||
from backend.database.db_mysql import async_db_session
|
||||
from backend.database.db_redis import redis_client
|
||||
from backend.utils.serializers import select_as_dict
|
||||
|
||||
|
||||
class ConfigService:
|
||||
@staticmethod
|
||||
async def get() -> Config | dict:
|
||||
async def get_built_in_config(type: str) -> Sequence[Config]:
|
||||
async with async_db_session() as db:
|
||||
cache_config = await redis_client.hgetall(admin_settings.CONFIG_REDIS_KEY)
|
||||
if not cache_config:
|
||||
config = await config_dao.get_one(db)
|
||||
if not config:
|
||||
raise errors.NotFoundError(msg='系统配置不存在')
|
||||
data_map = select_as_dict(config)
|
||||
del data_map['created_time']
|
||||
del data_map['updated_time']
|
||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=data_map)
|
||||
return config
|
||||
else:
|
||||
return cache_config
|
||||
return await config_dao.get_by_type(db, type)
|
||||
|
||||
@staticmethod
|
||||
async def create(*, obj: CreateConfigParam) -> None:
|
||||
async def save_built_in_config(objs: list[SaveConfigParam], type: str) -> None:
|
||||
async with async_db_session.begin() as db:
|
||||
config = await config_dao.get_one(db)
|
||||
for obj in objs:
|
||||
config = await config_dao.get_by_key_and_type(db, obj.key, type)
|
||||
if config is None:
|
||||
if await config_dao.get_by_key(db, obj.key, built_in=True):
|
||||
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
|
||||
await config_dao.create_model(db, obj, flush=True, type=type)
|
||||
else:
|
||||
await config_dao.update_model(db, config.id, obj, type=type)
|
||||
|
||||
@staticmethod
|
||||
async def get(pk) -> Config | dict:
|
||||
async with async_db_session() as db:
|
||||
config = await config_dao.get(db, pk)
|
||||
if not config:
|
||||
raise errors.NotFoundError(msg='参数配置不存在')
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
async def get_select(*, name: str = None, type: str = None) -> Select:
|
||||
return await config_dao.get_list(name=name, type=type)
|
||||
|
||||
@staticmethod
|
||||
async def create(*, obj: CreateAnyConfigParam) -> None:
|
||||
async with async_db_session.begin() as db:
|
||||
if obj.type in admin_settings.CONFIG_BUILT_IN_TYPES:
|
||||
raise errors.ForbiddenError(msg='非法类型参数')
|
||||
config = await config_dao.get_by_key(db, obj.key)
|
||||
if config:
|
||||
raise errors.ForbiddenError(msg='系统配置已存在')
|
||||
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
|
||||
await config_dao.create(db, obj)
|
||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump())
|
||||
|
||||
@staticmethod
|
||||
async def update(*, pk: int, obj: UpdateConfigParam) -> int:
|
||||
async def update(*, pk: int, obj: UpdateAnyConfigParam) -> int:
|
||||
async with async_db_session.begin() as db:
|
||||
config = await config_dao.get(db, pk)
|
||||
if not config:
|
||||
raise errors.NotFoundError(msg='参数配置不存在')
|
||||
count = await config_dao.update(db, pk, obj)
|
||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump())
|
||||
return count
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, pk: list[int]) -> int:
|
||||
async with async_db_session.begin() as db:
|
||||
configs = await config_dao.get_all(db)
|
||||
if len(configs) == 1:
|
||||
raise errors.ForbiddenError(msg='系统配置无法彻底删除')
|
||||
count = await config_dao.delete(db, pk)
|
||||
await redis_client.delete(admin_settings.CONFIG_REDIS_KEY)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user