Example #1
0
def init(loop) :
    yield from orm.create_pool(loop = loop, **configs.db)
    app = web.Application(loop = loop, debug = True, middlewares = [
        response_factory, logger_factory, auth_factory
        #过滤时,依该list的顺序来过滤;不过影响不大
        #在过滤方法中,调用yield from handler(request)相当于调用下一个过滤方法;若方法所有过滤已完成
        #则调用call方法
    ])
    init_jinja2(app, filters = dict(datetime = datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)

    logger.info('init complete!')

    #print(ip)
    r = input('run at local(1) or temp test at web(2)?')
    if r == '1' :
        _ip = '127.0.0.1'
        port = 9000
    else :
        port = 80
        _ip = ip
    srv = yield from loop.create_server(app.make_handler(), _ip, port)
    logger.info('server started at http://%s:%s...' % (_ip, port))
    return srv
Example #2
0
def init(loop):
    # 创建连接池, db参数传配置文件里的配置db
    yield from orm.create_pool(loop = loop, **configs.db)
    # 创建web应用  循环类型是消息循环
    app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, response_factory]) 
    # middlewares(中间件)设置3个中间处理函数(都是装饰器)
    # middlewares中的每个factory接受两个参数,app 和 handler(即middlewares中的下一个handler)
    # 譬如这里logger_factory的handler参数其实就是auth_factory
    # middlewares的最后一个元素的handler会通过routes查找到相应的,其实就是routes注册的对应handler
    # 这其实是装饰模式的典型体现,logger_factory, auth_factory, response_factory都是URL处理函数前(如handler.index)的装饰功能
    # 设置模板为jiaja2,并以时间为过滤器
    init_jinja2(app, filters = dict(datetime = datetime_filter))
    # 添加请求的handlers,即各请求相对应的处理函数
    add_routes(app, 'handlers') 
    # 添加静态文件所在地址
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = yield from  loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    '''app.make_hander()中对middleware的处理: (装饰器原理)
    for factory in reversed(self._middlewares):
        handler = yield from factory(app, handler)
    resp = yield from handler(request)

    middleware的代码示意:m1( m2( m3( doFoo())))
    middleware调用流程:
        req -> m1 -> m2 -> m3 -> doFoo()- 一路return 原路返回
        <- m1 <- m2 <- m2 <- resq - <-'''
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #3
0
def init(loop):
    yield from orm.create_pool(loop=loop, user='******', password='******', db='awesome')
    app = web.Application(loop=loop, middlewares=[ logger_factory, response_factory ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #4
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, "handlers")
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000)
    logging.info("server started at http://127.0.0.1:9000...")
    return srv
Example #5
0
async def init(loop):
	await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
	app = web.Application(loop=loop, middlewares=[logger_factory, response_factory])
	init_jinja2(app, filters=dict(datetime=datetime_filter))
	add_routes(app, 'handlers')
	add_static(app)
	srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
	logging.info('server started at http://127.0.0.1:9000!!!')
	return srv
Example #6
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '10.163.183.192', 9000)
    logging.info('server started at http://10.163.183.192:9000...')
    return srv
Example #7
0
async def init(loop):
    await orm.create_pool(loop=loop,**configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #8
0
def init(loop):
    yield from create_pool(loop, **configs.db)
    app = web.Application(loop=loop, middlewares = [
        logger_factory, auth_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', 9000)
    logging.info('server start at port 9000')
    return srv
Example #9
0
def init( loop ):
    yield from orm.create_pool( loop = loop, **configs.db )
    # middleware 是一种拦截器,一个URL在被某个函数处理前,可以经过一系列的middleware的处理
    # 一个middleware可以改变URL的输入和输出,甚至可以决定不继续处理而直接返回,middleware的用处就在于把通用的功能从每个URL处理函数中拿
    # 出来,集中到一个地方
    app = web.Application( loop = loop, middlewares = [ logger_factory, auth_factory, response_factory ] )
    init_jinja2( app, filters = dict( datetime = datatime_filter ) )
    add_routes( app, 'handlers' )
    add_static( app )
    srv = yield from loop.create_server( app.make_handler(), configs.server.server_ip, configs.server.server_port )
    logging.info( 'server started at http://%s:%d...' % ( configs.server.server_ip, configs.server.server_port ) )
    return srv
Example #10
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs['db'])
    app = web.Application(loop=loop,middlewares=[logger_factory, auth_factory, response_factory])
	#app.router.add_route('GET','/',index)
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    ############### app = web.Application(loop=loop)
    #################add_route(app,handlers.index)
    srv = yield from loop.create_server(app.make_handler(),'127.0.0.1',9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #11
0
async def init(loop):
    await create_pool(loop=loop, host=configs['db']['host'], port=configs['db']['port'],
                      user=configs['db']['user'],
                      password=configs['db']['password'], db=configs['db']['db'])
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #12
0
def init(loop):
    #in app.py, adding middleware、jinja2 template and auto register support:
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
    ])#create a Application instance 
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
	#app.router.add_route('GET', '/', index)#register handler in the application’s router pointing HTTP method, path and handler.
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 5000)#Create a TCP server
    logging.info('server started at http://127.0.0.1:5000...')
    return srv
Example #13
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory,data_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    ip=configs.server['ip']
    port=configs.server['port']
    srv = yield from loop.create_server(app.make_handler(),ip,port)
    logging.info('server started at http://%s:%d...' % (ip,port))
    return srv
Example #14
0
async def init(loop):
    await orm.create_pool(loop=loop, host='localhost', port=3306, user='******', password='******', db='awesome')
    # 这里middlewares就是一个大型装饰器
    # for factory in reversed(self._middlewares):
    #   handler = yield from factory(app, handler)
    # resp = yield from handler(request)
    # 这里相当于反复对handler进行装饰,reversed(self._middlewares)表示装饰时是倒序包装的,这样执行时就是按照顺序执行
    app = web.Application(loop=loop, middlewares=[logger_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #15
0
def init(loop):
    yield from connect_mysql(loop)
    app = web.Application(loop=loop,
                          middlewares=[logger_factory,
                                       data_factory,
                                       auth_factory,
                                       response_factory,
                                       ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #16
0
async def init(loop):
    # await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    # 改用读配置文件方式配置环境
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), configs.server.host,
                                   configs.server.port)
    logging.info('Server started at http://%s:%s ...' %
                 (configs.server.host, configs.server.port))
    return srv
Example #17
0
def init(loop):
    yield from orm.create_pool(
        loop=loop,
        **configs.db)  #configs.db为一个字典,函数中使用关键字参数调用configs.db中db相关的配置
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(
        app, 'handlers'
    )  #调用add_routes()参数handlers没有点后,动态导入handlers模块,并注册所有的handler,避免了一个一个URL函数的注册
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #18
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')
    app = web.Application(loop=loop,
                          middlewares=[logger_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'test')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #19
0
def init(loop):
    yield from orm.create_pool(loop=loop,
                               host='127.0.0.1',
                               port=3306,
                               user='******',
                               password='******',
                               db='pyPractice')
    app = web.Application(loop=loop,
                          middlewares=[logger_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #20
0
async def init(loop):
    await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    # 定义一个webAPP:
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 添加get方法,就是常规上网方式:
    add_routes(app, 'handlers')
    add_static(app)
    # 用异步协程方法创建一个网络服务
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    # 设置网络登陆信息
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #21
0
async def init(loop):
    await orm.create_pool(loop, user='******', password='******', db='webapp')
    #生成app对象   并且添加了个中间件 middleware是一种拦截器
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app,
                filters=dict(datetime=datetime_filter))  #加入jinja2的自主注册 过滤器添加进去
    add_routes(app, 'handlers')  #在coroweb.py  自动扫描handles模块,把符合的url添加进路由
    add_static(app)
    # app.router.add_route('GET', '/', index) #设置路由
    srv = await loop.create_server(app.make_handler(), '127.0.0.1',
                                   9000)  #创建服务器
    logging.info('server started at http://127.0.0.1:9000……')
    return srv  #为什么要返回srv?
Example #22
0
def init(loop):
    # 创建全局数据库连接池
    yield from orm.create_pool(loop = loop, host="127.0.0.1", port = 3306, user = "******", password = "******", db = "awesome")
    # 创建web应用,
    app = web.Application(loop = loop, middlewares=[logger_factory, response_factory]) # 创建一个循环类型是消息循环的web应用对象
    # 设置模板为jiaja2, 并以时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册所有url处理函数
    add_routes(app, "handlers")
    # 将当前目录下的static目录将如app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000)
    logging.info("server started at http://127.0.0.1:9000")
    return srv
Example #23
0
def init(loop):
    # 创建全局数据库连接池
    yield from orm.create_pool(loop = loop, host="127.0.0.1", port = 3307, user = "******", password = "******", db = "awesome")
    # 创建web应用,
    app = web.Application(loop = loop, middlewares=[logger_factory, auth_factory, response_factory]) # 创建一个循环类型是消息循环的web应用对象
    # 设置模板为jiaja2, 并以时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册所有url处理函数
    add_routes(app, "handlers")
    # 将当前目录下的static目录将如app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000)
    logging.info("server started at http://127.0.0.1:9000")
    return srv
Example #24
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)  #创建数据库连接池,传入数据库的配置信息
    app = web.Application(  #通过webapp构架函数构建web App
        loop=loop,  #loop
        middlewares=[logger_factory, auth_factory,
                     response_factory])  #传入中间操作函数
    #middleware的用处就在于把通用的功能从每个URL处理函数中拿出来,集中放到一个地方
    init_jinja2(
        app, filters=dict(datetime=datetime_filter))  #初始化jinja2模板,传入app,以及过滤器
    add_routes(app, 'handlers')  #注册app的路由处理
    add_static(app)  #添加app的静态文件路径
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1',
                                        9000)  #创建服务
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #25
0
def init(loop):
    yield from orm.create_pool(loop=loop,
                               host="192.168.1.111",
                               user="******",
                               password="******",
                               db="zun_qy_pay")
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #26
0
async def init(loop):
    # await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)

    runner = web.AppRunner(app)
    await runner.setup()
    srv = await loop.create_server(runner.server, host='127.0.0.1', port=9000)
    logging.info('Server started at http://127.0.0.1:9000...')
    return srv
Example #27
0
async def init(loop):
    # 创建数据库连接池
    await orm.create_pool(loop=loop, host='10.104.20.123', port=3306, user='******', password='******', db='awesome')
    # 创建app对象,同时传入上文定义的拦截器middlewares
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
    ])
    # 初始化jinja2模板,并传入时间过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 下面这两个函数在coroweb模块中
    add_routes(app, 'handlers')  # handlers指的是handlers模块也就是handlers.py
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #28
0
async def init(loop):
    # await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='web_app')
    # 可用**configs.db,pylint无法检查会报error
    await orm.create_pool(loop=loop, **configs['db'])
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, response_factory, auth_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # add_routes(app, 'ig_test_view')
    add_routes(app, 'handlers')
    add_static(app)
    # srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    # logging.info('server started at http://127.0.0.1:9000...')
    # return srv
    return app
Example #29
0
async def init(loop):
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(
        loop=loop, middlewares=[logger_factory, ip_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    scan_handles_dir(
        app,
        os.path.join(os.path.dirname(os.path.abspath(__file__)), 'handlers'))
    add_static(
        app,
        os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources'),
        'resources')
    add_static(app, configs.pictures_path, 'pictures')
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #30
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
#        logger_factory, auth_factory, response_factory, data_factory 
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'userregAlogin')
#用户添加访问逻辑
    add_routes(app, 'handlers')
    add_routes(app, 'errors')
    add_routes(app, 'admins')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', 8888)
    logging.info('服务在http://192.168.1.53:8888开始运行...')
    return srv
Example #31
0
def init(loop):
    yield from orm.create_pool(loop=loop, **configs.db)
    #创建Web App,循环类型为消息循环传入拦截器
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
    ])
    #初始化jinja2模板
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    #注册URL处理函数
    add_routes(app, 'handlers')
    #添加静态文件
    add_static(app)
    #创建TCP服务器
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)         #创建TCP服务
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #32
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='mysql')
    # middleware是一种拦截器,一个URL在被某个函数处理前,可以经过一系列的middleware的处理
    app = web.Application(loop=loop,
                          middlewares=[logger_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #33
0
def init(loop):
    yield from create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory,auth_factory, response_factory
    ])
    init_jinja2(app,filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    # 获取本机机器名
    myname = socket.gethostname()
    # 获取本机 IP 地址
    # myaddr = socket.gethostbyname(myname)
    myaddr = socket.gethostbyname(socket.gethostname())
    srv = yield from loop.create_server(app.make_handler(),'127.0.0.1',9000)
    logging.info('server started at http://%s:9000...' % '127.0.0.1')
    return srv
Example #34
0
def init(loop):
    yield from orm.create_pool(loop=loop,
                               host=os.getenv('MYSQL_PORT_3306_TCP_ADDR'),
                               port=int(os.getenv('MYSQL_PORT_3306_TCP_PORT')),
                               user=os.getenv('MYSQL_USERNAME'),
                               password=os.getenv('MYSQL_PASSWORD'),
                               db=os.getenv('MYSQL_INSTANCE_NAME'))
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', 9000)
    logging.info('server started at http://0.0.0.0:9000...')
    return srv
Example #35
0
def init(loop):
    yield from orm.create_pool(loop = loop, host = '127.0.0.1', port = 3306, user = '******', password = '******', database = 'excodout')
    #梗#了解下 middlewares
    app = web.Application(loop = loop,middlewares = [logger_factory, auth_factory, response_factory])
    init_jinjia2(app, filters = dict(datetime = datetime_filter))
    #add_routes第二个参数是模块名,把模块中所有函数处理后添加给app
    #ipdb.set_trace()



    add_routes(app, 'handlers')
    add_static(app)
    #app.router.add_route('GET', '/', index)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9999)
    loggerII.info('Server started at http://127.0.0.1:9999...')
    return srv
Example #36
0
def init(loop):
    '''
    初始化: 创建连接池, 创建主程序, 初始化模版,
           添加路径处理, 添加静态资源处理, 创建连接
    '''
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        # 中间件处理函数
        logger_factory, auth_factory, data_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    server = yield from loop.create_server(app.make_handler(), '127.0.0.1', 3000)
    logging.info('server started at http://127.0.0.1:3000...')
    return server
Example #37
0
async def init(loop):
    await orm.create_pool(loop=loop, **configs.db)
    # middlewares(中间件)设置3个中间处理函数(都是装饰器)
    # middlewares中的每个factory接受两个参数,app 和 handler(即middlewares中的下一个handler)
    # 譬如这里logger_factory的handler参数其实就是auth_factory
    # middlewares的最后一个元素的handler会通过routes查找到相应的,就是routes注册的对应handler处理函数
    # 这是装饰模式的体现,logger_factory, auth_factory, response_factory都是URL处理函数前(如handler.index)的装饰功能
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')  # 添加URL处理函数
    add_static(app)  # 添加CSS等静态文件路径
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #38
0
async def init(loop):
    await orm.create_pool(loop=loop, **configs.db)
    # 从aiohttp模块中调用WSGI接口方法,将客户端请求抛给web应用程序去处理,并启动拦截器
    # app是一个请求实例
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, response_factory, auth_factory])
    # 注册模板
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册url处理函数,注册后得到响应体response
    add_routes(app, 'handlers')
    # 注册静态文件
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #39
0
async def init(loop):  # 定义init函数,标记为协程,传入loop协程参数
    """服务器运行程序:创建web实例程序,该实例程序绑定路由和处理函数,运行服务器,监听端口请求,送到路由处理"""
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(
        loop=loop,
        middlewares=(logger_factory, auth_factory,
                     response_factory))  # 创建一个web服务器实例,用于处理URL,HTTP协议
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(
        app, 'handlers'
    )  # 将URL注册进route,将URL和index处理函数绑定,当浏览器敲击URL时,返回处理函数的内容,也就是返回一个HTTP响应
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1',
                                   9000)  # 创建一个监听服务
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #40
0
async def init(loop):
    await orm.create_poop(loop=loop,
                          host='192.168.1.104',
                          port=3306,
                          user='******',
                          password='******',
                          db='liaoxuefeng')
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #41
0
async def init(loop):
	# 创建数据库连接池,之后用db参数传配置文件里的配置db
	await orm.create_pool(loop, **configs.db)
	# middlewares设置两个中间处理函数
    # middlewares中的每个factory接受两个参数,app 和 handler(即middlewares中得下一个handler)
    # 譬如这里logger_factory的handler参数其实就是response_factory()
    # middlewares的最后一个元素的Handler会通过routes查找到相应的,其实就是routes注册的对应handler
	app = web.Application(loop=loop, middlewares=[
		logger_factory, auth_factory, response_factory
	])
	init_jinja2(app, filters=dict(datetime=datetime_filter))
	add_routes(app, 'handlers')
	add_static(app)
	srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
	logging.info('sever started at http://127.0.0.1//9000...')
	return srv
Example #42
0
async def init(loop):
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(middlewares=[
        logger_factory, auth_factory, response_factory
    ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))

    add_routes(app, 'handlers')
    add_static(app)

    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, 'localhost', 9000)
    srv = await site.start()
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #43
0
async def init(loop):
    await orm.create_pool(user='******',
                          password='******',
                          db='awesome',
                          loop=loop)
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    add_routes(app, 'handlers_user')
    add_routes(app, 'handlers_blog')
    add_static(app)
    runner = web.AppRunner(app)
    await runner.setup()
    srv = await loop.create_server(runner.server, '127.0.0.1', 9999)
    logging.info('server started at http://127.0.0.1:9999...')
    return srv
Example #44
0
def init(loop):
    yield from orm.create_pool(loop=loop,
                               host='127.0.0.1',
                               port=3306,
                               user='******',
                               password='******',
                               db='awesome')
    app = web.Application(loop=loop,
                          middlewares=[logger_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    # app.router.add_route('GET', '/', index)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000')
    return srv
Example #45
0
def init(loop):
    # yield from create_pool(loop=loop, host='localhost', port=3306, user='******', password='******', db='awesome')
    yield from create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, data_factory, response_factory])
    app.on_shutdown.append(on_close)
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    handler = app.make_handler()
    srv = yield from loop.create_server(handler, '127.0.0.1', 9000)
    logging.info('Server start at http://127.0.0.1:9000...')
    rs = dict()
    rs['app'] = app
    rs['srv'] = srv
    rs['handler'] = handler
    return rs
Example #46
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host=configs.db.host,
                          port=configs.db.port,
                          user=configs.db.user,
                          password=configs.db.password,
                          db=configs.db.db)
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '188.131.139.152', 9000)
    logging.info('server started at http://188.131.139.152:9000')
    return srv
Example #47
0
async def init(loop):
    #创建数据库连接池
    await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    #middlewares翻译过来是中间件,factory是工厂,把request和response送进厂里改造一番再出来。好吧,是我瞎猜的,就当没看见。
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
    ])
    #初始化jinja2模板,并传入时间过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    #下面这两个函数在coroweb模块中,这里我就不注释了
    add_routes(app, 'handlers')#handlers指的是handlers模块也就是handlers.py
    add_static(app)
    #监听127.0.0.1这个IP的9000端口的访问请求
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #48
0
def init(loop):
    """
    Initiation function for the whole app.
    Using Coroutine at this point.
    """
    
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
        ])    #Passing the main loop and middlewares to app.
    init_jinja2(app, filters=dict(datetime=datetime_filter))    #Initialize jinja2
    add_routes(app, 'handlers')        #When being requested the root folder by GET method, call index()
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', 8080)
    logging.info('Server started at http://127.0.0.1:8080...')
    return srv
Example #49
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host=configs['db']['host'],
                          port=configs['db']['port'],
                          user=configs['db']['user'],
                          password=configs['db']['password'],
                          db=configs['db']['database'])
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #50
0
async def init(loop):
    #await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    await orm.create_pool(loop=loop,
                          **configs['database'])  # 导入config配置文件,连接数据库
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # add_routes导入模块名,调用add_route,内部app.router.add_route创建RequestHandler实例
    add_routes(app, 'handlers')  # module_name为独立模块名,或带.的模块名的子模块
    add_static(app)
    srv = await loop.create_server(
        app.make_handler(), '127.0.0.1', 9000
    )  # 当向服务器发出请求时,会进行中间层middlewares函数调用,并调用RequestHandler实例__call__函数,进行处理
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #51
0
def init(loop1):
    yield from orm.create_pool(loop=loop1,
                               host='127.0.0.1',
                               port=3306,
                               user='******',
                               password='******',
                               db='awesome')
    app = web.Application(
        loop=loop1,
        middlewares=[logger_factory, anth_factory, response_factory])
    init_jinja2(app, filters={'datetime': datetime_filter})
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop1.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #52
0
def init(loop):
    configs_host = configs['db']['host']
    configs_user = configs['db']['user']
    configs_port = configs['db']['port']
    configs_password = configs['db']['password']
    configs_db = configs['db']['db']
    yield from orm.create_pool(loop=loop, host=configs_host, port=configs_port, user=configs_user, password=configs_password, db=configs_db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, response_factory
        ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(),'127.0.0.1',9000)
    logging.info('Server started at http://127.0.0.1:9000...')
    return srv
Example #53
0
def init(loop):
	yield from orm.create_pool(loop = loop,**configs.db)
	app = web.Application(loop = loop,
		middlewares=[
			logger_factory,
			auth_factory,
			response_factory])
	init_jinja2(app,filters=dict(datetime=datetime_filter))
	add_routes(app, _USER_HANDLER)
	add_routes(app, _BLOG_HANDLER)
	add_routes(app, _COMMENT_HANDLER)
	add_routes(app, _CATEGORY_HANDLER)
	add_static(app)
	srv = yield from loop.create_server(app.make_handler(),configs.server.host,configs.server.port)
	logging.info('Server started at %s:%s...' %(configs.server.host,configs.server.port))
	return srv
async def init(loop):
	# create a global database connection pool
	await orm.create_pool(loop=loop, **configs.db)
	# create an Application instance
	app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, response_factory])
	# init jinja2
	init_jinja2(app, filters=dict(datetime=datetime_filter))
	# add url handler func, 'handlers' is the module name
	add_routes(app, 'handlers')
	# add path of static files
	add_static(app)
	# start server
	srv_host = '127.0.0.1'
	srv_port = 9000
	srv = await loop.create_server(app.make_handler(), host=srv_host, port=srv_port)
	logging.info('server started at http://%s:%d ........' % (srv_host, srv_port)) 
	return srv
Example #55
0
def init(loop):
    yield from orm.create_pool(
        loop=loop,
        # host="115.159.219.141",
        host="localhost",
        port=3306,
        user="******",
        password="******",
        db="web",
        autocommit=True
    )
    app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filter=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_static(app)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Example #56
0
def init(loop):
    # 往 web对象中加入消息循环,生成一个支持异步IO的对象
    yield from orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop, middlewares=[
        logger_factory, auth_factory, response_factory
    ])    
    # filters : {'datetime': <function datetime_filter at 0x0000000002062EA0>}
    init_jinja2(app, filters=dict(datetime=datetime_filter))

    # handlers 为URL处理函数(handlers.py)
    add_routes(app, 'handlers')

    add_static(app)
    # 监听127.0.0.1:9000 端口的http请求(利用asyncio创建TCP服务)
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    # 把监听http请求的这个协程返回给loop - 用以持续监听http请求
    return srv
Example #57
0
def init(loop):
	# yield from orm.create_pool(loop=loop, host='12.7.0.0.1', prot=3306, user='******',password='******',db='awesome')
	yield from orm.create_pool(loop=loop,**configs.db)

	# app = web.Application(loop=loop)
	app = web.Application(loop=loop,middlewares=[
		logger_factory,auth_factory,response_factory
		])
	init_jinja2(app,filters=dict(datetime=datetime_filter))
	
	add_routes(app,'handlers')

	add_static(app)

	# app.router.add_route('GET','/',index)
	srv = yield from loop.create_server(app.make_handler(),'127.0.0.1',9000)
	logging.info('server startted')
	return srv
async def init(loop):
    # 创建全局数据库连接池
    await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    # 创建web应用
    app = web.Application(loop = loop, middlewares=[
        logger_factory, response_factory
    ])

    #创建一个Application实例
    # 设置模板为jinja2,并用时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册所有url处理函数
    add_routes(app, 'handlers')
    # 将当前目录下的static目录加入app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')#发送日志
    return srv
Example #59
0
def init(loop):
	#创建数据库连接池,db参数传配置文件里的配置db
	yield from orm.create_pool(loop=loop, **configs.db)
	#middlewares设置两个中间处理函数
	#middlewares中的每个factory接受两个参数,app 和 handler(即middlewares中得下一个handler)
	#譬如这里logger_factory的handler参数其实就是response_factory()
	#middlewares的最后一个元素的Handler会通过routes查找到相应的,其实就是routes注册的对应handler
	app = web.Application(loop=loop, middlewares=[
		logger_factory, auth_factory, response_factory
	])
	#初始化jinja2模板
	init_jinja2(app, filters=dict(datetime=datetime_filter))
	#添加请求的handlers,即各请求相对应的处理函数
	add_routes(app, 'handlers')
	#添加静态文件所在地址
	add_static(app)
	#启动
	srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
	logging.info('server started at http://127.0.0.1:9000...')
	return srv