Beispiel #1
0
 def setup(self, environ):
     cfg = environ['pulsar.cfg']
     self.store = create_store(cfg.data_store)
     pubsub = self.store.pubsub()
     return WsgiHandler([
         Router('/', get=self.home_page),
         FileRouter('/favicon.ico', FAVICON),
         WebSocket('/message', TweetsWsHandler(pubsub, self.channel))
     ])
Beispiel #2
0
    def setup(self, environ):
        # only post allowed by the JSON RPC handler
        api = Producer(self.cfg)
        handler = TaskQueueRpc(api)
        for consumer in api.consumers:
            rpc = consumer.rpc()
            if rpc:
                handler.putSubHandler(consumer.name, rpc)

        request = [Router('/', post=handler)]
        response = [GZipMiddleware(200)]
        return WsgiHandler(middleware=request, response_middleware=response)
Beispiel #3
0
 async def test_media_router_serve_only(self):
     router = MediaRouter('/', serve_only=('json', 'png'))
     self.assertIsInstance(router._serve_only, set)
     self.assertEqual(len(router._serve_only), 2)
     cli = HttpWsgiClient(WsgiHandler((router, hello)))
     response = await cli.get('http://example.com/foo')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.text, 'Hello World!\n')
     response = await cli.get('http://example.com/foo/bla')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.text, 'Hello World!\n')
     response = await cli.get('http://example.com/foo/bla.png')
     self.assertEqual(response.status_code, 404)
Beispiel #4
0
 def setup(self, environ=None):
     '''Set up the :class:`.WsgiHandler` the first time this
     middleware is accessed.
     '''
     from django.conf import settings
     from django.core.wsgi import get_wsgi_application
     #
     try:
         dotted = settings.WSGI_APPLICATION
     except AttributeError:  # pragma nocover
         dotted = None
     if dotted:
         return module_attribute(dotted)()
     else:
         app = middleware_in_executor(get_wsgi_application())
         return WsgiHandler((wait_for_body_middleware, app))
Beispiel #5
0
    def setup(self, environ):
        '''Called once only to setup the WSGI application handler.

        Check :ref:`lazy wsgi handler <wsgi-lazy-handler>`
        section for further information.
        '''
        cfg = environ['pulsar.cfg']
        loop = environ['pulsar.connection']._loop
        self.store = create_store(cfg.data_store, loop=loop)
        pubsub = self.store.pubsub(protocol=Protocol())
        channel = '%s_webchat' % self.name
        pubsub.subscribe(channel)
        return WsgiHandler([Router('/', get=self.home_page),
                            WebSocket('/message', Chat(pubsub, channel)),
                            Router('/rpc', post=Rpc(pubsub, channel),
                                   response_content_types=JSON_CONTENT_TYPES)])
Beispiel #6
0
 def setup(self, environ=None):
     '''Set up the :class:`.WsgiHandler` the first time this
     middleware is accessed.
     '''
     from django.conf import settings
     from django.core.wsgi import get_wsgi_application
     #
     app = get_wsgi_application()
     green_workers = self.cfg.greenlet if self.cfg else 0
     if greenio and green_workers:
         if pg:
             pg.make_asynchronous()
         app = greenio.RunInPool(app, green_workers)
         self.green_safe_connections()
     else:
         app = middleware_in_executor(app)
     return WsgiHandler((wait_for_body_middleware, app))
Beispiel #7
0
    def setup(self, environ):
        '''Called once only by the WSGI server.

        It returns a :class:`.WsgiHandler` with three routes:

        * The base route served by the :meth:`home_page` method
        * The websocket route
        * A route for static files
        '''
        cfg = environ['pulsar.cfg']
        # Create the store and the pubsub handler
        self.store = create_store(cfg.data_store)
        pubsub = self.store.pubsub()
        # subscribe to channel
        ensure_future(self.subscribe(pubsub))
        return WsgiHandler([Router('/', get=self.home_page),
                            MediaRouter('/static', STATIC_DIR),
                            WebSocket('/message',
                                      TweetsWsHandler(pubsub, self.channel))])
Beispiel #8
0
    def setup(self, environ):
        '''Called once only to setup the WSGI application handler.

        Check :ref:`lazy wsgi handler <wsgi-lazy-handler>`
        section for further information.
        '''
        request = wsgi_request(environ)
        cfg = request.cache.cfg
        loop = request.cache._loop
        self.store = create_store(cfg.data_store, loop=loop)
        pubsub = self.store.pubsub(protocol=Protocol())
        channel = '%s_webchat' % self.name
        ensure_future(pubsub.subscribe(channel), loop=loop)
        return WsgiHandler([
            Router('/', get=self.home_page),
            WebSocket('/message', Chat(pubsub, channel)),
            Router('/rpc',
                   post=Rpc(pubsub, channel),
                   response_content_types=JSON_CONTENT_TYPES)
        ], [AsyncResponseMiddleware,
            GZipMiddleware(min_length=20)])
Beispiel #9
0
    def handler(self):
        '''The WSGI application handler for this :class:`App`.

        It is lazily loaded the first time it is accessed so that
        this :class:`App` can be used by pulsar in a multiprocessing setup.
        '''
        # do this here so that the config is already loaded before fire signal
        extensions = list(itervalues(self.extensions))
        self.fire('on_config')
        middleware = []
        rmiddleware = []
        for extension in extensions:
            _middleware = extension.middleware(self)
            if _middleware:
                middleware.extend(_middleware)
            _middleware = extension.response_middleware(self)
            if _middleware:
                rmiddleware.extend(_middleware)
        # Response middleware executed in reversed order
        rmiddleware = list(reversed(rmiddleware))
        handler = WsgiHandler(middleware, response_middleware=rmiddleware)
        self.fire('on_loaded', handler)
        return handler
Beispiel #10
0
 def setup(self, environ=None):
     '''Set up the :class:`.WsgiHandler` the first time this
     middleware is accessed.
     '''
     from django.conf import settings
     from django.core.wsgi import get_wsgi_application
     #
     try:
         dotted = settings.WSGI_APPLICATION
     except AttributeError:  # pragma nocover
         dotted = None
     if dotted:
         app = module_attribute(dotted)
     else:
         app = get_wsgi_application()
     green_workers = self.cfg.greenlet if self.cfg else 0
     if greenio and green_workers:
         if pg:
             pg.make_asynchronous()
         app = greenio.RunInPool(app, green_workers)
         self.green_safe_connections()
     else:
         app = middleware_in_executor(app)
     return WsgiHandler((wait_for_body_middleware, app))
Beispiel #11
0
 def setup(self):
     from django.conf import settings
     return WsgiHandler((wait_for_body_middleware, get_wsgi_application()))
 def setup(self, environ):
     return WsgiHandler(middleware=[SimpleRoute('/')])