-
Notifications
You must be signed in to change notification settings - Fork 14
/
appengine_config.py
39 lines (26 loc) · 1.39 KB
/
appengine_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Appstats configuration."""
from gaesessions import SessionMiddleware, Session, _tls
COOKIE_KEY = '@COOKIE_KEY@'
if COOKIE_KEY == '@' + 'COOKIE_KEY' + '@':
COOKIE_KEY = 'generate a random key OFFLINE and paste to COOKIE_KEY'
class GaeSessionMiddleware(SessionMiddleware):
def __call__(self, environ, start_response):
# initialize a session for the current user
_tls.current_session = Session(lifetime=self.lifetime, no_datastore=self.no_datastore, cookie_only_threshold=self.cookie_only_thresh, cookie_key=self.cookie_key)
# create a hook for us to insert a cookie into the response headers
def my_start_response(status, headers, exc_info=None):
try:
_tls.current_session.save() # store the session if it was changed
except ValueError: # 1Mb memcache limit
_tls.current_session.clear()
for ch in _tls.current_session.make_cookie_headers():
headers.append(('Set-Cookie', ch))
return start_response(status, headers, exc_info)
# let the app do its thing
return self.app(environ, my_start_response)
def webapp_add_wsgi_middleware(app):
"""WSGI middleware declaration."""
from google.appengine.ext.appstats import recording
app = GaeSessionMiddleware(app, cookie_key=COOKIE_KEY)
app = recording.appstats_wsgi_middleware(app)
return app