Created
July 10, 2014 18:14
-
-
Save stavinsky/5758bafc1ad2c473cf6e to your computer and use it in GitHub Desktop.
standalone cherrypy app for openshift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import cherrypy | |
## this have to be here to work on OpenShift environment | |
try: | |
zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'], | |
'virtenv', 'bin', 'activate_this.py') | |
execfile(zvirtenv, dict(__file__=zvirtenv)) | |
except KeyError: | |
pass | |
except IOError: | |
pass | |
class Root(): | |
@cherrypy.expose() | |
def index(self): | |
return "hello world" | |
def local_start(): | |
""" | |
For test environment we use local paths | |
""" | |
if 'OPENSHIFT_PYTHON_VERSION' not in os.environ: | |
os.environ['OPENSHIFT_DATA_DIR'] = '/var/tmp' | |
os.environ['OPENSHIFT_REPO_DIR'] = '/var/tmp' | |
os.environ['OPENSHIFT_PYTHON_IP'] = '127.0.0.1' | |
os.environ['OPENSHIFT_PYTHON_PORT'] = "8080" # this have to be a string | |
os.environ['OPENSHIFT_LOG_DIR'] = '/var/tmp' | |
if __name__ == '__main__': | |
local_start() | |
## this is config for server | |
global_config = { | |
'server.thread_pool': 100, | |
'log.screen': True, | |
'log.error_file': '%s/error.log' % os.environ['OPENSHIFT_LOG_DIR'], | |
'socket_queue_size': 50, | |
'thread_pool_max': -1, | |
} | |
## this is config file for app | |
root_conf = { | |
'/': { | |
'tools.sessions.on': True, | |
'tools.sessions.expire': 600, | |
'tools.caching.on': False, | |
'tools.caching.delay': 600, | |
'log.screen': True, | |
'log.access_file': '%s/access.log' % os.environ['OPENSHIFT_LOG_DIR'], | |
'log.error_file': '%s/error.log' % os.environ['OPENSHIFT_LOG_DIR'] | |
}, | |
'/static': { | |
'tools.staticdir.on': True, | |
'tools.staticdir.dir': "%s/static/" % os.environ['OPENSHIFT_REPO_DIR'] | |
}, | |
'/favicon.ico': { | |
'tools.staticfile.on': True, | |
'tools.staticfile.filename': "%s/static/favicon.ico" % os.environ['OPENSHIFT_REPO_DIR'] | |
}, | |
} | |
cherrypy.config.update(global_config) | |
#cherrypy.config.update({'environment': 'production'}) | |
cherrypy.tree.mount(Root(), "", root_conf) | |
ip = os.environ['OPENSHIFT_PYTHON_IP'] | |
port = int(os.environ['OPENSHIFT_PYTHON_PORT']) | |
cherrypy.server.socket_host = ip | |
cherrypy.server.socket_port = port | |
cherrypy.engine.start() | |
cherrypy.engine.block() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment