1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | """ |
---|
5 | This file is part of the web2py Web Framework |
---|
6 | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> |
---|
7 | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) |
---|
8 | """ |
---|
9 | |
---|
10 | ############################################################################## |
---|
11 | # Configuration parameters for Google App Engine |
---|
12 | ############################################################################## |
---|
13 | LOG_STATS = False # web2py level log statistics |
---|
14 | APPSTATS = True # GAE level usage statistics and profiling |
---|
15 | DEBUG = False # debug mode |
---|
16 | # |
---|
17 | # Read more about APPSTATS here |
---|
18 | # http://googleappengine.blogspot.com/2010/03/easy-performance-profiling-with.html |
---|
19 | # can be accessed from: |
---|
20 | # http://localhost:8080/_ah/stats |
---|
21 | ############################################################################## |
---|
22 | # All tricks in this file developed by Robin Bhattacharyya |
---|
23 | ############################################################################## |
---|
24 | |
---|
25 | |
---|
26 | import time |
---|
27 | import os |
---|
28 | import sys |
---|
29 | import logging |
---|
30 | import cPickle |
---|
31 | import pickle |
---|
32 | import wsgiref.handlers |
---|
33 | import datetime |
---|
34 | |
---|
35 | path = os.path.dirname(os.path.abspath(__file__)) |
---|
36 | |
---|
37 | # os.chdir(path) ? |
---|
38 | |
---|
39 | if not os.path.isdir('applications'): |
---|
40 | raise RuntimeError('Running from the wrong folder') |
---|
41 | |
---|
42 | sys.path = [path] + [p for p in sys.path if not p == path] |
---|
43 | |
---|
44 | sys.modules['cPickle'] = sys.modules['pickle'] |
---|
45 | |
---|
46 | |
---|
47 | from gluon.settings import global_settings |
---|
48 | from google.appengine.ext import webapp |
---|
49 | from google.appengine.ext.webapp.util import run_wsgi_app |
---|
50 | |
---|
51 | |
---|
52 | global_settings.web2py_runtime_gae = True |
---|
53 | global_settings.db_sessions = True |
---|
54 | if os.environ.get('SERVER_SOFTWARE', '').startswith('Devel'): |
---|
55 | (global_settings.web2py_runtime, DEBUG) = \ |
---|
56 | ('gae:development', True) |
---|
57 | else: |
---|
58 | (global_settings.web2py_runtime, DEBUG) = \ |
---|
59 | ('gae:production', False) |
---|
60 | |
---|
61 | |
---|
62 | import gluon.main |
---|
63 | |
---|
64 | |
---|
65 | def log_stats(fun): |
---|
66 | """Function that will act as a decorator to make logging""" |
---|
67 | def newfun(env, res): |
---|
68 | """Log the execution time of the passed function""" |
---|
69 | timer = lambda t: (t.time(), t.clock()) |
---|
70 | (t0, c0) = timer(time) |
---|
71 | executed_function = fun(env, res) |
---|
72 | (t1, c1) = timer(time) |
---|
73 | log_info = """**** Request: %.2fms/%.2fms (real time/cpu time)""" |
---|
74 | log_info = log_info % ((t1 - t0) * 1000, (c1 - c0) * 1000) |
---|
75 | logging.info(log_info) |
---|
76 | return executed_function |
---|
77 | return newfun |
---|
78 | |
---|
79 | |
---|
80 | logging.basicConfig(level=logging.INFO) |
---|
81 | |
---|
82 | |
---|
83 | def wsgiapp(env, res): |
---|
84 | """Return the wsgiapp""" |
---|
85 | env['PATH_INFO'] = env['PATH_INFO'].decode('latin1').encode('utf8') |
---|
86 | |
---|
87 | #when using the blobstore image uploader GAE dev SDK passes these as unicode |
---|
88 | # they should be regular strings as they are parts of URLs |
---|
89 | env['wsgi.url_scheme'] = str(env['wsgi.url_scheme']) |
---|
90 | env['QUERY_STRING'] = str(env['QUERY_STRING']) |
---|
91 | env['SERVER_NAME'] = str(env['SERVER_NAME']) |
---|
92 | |
---|
93 | #this deals with a problem where GAE development server seems to forget |
---|
94 | # the path between requests |
---|
95 | if global_settings.web2py_runtime == 'gae:development': |
---|
96 | gluon.admin.create_missing_folders() |
---|
97 | |
---|
98 | web2py_path = global_settings.applications_parent # backward compatibility |
---|
99 | |
---|
100 | return gluon.main.wsgibase(env, res) |
---|
101 | |
---|
102 | |
---|
103 | if LOG_STATS or DEBUG: |
---|
104 | wsgiapp = log_stats(wsgiapp) |
---|
105 | |
---|
106 | |
---|
107 | def main(): |
---|
108 | """Run the wsgi app""" |
---|
109 | run_wsgi_app(wsgiapp) |
---|
110 | |
---|
111 | if __name__ == '__main__': |
---|
112 | main() |
---|