Exemple #1
0
def run(userUrlmap, **confArgs):
	"""Start handling requests. Run script with -h or --help options to
		see the list of possible options.
		Input: (list(tuples)) url mapping, (mixed) various configuration attributes.
			Accepted configuration attributes:
			* server_type - server type, one of "standalone", "fcgi" or "cgi"
			* user_module_dir - path to user module directory
			* template_dir - path to user templates directory
			* static_file_dir - path to directory for static files
			* interface - ip address or hostname which server (standalone or fcgi)
				will use for binding
			* port - port which server will use for binding
			* pid_file - path to server process id file
			* user - server process will belong to this user
			* group - server process will belong to this group
		Output: none
		NOTE: arguments of this function are overriden by command line arguments!
	"""
	# set signal handler
	signal.signal(signal.SIGINT, killSignalHandler)
	signal.signal(signal.SIGTERM, killSignalHandler)
	
	# SIGHUP could not be handled on Windows
	if sysname != 'windows':
		signal.signal(signal.SIGHUP, hupSignalHandler)
	
	
	# fork and run as daemon by default
	runInBackground = True
	
	# fill configuration with data supplied by user
	serverConf = conf['SERVER']
	for name, value in confArgs.items():
		if name not in serverConf:
			raise NameError, 'Attribute "%s" is not supported' % name
		serverConf[name] = value
	
	# get configuration attributes from cli
	# they should overwrite any user supplied configuration parameters
	getCliconfig()
	
	# apply configuration
	if serverConf['foreground']:
		runInBackground = False
	if serverConf['debug']:
		global debugOn
		debugOn = True
		logger.logToScreen = True
		logger.showErrors = True
		logger.showDebug = True
		logger.showInfo = True
		logger.showWarning = True
		logger.createOutputHandlers()
		runInBackground = False
		serverConf['foreground'] = True
	user = serverConf['user']
	group = serverConf['group']
	
	# show parsed configuration data to user
	debug ('Final server configuration:')
	debug (conf)
	
	# fork and run as daemon
	if sysname != 'windows' and runInBackground:
		info ('Daemonizing...')
		childProcId = os.fork()
		if childProcId != 0:
			sys.exit(0)
	
	# store global variables
	global userModDir, userTemplateDir, staticFileRoot
	userModDir = serverConf['user_module_dir']
	userTemplateDir = serverConf['template_dir']
	staticFileRoot = serverConf['static_file_dir']
	
	# set up and check template directories
	global baseTplPath, compiledTplPath, langfileDirPath
	baseTplPath = os.path.join(userTemplateDir, baseTplPath)
	compiledTplPath = os.path.join(userTemplateDir, compiledTplPath)
	langfileDirPath = os.path.join(userTemplateDir, langfileDirPath)
	checkDir(userTemplateDir) # we need read only access
	checkDir(langfileDirPath) # we need read only access
	checkDir(staticFileRoot) # we need read only access
	checkDir(compiledTplPath, user = user, group = group) # rw access needed
	
	# store pid in file
	makePidfile(serverConf['pid_file'], user, group)
	
	# assign webserver before loading any other modules
	assignWebserver(serverConf['server_type'])
	
	# set up webserver and bind it to interface and port 
	web.init(handleRequest, serverConf['interface'], serverConf['port'])
	# switch user and group ids
	switchUid(user, group)
	
	# compile regexps in urlmap and load mapped functions
	loadModules(userUrlmap)
	
	# compile user templates
	compileTemplates()

	# start serving requests at last
	keepRunning = True
	while keepRunning:
		keepRunning = web.run()
		if keepRunning:
			reloadModules()
			compileTemplates()
	
	# Actually we should never get this far
	# but I'll put quit() here just because of being such a paranoic.
	quit('Exiting')
Exemple #2
0
logger.showDebug = False
logger.showInfo = False
logger.showWarning = True

# counts processed requests
requestCounter = 0

# regular expression which matches static files
staticFileRegexp = re.compile(r'/static/(.+)')

# log message prefixes
logger.errorPrefix = "ERROR: "
logger.debugPrefix = "DEBUG: "
logger.infoPrefix = "INFO:  "
logger.warningPrefix = "WARNING: "
logger.createOutputHandlers()

# configuration defaults
configDefaults = {
	'SERVER' : {
		'server_type' : 'standalone',
		'interface' : 'localhost',
		'port' : 8000,
		'foreground' : False,
		'debug' : False,
		'pid_file' : '/usr/local/var/run/webstuff/webstuffd.pid',
		'user' : '',
		'group' : '',
		'user_module_dir' : './modules',
		'template_dir' : './templates',
		'static_file_dir' : './static',