I am using bottle to write a small web program and name the following source file index.py
. I also use beaker session library in the program. When I run the code using python index.py
everything goes right. But when I use gunicorn -c gunicorn.conf index:app
I get error message like this saying that beaker key beaker.session
doesn't exist. How can I change the code to make things work again in gunicorn server?
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 857, in _handle
self.trigger_hook('before_request')
File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 640, in trigger_hook
return [hook(*args, **kwargs) for hook in self._hooks[__name][:]]
File "/Users/yizeng/Documents/python_projects/simple-courses/index.py", line 17, in setup_request
request.session = request.environ['beaker.session']
KeyError: 'beaker.session'
source code for index.py
:
import os, sys, bottle
from bottle import debug, route, request, run, Bottle, static_file, hook
from apps import model, views
from beaker.middleware import SessionMiddleware
app = bottle.app()
session_options = {
'session.type': 'file',
'session.cookie_expires': 300,
'session.data_dir': './data',
'session.auto': True
}
@hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']
@app.route('/assets/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='assets')
@app.route('/test')
def test():
s = request.environ.get('beaker.session')
s['test'] = s.get('test', 0) + 1
s.save()
return 'Test counter: %d' % s['test']
app_session = SessionMiddleware(app, session_options)
if __name__ == '__main__':
app.run(app=app_session)
I believe you don't need the last block
if __name...
at all. Gunicorn running the index module's app "varibale" as a WSIG means it should spin up an instance of your bottle app.