def run_server(path, container, port=None): """ 创建一个简单的server用来测试 :param path: :param port: :return: """ try: import asyncio from apistellar import Application from uvicorn.main import Server, Config class TestServer(Server): # 子线程无法注册信号 def install_signal_handlers(self): pass except ImportError: warnings.warn("Python3.6+: apistellar required. ") raise app = Application("test", current_dir=path) port = port or free_port() server = TestServer(Config(app, host="127.0.0.1", port=port, loop="asyncio")) container.append(None) container.append(server) server.run()
def run_server(path, container, port=None): """ 创建一个简单的server用来测试 :param path: :param port: :return: """ try: import asyncio from apistellar import Application from uvicorn.main import Server, HttpToolsProtocol except ImportError: warnings.warn("Python3.6: apistellar required. ") raise loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) app = Application("test", current_dir=path) port = port or free_port() server = Server(app, "127.0.0.1", port, loop, None, HttpToolsProtocol) loop.run_until_complete(server.create_server()) if server.server is not None: container.append(port) container.append(loop) container.append(server.server) loop.create_task(server.tick()) loop.run_forever()
import os import logging from uvicorn import run from apistellar import Application from whitenoise import WhiteNoise # 静态文件每次请求重新查找 WhiteNoise.autorefresh = True app_name = "blog" logging.basicConfig(level=logging.DEBUG) app = Application(app_name, static_dir="static", template_dir="templates", current_dir=os.path.dirname(os.path.abspath(__file__))) if __name__ == "__main__": run(app)
def test_app_no_route(): Application("test")
import os import logging from uvicorn import run from apistellar import Application from whitenoise import WhiteNoise # 静态文件每次请求重新查找 WhiteNoise.autorefresh = True app_name = "blog" logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(name)s] %(levelname)s: %(message)s') app = Application(app_name, debug=False, current_dir=os.path.dirname(os.path.abspath(__file__))) if __name__ == "__main__": run(app)