#!/usr/bin/python

import sys
import os

from tornado.options import options, define, parse_command_line
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi

from django.core.wsgi import get_wsgi_application


define('port', type=int, default=8080)


class HelloHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('Hello from tornado')


def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'haldir.settings'
    sys.path.append('./haldir')

    parse_command_line()

    wsgi_app = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(wsgi_app)

    settings = {
        'debug': True,
        'autoreload': True,
        # 'static_path': '/tmp/org.au9ustine.haldir/static'
    }
    
    tornado_app = tornado.web.Application(
    [
        (r'/hello-tornado', HelloHandler),
        (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': '/tmp/org.au9ustine.haldir/static'}),
        (r'.*', tornado.web.FallbackHandler, dict(fallback=container)),
    ], **settings)
    server = tornado.httpserver.HTTPServer(tornado_app)
    server.listen(options.port)
    
    tornado.ioloop.IOLoop.instance().start()


if __name__ == '__main__':
    main()
