diff --git a/ahserver/globalEnv.py b/ahserver/globalEnv.py index 08c4f86..3dced51 100644 --- a/ahserver/globalEnv.py +++ b/ahserver/globalEnv.py @@ -33,8 +33,7 @@ from appPublic.httpclient import HttpClient from sqlor.dbpools import DBPools,runSQL,runSQLPaging from sqlor.filter import DBFilter, default_filterjson -from sqlor.crud import CRUD - +from aiohttp.web import StreamResponse from .xlsxData import XLSXData from .uriop import URIOp @@ -49,6 +48,22 @@ def basic_auth_headers(user, passwd): "Authorization":ba.encode() } +async def stream_response(request, async_data_generator, content_type='text/html'): + res = StreamResponse() + res.content_type = content_type + res.set_status(200) + await res.prepare(request) + async for d in async_data_generator(): + if isinstance(d, bytes): + await res.write(d) + elif isinstance(d, str): + await res.write(d.encode('utf-8')) + else: + d = json.dumps(d) + await res.write(d.encode('utf-8')) + await res.write_eof() + return res + def data2xlsx(rows,headers=None): wb = Workbook() ws = wb.active @@ -206,7 +221,6 @@ def initEnv(): g.data2xlsx = data2xlsx g.xlsxdata = XLSXData g.openfile = openfile - g.CRUD = CRUD g.DBPools = DBPools g.DBFilter = DBFilter g.default_filterjson = default_filterjson @@ -227,6 +241,7 @@ def initEnv(): g.basic_auth_headers = basic_auth_headers g.HttpClient = HttpClient g.rfexe = RegisterFunction().exe + g.stream_response = stream_response def set_builtins(): all_builtins = [ i for i in dir(builtins) if not i.startswith('_')]