Add sync to async decorator support (#96)

* Add sync to async decorator support

* Update ASyncTranslator to asgiref
This commit is contained in:
Wu Clan
2023-06-06 20:13:33 +08:00
committed by GitHub
parent 9e833db5f5
commit abcc9d2308
20 changed files with 115 additions and 92 deletions

View File

@ -2,13 +2,15 @@
# -*- coding: utf-8 -*-
import httpx
from XdbSearchIP.xdbSearcher import XdbSearcher
from asgiref.sync import sync_to_async
from httpx import HTTPError
from fastapi import Request
from backend.app.core.path_conf import IP2REGION_XDB
async def get_request_ip(request: Request) -> str:
@sync_to_async
def get_request_ip(request: Request) -> str:
"""获取请求的 ip 地址"""
real = request.headers.get('X-Real-IP')
if real:
@ -46,6 +48,7 @@ async def get_location_online(ipaddr: str, user_agent: str) -> str:
return city or '未知' if city != '' else '未知'
@sync_to_async
def get_location_offline(ipaddr: str) -> str:
"""
离线获取 ip 地址属地无法保证准确率100%可用