#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- mode: python -*-

import aiopg
import psycopg2
import tornado.locks
from tornado.options import define, options
from tornado.web import Application

from app.application import Application

define('port', default=8080, help="listening port")
define('bind_address', default="", help="bind address")
define("db_host", default="127.0.0.1", help="database host")
define("db_port", default=5432, help="database port")
define("db_database", default="tornado", help="database name")
define("db_user", default="tornado", help="database user")
define("db_password", default="tornado", help="database password")


async def maybe_create_tables(db):
    try:
        with (await db.cursor()) as cur:
            await cur.execute("SELECT COUNT(*) FROM schema LIMIT 1")
            await cur.fetchone()
    except psycopg2.ProgrammingError:
        print("Database error!")


async def main():
    options.parse_command_line()

    async with aiopg.create_pool(
            host=options.db_host,
            port=options.db_port,
            user=options.db_user,
            password=options.db_password,
            dbname=options.db_database) as db:
        await maybe_create_tables(db)
        app = Application(db)
        app.listen(options.port, options.bind_address, xheaders=True)
        print("Listening on http://%s:%i" % (options.bind_address, options.port))
        shutdown_event = tornado.locks.Event()
        await shutdown_event.wait()

[((13, 0, 13, 51), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((14, 0, 14, 55), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((15, 0, 15, 60), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((16, 0, 16, 53), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((17, 0, 17, 62), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((18, 0, 18, 58), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((19, 0, 19, 66), 'tornado.options.define', 'define', (), '', False, 'from tornado.options import define, options\n'), ((32, 4, 32, 32), 'tornado.options.options.parse_command_line', 'options.parse_command_line', ({}, {}), '()', False, 'from tornado.options import define, options\n'), ((34, 15, 39, 39), 'aiopg.create_pool', 'aiopg.create_pool', (), '', False, 'import aiopg\n'), ((41, 14, 41, 29), 'app.application.Application', 'Application', ({(41, 26, 41, 28): 'db'}, {}), '(db)', False, 'from app.application import Application\n')]