def main(argv): logging.basicConfig(level=logging.DEBUG) app = init_app(argv) config = get_config(argv) web.run_app(app, host=config['host'], port=config['port'])
async def init_app(argv=None): app = web.Application() app['config'] = get_config(argv) aiohttp_jinja2.setup( app, loader=jinja2.PackageLoader('aiohttpdemo_polls', 'templates') ) # create db connection on startup, shutdown on exit app.on_startup.append(init_pg) app.on_cleanup.append(close_pg) # setup views and routes setup_routes(app) setup_middlewares(app) return app
async def init_app(argv=None): # 申请一个web应用,我们的网页的相关信息将要和它绑定 app = web.Application() # 获取有关设置 app['config'] = get_config(argv) # 设置渲染,也就是html相关的文件所在地 # setup Jinja2 template renderer aiohttp_jinja2.setup(app, loader=jinja2.PackageLoader('aiohttpdemo_polls', 'templates')) # startup 建立数据库连接, 退出后则关闭 app.on_startup.append(init_pg) app.on_cleanup.append(close_pg) # 设置视图和路由 setup_routes(app) # 设置中间件 setup_middlewares(app) return app
from aiohttpdemo_polls.settings import BASE_DIR, get_config # 建立连接的参数,可以学习这种写法 DSN = "postgresql://{user}:{password}@{host}:{port}/{database}" ADMIN_DB_URL = DSN.format(user='******', password='******', database='postgres', host='localhost', port=5432) # 管理员 admin_engine = create_engine(ADMIN_DB_URL, isolation_level='AUTOCOMMIT') USER_CONFIG_PATH = BASE_DIR / 'config' / 'polls.yaml' USER_CONFIG = get_config(['-c', USER_CONFIG_PATH.as_posix()]) USER_DB_URL = DSN.format(**USER_CONFIG['postgres']) user_engine = create_engine(USER_DB_URL) # 测试 TEST_CONFIG_PATH = BASE_DIR / 'config' / 'polls_test.yaml' TEST_CONFIG = get_config(['-c', TEST_CONFIG_PATH.as_posix()]) TEST_DB_URL = DSN.format(**TEST_CONFIG['postgres']) test_engine = create_engine(TEST_DB_URL) # 初始化数据库 # 包括建库,添加用户,设置密码,以及授权 def setup_db(config): db_name = config['database']
from aiohttpdemo_polls.db import question, choice from aiohttpdemo_polls.settings import BASE_DIR, get_config DSN = "postgresql://{user}:{password}@{host}:{port}/{database}" ADMIN_DB_URL = DSN.format(user='******', password='******', database='postgres', host='localhost', port=5432) admin_engine = create_engine(ADMIN_DB_URL, isolation_level='AUTOCOMMIT') USER_CONFIG_PATH = BASE_DIR / 'config' / 'polls.yaml' USER_CONFIG = get_config([ '-c', USER_CONFIG_PATH.as_posix() ]) # Return a str representation of path with forward slashes (/). USER_DB_URL = DSN.format(**USER_CONFIG['postgres']) user_engine = create_engine(USER_DB_URL) TEST_CONFIG_PATH = BASE_DIR / 'config' / 'polls_test.yaml' TEST_CONFIG = get_config(['-c', TEST_CONFIG_PATH.as_posix()]) TEST_DB_URL = DSN.format(**TEST_CONFIG['postgres']) test_engine = create_engine(TEST_DB_URL) def setup_db(config): db_name = config['database'] db_user = config['user'] db_password = config['password']
def db(): test_config = get_config(['-c', TEST_CONFIG_PATH.as_posix()]) setup_db(test_config['postgres']) yield teardown_db(test_config['postgres'])
from aiohttpdemo_polls.db import question, choice from aiohttpdemo_polls.settings import BASE_DIR, get_config DSN = "postgresql://{user}:{password}@{host}:{port}/{database}" ADMIN_DB_URL = DSN.format( user='******', password='******', database='postgres', host='localhost', port=5432 ) admin_engine = create_engine(ADMIN_DB_URL, isolation_level='AUTOCOMMIT') USER_CONFIG_PATH = BASE_DIR / 'config' / 'polls.yaml' USER_CONFIG = get_config() USER_DB_URL = DSN.format(**USER_CONFIG['postgres']) user_engine = create_engine(USER_DB_URL) TEST_CONFIG_PATH = BASE_DIR / 'config' / 'polls_test.yaml' TEST_CONFIG = get_config() TEST_DB_URL = DSN.format(**TEST_CONFIG['postgres']) test_engine = create_engine(TEST_DB_URL) def setup_db(config): db_name = config['database'] db_user = config['user'] db_pass = config['password']
def db(): test_config = get_config() setup_db(test_config['postgres']) yield teardown_db(test_config['postgres'])