mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-10-12 12:20:27 +00:00
* feat: add postgresql db supports * change: change mysql conn str create way * fix: Modify the default alembic migration file to meet multi-database support * Update settings and lint * update models * Simplify database config * Simplify the get db method * Update create db url * Updated model type adaptation * Update sql scripts * Fix models type * Adaptation to postgresql code generation * Update README.md * Fix alembic file template * Update docker scripts
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from fast_captcha import img_captcha
|
|
from fastapi import APIRouter, Depends, Request
|
|
from fastapi_limiter.depends import RateLimiter
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from backend.app.admin.conf import admin_settings
|
|
from backend.common.response.response_schema import ResponseModel, response_base
|
|
from backend.database.redis import redis_client
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get(
|
|
'',
|
|
summary='获取登录验证码',
|
|
dependencies=[Depends(RateLimiter(times=5, seconds=10))],
|
|
)
|
|
async def get_captcha(request: Request) -> ResponseModel:
|
|
"""
|
|
此接口可能存在性能损耗,尽管是异步接口,但是验证码生成是IO密集型任务,使用线程池尽量减少性能损耗
|
|
"""
|
|
img_type: str = 'base64'
|
|
img, code = await run_in_threadpool(img_captcha, img_byte=img_type)
|
|
ip = request.state.ip
|
|
await redis_client.set(
|
|
f'{admin_settings.CAPTCHA_LOGIN_REDIS_PREFIX}:{ip}', code, ex=admin_settings.CAPTCHA_LOGIN_EXPIRE_SECONDS
|
|
)
|
|
return response_base.success(data={'image_type': img_type, 'image': img})
|