pythongoogle-app-enginegae-module

Google App Engine: Disable session middleware for services


I have an app running with session middleware to handle logged on users. This is configured in appengine_config.py like this:

import datetime
from gaesessions import SessionMiddleware
import jinja2
# Add session middleware
def webapp_add_wsgi_middleware(app):
    app = SessionMiddleware(app, cookie_key="Yqjo13XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", lifetime=datetime.timedelta(minutes=30), no_datastore=True, cookie_only_threshold=0)
    return app

I now created a service to handle the cronjob requests but I don't want that to load the session middleware. Is there a way to have the service, located in a separate dir but with it's cronservice.yaml in the app root directory, to ignore the settings in the appengine_config.py? Those are only meant for the default app, the web UI.


Solution

  • Since my app is split up in several services I have my frontend controllers in a subdir frontend/controllers so I check the router info for it and if needed import the modules:

    def webapp_add_wsgi_middleware(app):
    # If frontend add session middleware
    if 'frontend' in str(app.router):
        # Setup GAE sessions
        import datetime
        from gaesessions import SessionMiddleware
        import jinja2
        app = SessionMiddleware(app, cookie_key="XXXXX", lifetime=datetime.timedelta(minutes=30), no_datastore=True, cookie_only_threshold=0)
    
    return app
    

    When this hits the str(app.router) contains