Skip to content

Instantly share code, notes, and snippets.

@mfojtik
Created February 21, 2014 14:00
Show Gist options
  • Save mfojtik/9134715 to your computer and use it in GitHub Desktop.
Save mfojtik/9134715 to your computer and use it in GitHub Desktop.
Python application
#!/usr/bin/python
import os
import psycopg2
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
#
# IMPORTANT: Put any additional includes below this line. If placed above this
# line, it's possible required libraries won't be in your searchable path
#
def application(environ, start_response):
ctype = 'text/plain'
if environ['PATH_INFO'] == '/health':
response_body = "1"
elif environ['PATH_INFO'] == '/env':
response_body = ['%s: %s' % (key, value)
for key, value in sorted(environ.items())]
response_body = '\n'.join(response_body)
elif environ['PATH_INFO'] == '/postgres_check':
conn = psycopg2.connect("user=%s port=%s password=%s host=%s" % (
environ['OPENSHIFT_POSTGRESQL_DB_USERNAME'],
environ['OPENSHIFT_POSTGRESQL_DB_PORT'],
environ['OPENSHIFT_POSTGRESQL_DB_PASSWORD'],
environ['OPENSHIFT_POSTGRESQL_DB_HOST'],
))
cur = conn.cursor()
cur.execute("SELECT 0 + 1 AS SOLUTION")
response_body = str(cur.fetchone())
cur.close()
conn.close()
else:
ctype = 'text/html'
response_body = ''
status = '200 OK'
response_headers = [('Content-Type', ctype), ('Content-Length', str(len(response_body)))]
#
start_response(status, response_headers)
return [response_body]
#
# Below for testing only
#
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 8051, application)
# Wait for a single request, serve it and quit.
httpd.handle_request()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment