Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

TurboGearsOnDreamHost: tg_fastcgi.fcgi.2.v9

File tg_fastcgi.fcgi.2.v9, 4.3 KB (added by alecf@…, 6 years ago)

version for v0.9beta with VirtualPathFilter? support

Line 
1#!/home/<user>/bin/python/bin/python
2#
3# File name: tg_fastcgi.fcgi
4#
5# This module provides the glue for running TurboGears applications behind
6# FastCGI-enabled web servers. The code in this module depends on the fastcgi
7# module downloadable from here:
8#
9# http://www.saddi.com/software/py-lib/py-lib/fcgi.py
10#
11# NOTE: The fcgi.py file needs to be placed in a location that is on the
12# system path, such as the same the directory as the tg_fastcgi.py file
13# or in the base directory of the TG app code.
14#
15# To configure this module, please edit the three variables in the "USER EDIT
16# SECTION" before starting the TG application.  Also remember to edit the
17# top of this file with the correct Python installation information.
18
19import cherrypy
20import sys
21import os
22from os.path import *
23import pkg_resources
24import turbogears
25
26pkg_resources.require("TurboGears")
27
28# -- START USER EDIT SECTION
29# -- Users must edit this section --
30code_dir = ''                               # (Required) The base directory of the TG app code.
31root_class_name = ''                        # (Required) The fully qualified Root class name.
32project_module_name = 'PROJECTNAME.config'  # (Required) The config module name. Replace
33                                            # PROJECTNAME with your project's name (e.g. wiki20).
34log_dir = ''                                # (Optional) The log directory. Default = code_dir.
35# -- END USER EDIT SECTION
36
37class VirtualPathFilter(object):
38    def on_start_resource(self):
39        if not cherrypy.config.get('virtual_path_filter.on', False):
40            return
41        prefix = cherrypy.config.get('virtual_path_filter.prefix', '')
42        if not prefix:
43            return
44
45        path = cherrypy.request.object_path
46        if path == prefix:
47            path = '/'
48        elif path.startswith(prefix):
49            path = path[len(prefix):]
50        else:
51            raise cherrypy.NotFound(path)
52        cherrypy.request.object_path = path
53
54
55def tg_init():
56    """ Checks for the required data and initializes the application. """
57
58    global code_dir
59    global root_class_name
60    global log_dir
61    global project_module_name
62    last_mark = 0
63
64    # Input checks
65    if not code_dir or not isdir(code_dir):
66        raise ValueError("""The code directory setting is missing.
67                            The fastcgi code will be unable to find
68                            the TG code without this setting.""")
69
70    if not root_class_name:
71        raise ValueError("""The fully qualified root class name must
72                            be provided.""")
73
74    last_mark = root_class_name.rfind('.')
75   
76    if (last_mark < 1) or (last_mark + 1) == len(root_class_name):
77        raise ValueError("""The user-defined class name is invalid.
78                            Please make sure to include a fully
79                            qualified class name for the root_class
80                            value (e.g. wiki20.controllers.Root).""")   
81
82    sys.path.append(code_dir)
83
84    # Change the directory so the TG log file will not be written to the
85    # web app root.
86    if log_dir and isdir(log_dir):
87        os.chdir(log_dir)
88    else:
89        os.chdir(code_dir)
90        log_dir = code_dir
91
92    sys.stdout = open(join(log_dir, 'stdout.log'),'a')
93    sys.stderr = open(join(log_dir, 'stderr.log'),'a')       
94
95    if exists(join(code_dir, "setup.py")):
96        turbogears.update_config(configfile=join(code_dir, "devcfg.py"),modulename=project_module_name)
97    else:
98        turbogears.update_config(configfile=join(code_dir, "prodcfg.py"),modulename=project_module_name)
99
100    # Set environment to production to disable auto-reload and
101    # add virutal path information.
102    cherrypy.config.update({
103        'global': {'server.environment': 'production'},
104        '/' : { 'virtual_path_filter.on' : True,
105                'virtual_path_filter.prefix' : '/tg_fastcgi.fcgi' }
106                })
107
108    # Parse out the root class information for Cherrypy Root class.
109    package_name = root_class_name[:last_mark]
110    class_name = root_class_name[last_mark+1:]
111    exec('from %s import %s as Root' % (package_name, class_name))
112    Root._cp_filters = [VirtualPathFilter()]
113    cherrypy.root = Root()
114
115# Main section -
116# Initialize the application, then start the server.
117tg_init()
118
119from fcgi import WSGIServer
120cherrypy.server.start(initOnly=True, serverClass=None)
121
122from cherrypy._cpwsgi import wsgiApp
123WSGIServer(application=wsgiApp).run()