Exemple #1
0
def main():
    load_config()

    handlers = [
        (r"/websocket", ChatHandler),
        (r"/", MainHandler)
    ]
    settings = {
        "debug": True,
        "chat_clients": [],
        "server_id": gen_token(8)
    }

    if options.CLOUDAMQP_URL:
        def broadcast(body):
            for client in settings.get('chat_clients'):
                client.write_message(body)
        queue = PikaTopic(options.CLOUDAMQP_URL,
                          broadcast, 'chat-messages')
        settings['broadcast_queue'] = queue
        logging.info("broadcast_queue %s", options.CLOUDAMQP_URL)
        queue.connect()

    def keep_alive():
        msg = str(time.time()).encode("utf8")
        for client in settings["chat_clients"]:
            client.ping(msg)
    tornado.ioloop.PeriodicCallback(keep_alive, 30000).start()

    application = tornado.web.Application(handlers, **settings)
    application.listen(options.PORT)
    logging.info("listening on port %s", options.PORT)

    tornado.ioloop.IOLoop.current().start()
Exemple #2
0
def make_app():
    http_origins = options.CORS_URLS
    ws_origins = [url_to_ws_origins(u) for u in http_origins]
    handlers = [
        (r"/api(.*)", RpcHandler, {'http_origins': http_origins,
                                   'ws_url': options.WS_URL}),
        (r"/websocket", RpcWebsocket, {'ws_origins': ws_origins}),
        (r"/logout", LogoutHandler),
        (r"/api.html", ApiPageHandler),
        (r"/", IndexHandler),
    ]

    settings = {
        'services': Service.describe(spddo.micro.func),
        'micro_context': Context,
        'cookie_name': 'micro-session',
        'cookie_secret': '-it-was-a-dark-and-spddo-chat-night-',
        'template_path': resource_filename('spddo.micro', "templates"),
        'allow_exception_messages': options.DEBUG,
        'gzip': True,
        'debug': options.DEBUG
    }

    db_url = heroku_db_url(options.CLEARDB_DATABASE_URL)
    db_connection.db_init(db_url)
    if options.DEBUG:
        create_all(Base, db_connection._engine_)
        with db_connection.session() as session:
            try:
                session.add(model.Person(email="*****@*****.**",
                                         password="******"))
                session.commit()
            except IntegrityError:
                session.rollback()

    if options.PROC_POOL_SIZE:
        pool_init_processes(options.PROC_POOL_SIZE,
                            options.DEBUG)

    if options.CLOUDAMQP_URL:
        queue = PikaTopic(options.CLOUDAMQP_URL,
                          RpcWebsocket.async_broadcast,
                          'micro-chat')
        queue.connect()
        settings["broadcast_queue"] = queue
        logging.info("broadcast_queue %s", options.CLOUDAMQP_URL)

    spddo.micro.func.cache.init_mc()

    return tornado.web.Application(handlers, **settings)
Exemple #3
0
def make_app():
    http_origins = options.CORS_URLS
    ws_origins = [url_to_ws_origins(u) for u in http_origins]
    handlers = [
        (r"/api(.*)", RpcHandler, {'http_origins': http_origins}),
        (r"/websocket", RpcWebsocket, {'ws_origins': ws_origins}),
        (r"/logout", LogoutHandler),
        (r'/(.*)', tornado.web.StaticFileHandler, {
            'path': resource_filename('spddo.todo', 'web'),
            'default_filename': 'index.html'
        })

    ]

    settings = {
        'services': Service.describe(spddo.todo.actions),
        'micro_context': Context,
        'cookie_name': 'micro-todo-session',
        'cookie_secret': '-it-was-a-dark-and-spddo-todo-night-',
        'allow_exception_messages': options.DEBUG,
        'gzip': True,
        'debug': options.DEBUG
    }

    db_url = heroku_db_url(options.CLEARDB_DATABASE_URL)
    engine = db_connection.db_init(db_url)
    if options.DEBUG:
        create_all(Base, engine)

    if options.PROC_POOL_SIZE:
        pool_init_processes(options.PROC_POOL_SIZE,
                            options.DEBUG)

    if options.CLOUDAMQP_URL:
        queue = PikaTopic(options.CLOUDAMQP_URL,
                          RpcWebsocket.async_broadcast,
                          'micro-chat')
        queue.connect()
        settings["broadcast_queue"] = queue
        logging.info("broadcast_queue %s", options.CLOUDAMQP_URL)

    return tornado.web.Application(handlers,
                                   **settings)