#!/usr/bin/python
#!/usr/bin/env python
#!/home/<user>/bin/python/bin/python
#
# File name: tg_fastcgi.fcgi
#
# This module provides the glue for running TurboGears applications behind
# FastCGI-enabled web servers. The code in this module depends on the fastcgi
# module downloadable from here:
#
# http://www.saddi.com/software/py-lib/py-lib/fcgi.py
# XXX: modified to use flup instead.
#
# NOTE: The fcgi.py file needs to be placed in a location that is on the
# system path, such as the same the directory as the tg_fastcgi.py file
# or in the base directory of the TG app code.
#
# To configure this module, please edit the three variables in the "USER EDIT
# SECTION" before starting the TG application. Also remember to edit the
# top of this file with the correct Python installation information.
import cherrypy
import sys
import os
from os.path import *
import pkg_resources
import turbogears
pkg_resources.require("TurboGears")
# -- START USER EDIT SECTION
# -- Users must edit this section --
# TODO: trailing slash or not?
code_dir = './'
log_dir = '/export/logs/' # (Optional) The log directory. Default = code_dir.
# -- END USER EDIT SECTION
def tg_init():
""" Checks for the required data and initializes the application. """
global code_dir
global log_dir
last_mark = 0
# Input checks
if not code_dir or not isdir(code_dir):
raise ValueError("""The code directory setting is missing.
The fastcgi code will be unable to find
the TG code without this setting.""")
sys.path.append(code_dir)
# Change the directory so the TG log file will not be written to the
# web app root.
if log_dir and isdir(log_dir):
os.chdir(log_dir)
else:
os.chdir(code_dir)
log_dir = code_dir
sys.stdout = open(join(log_dir, 'alexaholic-fastcgi-stdout.log'),'a')
sys.stderr = open(join(log_dir, 'alexaholic-fastcgi-stderr.log'),'a')
if exists(join(code_dir, "setup.py")):
sys.stderr.write("attemting to read config: %s" % join(code_dir, "dev.cfg"))
turbogears.update_config(configfile=join(code_dir, "dev.cfg"),
modulename="alexaholic.config" )
else:
sys.stderr.write("attemting to read config: %s" % join(code_dir, "prod.cfg"))
turbogears.update_config(configfile=join(code_dir, "prod.cfg"),
modulename="alexaholic.config")
# Set environment to production to disable auto-reload and
# add virtual path information.
cherrypy.config.update({
'global': {'server.environment': 'production'}})
# Get the Root controller.
from alexaholic.controllers import Root
cherrypy.root = Root()
# Main section -
# Initialize the application, then start the server.
tg_init()
from fcgi import WSGIServer
#from flup.server.fcgi import WSGIServer
cherrypy.server.start(initOnly=True, serverClass=None, server=None)
from cherrypy._cpwsgi import wsgiApp
WSGIServer(application=wsgiApp).run()