async def init(loop, init_port=6000): await init_db(loop) app = web.Application(loop=loop, middlewares=[logger_factory, response_factory]) add_routes(app, module_list=module_list) add_static(app) srv = await loop.create_server(app.make_handler(), host, init_port) info('server started at http://%s:%s...' % (host, init_port)) return srv
def to_response(r, m="", method="GET"): if isinstance(r, dict): resp = web.Response(body=json.dumps( r, ensure_ascii=False, default=__default).encode('utf-8')) content_type = "application/json;charset=utf-8" elif isinstance(r, bytes): resp = web.Response(body=r) content_type = 'application/octet-stream' else: if m: resp = web.Response(status=r, text=str(m)) else: resp = web.Response(body=str(r).encode('utf-8')) content_type = 'text/html;charset=utf-8' info("to_response data is: %s" % str(r)) resp.content_type = content_type resp.headers["X-Custom-Server-Header"] = "Custom data" resp.headers["ACCESS-CONTROL-ALLOW-ORIGIN"] = "*" resp.headers["ACCESS-CONTROL-ALLOW-METHODS"] = method resp.headers["ACCESS-CONTROL-MAX-AGE"] = "3600" resp.headers[ "ACCESS-CONTROL-ALLOW-HEADERS"] = "X-Client-Header, Referer, Accept, Origin, User-Agent, X-Requested-With, Content-Type, token" return resp
def log(sql, args=()): info('SQL: %s' % sql)