server.py 1.21 KB
# coding: utf-8

import os
import tornado.ioloop
import tornado.autoreload
import tornado.web
from tornado.options import define, options, parse_command_line
import settings
from log import init_log


def init_settings():
    # 加载环境配置项
    define('port', default=8080, type=int, help=u'监听端口')
    define("log_dir", default='run', type=str, help=u'日志目录')
    define('profile', default='local', type=str, help=u'运行配置')

    parse_command_line()
    init_log(options.log_dir)
    return options

def start_application():
    from url import urls
    log_dir = os.path.join(os.path.dirname(__file__), 'run')
    application_settings = {
        'handlers': urls,
        'debug': settings.DEBUG,
        'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
        'static_path': os.path.join(os.path.dirname(__file__), 'static'),
    }
    application = tornado.web.Application(**application_settings)

    print 'server on port %s' % settings.PORT
    print 'server log on %s' % log_dir

    application.listen(settings.PORT)
    io_loop = tornado.ioloop.IOLoop.instance()

    io_loop.start()

def run():
    init_settings()
    start_application()

if __name__ == '__main__':
    run()