pythonpyramidwsgitilestache

How to serve a wsgi app from within Pyramid?


I'm building a Pyramid application that needs to serve map tiles to an OpenLayers web map.

TileStache is a WMS tile server that serves the tiles that I need, and I want to access it as a view in my Pyramid app.

On its own, visiting the TileStache url, www.exampletilestacheurl.com/LAYERNAME/0/0/0.png, works great - it returns the tile properly.

In Pyramid, I want to wrap the TileStache app as a view using pyramid.wsgi.wsgiapp. My goal is that visiting www.mypyramidapp.com/tilestache/LAYERNAME/0/0/0.png would work just like the above TileStache url example.

I wrapped the TileStache app to be a view:

from pyramid.wsgi import wsgiapp

@wsgiapp
def tileserver(environ, start_response):
    # Enable TileStache tile server
    import TileStache
    tile_app = TileStache.WSGITileServer('tilestache/tilestache.cfg', autoreload=False)
    return [tile_app]

And assigned a route for the view in myapp.__init__.main:

from tilestache import tileserver
config.add_view(tileserver, name='tilestache')
config.add_route('tilestache', '/tilestache')

But when I visit any url starting with www.mypyramidapp.com/tilestache/, it just returns IndexError: list index out of range. Is anyone familiar with how wsgiapp works?


Solution

  • if tile_app is a wsgi application you need to return the result of calling it like so...

    from pyramid.wsgi import wsgiapp
    
    # Enable TileStache tile server
    import TileStache
    tile_app = TileStache.WSGITileServer('tilestache/tilestache.cfg', autoreload=False)
    
    @wsgiapp
    def tileserver(environ, start_response):
    
        return tile_app(environ, start_response)
    

    note: I moved the app creation to the module level so that it is created at import and not every time a request is handled. that might not be the behavior you are looking for but most of the time it is.