async def main(): # 初始化 jade 页面 from common.build import JadeWork templete = JadeWork.build("src", "src_html") # 启动 web 服务 from handlers import ApiHandler, TemplateHandler from handlers import endecode, downloader app = aiohttp.web.Application() ApiHandler.add_handlers({ "/api/encode": endecode.encode, "/api/decode": endecode.decode, "/api/download_url": downloader.download_url, "/api/get_download_status": downloader.get_download_status, }) app.router.add_static("/static/", path="./static/", name="static") # 静态资源 js css img (下载形式) app.router.add_route("POST", "/api/{match:.*}", ApiHandler.do) # API 接口 app.router.add_route("GET", "/{match:.*}", TemplateHandler.wrap("src", templete, index="index")) # html 页面 await asyncio.gather( aiohttp.web._run_app(app, port=7001), # 启动web服务,监听端口 )
async def main(): # 初始化数据库 import config from common import mongodb await mongodb.init_connection(config.mongodb_settings) # 初始化 jade 页面 from common.build import JadeWork templete = JadeWork.build("src", "src_html") # 启动 web 服务 from handlers import ApiHandler, TemplateHandler from handlers import test, test_queue from handlers import _crontab app = aiohttp.web.Application() ApiHandler.add_handlers({ "/api/test": test.do, "/api/test_queue": test_queue.do, }) app.router.add_static("/static/", path="./static/", name="static") # 静态资源 js css img (下载形式) app.router.add_route("POST", "/api/{match:.*}", ApiHandler.do) # API 接口 app.router.add_route("GET", "/{match:.*}", TemplateHandler.wrap("src", templete, index="index")) # html 页面 await asyncio.gather( _crontab.init(), test_queue.worker(), # 后台处理的 worker (通过 queue 传递请求) aiohttp.web._run_app(app, port=7001), # 启动web服务,监听端口 )
def make_app(): from tornado.log import enable_pretty_logging enable_pretty_logging() from handlers import StaticHandler, TemplateHandler, ApiHandler, main from build import build build("templates_jade", "templates") from common import mongodb from conf import config yield mongodb.init(config.MONGODB) settings = { "template_path": "templates", "autoreload": True, "debug": False, } ApiHandler.update_url_handlers({ "/api/200": main.do_return_ok, "/api/500": main.do_return_error, "/api/get_sequence_name": main.do_return_sequence_name, }) app = tornado.web.Application( [ (r"/main", main.MainHandler), # (.*) will pass the `request path` into the handler's get/post function, as an argument # ? why don't they just use `self.request.path` # Tornado use mimetypes.guess_type to obtain Content-Type so you'd better name them properly (r"/css/(.*)", StaticHandler, { "path": "static/css" }), (r"/js/(.*)", StaticHandler, { "path": "static/js" }), (r"/img/(.*)", StaticHandler, { "path": "static/img" }), (r"/api/.*", ApiHandler), # {"root": "templates"} will pass into the handler's initialize function (r"/(.*)", TemplateHandler, { "root": "templates", "default_filename": "index" }), ], **settings) app.listen(config.SYSTEM["listening_port"]) print("listening %s" % config.SYSTEM["listening_port"])
async def main(): # 初始化数据库 import config from common import mongodb await mongodb.init_connection(config.mongodb_settings) # 初始化 jade 页面 from common.build import JadeWork JadeWork.set_variables(config.site_variables) templete = JadeWork.build("src", "src_html") # 启动 web 服务 from handlers import ApiHandler, TemplateHandler, ContentHandler, Authorize from handlers import note, user app = aiohttp.web.Application() ApiHandler.add_handlers({ "/api/note/query": note.query, "/api/note/add": note.add, "/api/note/edit": note.edit, "/api/note/remove": note.remove, "/api/note/read_rank": note.read_rank, "/api/note/note_list": note.note_list, "/api/user/register": user.register, "/api/user/query": user.query, }) Authorize.init(config.security_settings, [ r"/api/note/add", r"/api/note/edit", r"/api/note/remove", r"/api/user/register", # 不开放注册 r"/note_edit", r"/note_add", ]) app.router.add_static("/static/", path="./static/", name="static") # 静态资源 js css img (下载形式) app.router.add_route("POST", "/login", user.LoginHandler.do) # LoginHandler 接口 app.router.add_route("POST", "/api/{match:.*}", ApiHandler.wrap()) # API 接口 app.router.add_route("GET", "/{match:.*}", TemplateHandler.wrap("src", templete, index="index")) # html 静态页面 await asyncio.gather( aiohttp.web._run_app(app, port=config.system_settings["listening_port"]), # 启动web服务,监听端口 )
def __init__( self, default_host: str = None, transforms: List[Type['OutputTransform']] = None, db_pool: Dict[str, Awaitable] = None, **settings: Any ) -> None: if isinstance(db_pool, dict): for name, pool in db_pool.items(): setattr(ApiHandler, name, pool) _handlers = [] for handler in ApiHandler.__subclasses__(): if hasattr(handler, 'route'): _handlers.append((handler.route, handler)) del handler.route super().__init__(_handlers, default_host, transforms, **settings)
from flask import Flask from handlers import DatabaseHandler, ApiHandler from flask_cors import CORS # set up the application app = Flask(__name__, static_url_path='/static') app.secret_key = b'N\x8e\xd1\xed\xadHE\xe8\xcbn\xa3o|\x10j\xf9' app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@localhost/digi' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Enable CORS CORS(app) # Set up Restfullness :> ApiHandler.set_api(app) # Set up database DatabaseHandler.set_database(app) DatabaseHandler.create_all() if __name__ == '__main__': app.run() def run(): app.run()
def make_app(): from handlers import StaticHandler, TemplateHandler, ApiHandler from build import build build("templates_jade", "templates") from common import mongodb from conf import config yield mongodb.init(config.MONGODB) from modules import init_schedules yield init_schedules.init() settings = { "template_path": "templates", "autoreload": False, # "autoreload": True, "debug": False, } from handlers import main, watchdog from handlers import rent_douban, jingdong, kaola, yanxuan ApiHandler.update_url_handlers({ "/api/200": main.do_return_ok, "/api/500": main.do_return_error, "/api/get_sequence_name": main.do_return_sequence_name, "/api/watchdog/add": watchdog.add, "/api/jingdong/task_add": jingdong.task_add, "/api/jingdong/task_list": jingdong.task_list, "/api/jingdong/task_update_status": jingdong.task_update_status, "/api/jingdong/update_good_price": jingdong.update_good_price, "/api/jingdong/remove_item": jingdong.remove_item, "/api/jingdong/price_walk": jingdong.price_walk, "/api/kaola/task_add": kaola.task_add, "/api/kaola/task_list": kaola.task_list, "/api/kaola/task_update_status": kaola.task_update_status, "/api/kaola/update_good_price": kaola.update_good_price, "/api/kaola/remove_item": kaola.remove_item, "/api/yanxuan/task_add": yanxuan.task_add, "/api/yanxuan/task_list": yanxuan.task_list, "/api/yanxuan/task_update_status": yanxuan.task_update_status, "/api/yanxuan/update_good_price": yanxuan.update_good_price, "/api/yanxuan/remove_item": yanxuan.remove_item, "/api/rent/task_list": rent_douban.task_list, "/api/rent/query_filter": rent_douban.query_filter, "/api/rent/change_filter": rent_douban.change_filter, }) app = tornado.web.Application( [ (r"/main", main.MainHandler), # (.*) will pass the `request path` into the handler's get/post function, as an argument # ? why don't they just use `self.request.path` # Tornado use mimetypes.guess_type to obtain Content-Type so you'd better name them properly (r"/css/(.*)", StaticHandler, { "path": "static/css" }), (r"/js/(.*)", StaticHandler, { "path": "static/js" }), (r"/img/(.*)", StaticHandler, { "path": "static/img" }), (r"/api/.*", ApiHandler), # {"root": "templates"} will pass into the handler's initialize function (r"/(.*)", TemplateHandler, { "root": "templates", "default_filename": "index" }), ], **settings) app.listen(config.SYSTEM["listening_port"]) print("listening %s" % config.SYSTEM["listening_port"])