| 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 | # |
|---|
| 19 | # 2006-04-17 |
|---|
| 20 | # ---------- |
|---|
| 21 | # * Updated for 0.9a4 by Mikael Jansson <mail@mikael.jansson.be> for changes in |
|---|
| 22 | # config files. |
|---|
| 23 | # * Removed hard TABs |
|---|
| 24 | # |
|---|
| 25 | |
|---|
| 26 | import cherrypy |
|---|
| 27 | import sys |
|---|
| 28 | import os |
|---|
| 29 | from os.path import * |
|---|
| 30 | import pkg_resources |
|---|
| 31 | import turbogears |
|---|
| 32 | |
|---|
| 33 | pkg_resources.require("TurboGears") |
|---|
| 34 | |
|---|
| 35 | # -- START USER EDIT SECTION |
|---|
| 36 | # -- Users must edit this section -- |
|---|
| 37 | code_dir = '' # (Required) The base directory of the TG app code. |
|---|
| 38 | root_class_name = '' # (Required) The fully qualified Root class name. |
|---|
| 39 | project_module_name = 'PROJECTNAME.config' # (Required) The config module name. Replace |
|---|
| 40 | # PROJECTNAME with your project's name (e.g. wiki20). |
|---|
| 41 | log_dir = '' # (Optional) The log directory. Default = code_dir. |
|---|
| 42 | # -- END USER EDIT SECTION |
|---|
| 43 | |
|---|
| 44 | class VirtualPathFilter(object): |
|---|
| 45 | def on_start_resource(self): |
|---|
| 46 | if not cherrypy.config.get('virtual_path_filter.on', False): |
|---|
| 47 | return |
|---|
| 48 | prefix = cherrypy.config.get('virtual_path_filter.prefix', '') |
|---|
| 49 | if not prefix: |
|---|
| 50 | return |
|---|
| 51 | |
|---|
| 52 | path = cherrypy.request.object_path |
|---|
| 53 | if path == prefix: |
|---|
| 54 | path = '/' |
|---|
| 55 | elif path.startswith(prefix): |
|---|
| 56 | path = path[len(prefix):] |
|---|
| 57 | else: |
|---|
| 58 | raise cherrypy.NotFound(path) |
|---|
| 59 | cherrypy.request.object_path = path |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | def tg_init(): |
|---|
| 63 | """ Checks for the required data and initializes the application. """ |
|---|
| 64 | |
|---|
| 65 | global code_dir |
|---|
| 66 | global root_class_name |
|---|
| 67 | global log_dir |
|---|
| 68 | global project_module_name |
|---|
| 69 | last_mark = 0 |
|---|
| 70 | |
|---|
| 71 | # Input checks |
|---|
| 72 | if not code_dir or not isdir(code_dir): |
|---|
| 73 | raise ValueError("""The code directory setting is missing. |
|---|
| 74 | The fastcgi code will be unable to find |
|---|
| 75 | the TG code without this setting.""") |
|---|
| 76 | |
|---|
| 77 | if not root_class_name: |
|---|
| 78 | raise ValueError("""The fully qualified root class name must |
|---|
| 79 | be provided.""") |
|---|
| 80 | |
|---|
| 81 | last_mark = root_class_name.rfind('.') |
|---|
| 82 | |
|---|
| 83 | if (last_mark < 1) or (last_mark + 1) == len(root_class_name): |
|---|
| 84 | raise ValueError("""The user-defined class name is invalid. |
|---|
| 85 | Please make sure to include a fully |
|---|
| 86 | qualified class name for the root_class |
|---|
| 87 | value (e.g. wiki20.controllers.Root).""") |
|---|
| 88 | |
|---|
| 89 | sys.path.append(code_dir) |
|---|
| 90 | |
|---|
| 91 | # Change the directory so the TG log file will not be written to the |
|---|
| 92 | # web app root. |
|---|
| 93 | if log_dir and isdir(log_dir): |
|---|
| 94 | os.chdir(log_dir) |
|---|
| 95 | else: |
|---|
| 96 | os.chdir(code_dir) |
|---|
| 97 | log_dir = code_dir |
|---|
| 98 | |
|---|
| 99 | sys.stdout = open(join(log_dir, 'stdout.log'),'a') |
|---|
| 100 | sys.stderr = open(join(log_dir, 'stderr.log'),'a') |
|---|
| 101 | |
|---|
| 102 | if exists(join(code_dir, "setup.py")): |
|---|
| 103 | turbogears.update_config(configfile=join(code_dir, "devcfg.py"),modulename=project_module_name) |
|---|
| 104 | else: |
|---|
| 105 | turbogears.update_config(configfile=join(code_dir, "prodcfg.py"),modulename=project_module_name) |
|---|
| 106 | |
|---|
| 107 | # Set environment to production to disable auto-reload and |
|---|
| 108 | # add virutal path information. |
|---|
| 109 | cherrypy.config.update({ |
|---|
| 110 | 'global': {'server.environment': 'production'}, |
|---|
| 111 | '/' : { 'virtual_path_filter.on' : True, |
|---|
| 112 | 'virtual_path_filter.prefix' : '/tg_fastcgi.fcgi' } |
|---|
| 113 | }) |
|---|
| 114 | |
|---|
| 115 | # Parse out the root class information for Cherrypy Root class. |
|---|
| 116 | package_name = root_class_name[:last_mark] |
|---|
| 117 | class_name = root_class_name[last_mark+1:] |
|---|
| 118 | exec('from %s import %s as Root' % (package_name, class_name)) |
|---|
| 119 | Root._cp_filters = [VirtualPathFilter()] |
|---|
| 120 | cherrypy.root = Root() |
|---|
| 121 | |
|---|
| 122 | # Main section - |
|---|
| 123 | # Initialize the application, then start the server. |
|---|
| 124 | tg_init() |
|---|
| 125 | |
|---|
| 126 | from fcgi import WSGIServer |
|---|
| 127 | cherrypy.server.start(initOnly=True, serverClass=None) |
|---|
| 128 | |
|---|
| 129 | from cherrypy._cpwsgi import wsgiApp |
|---|
| 130 | WSGIServer(application=wsgiApp).run() |
|---|