Created
September 12, 2012 00:34
-
-
Save heat/3703286 to your computer and use it in GitHub Desktop.
example wsgi flask application for appfog
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 | |
import time | |
import sys | |
import os | |
import json | |
from flask import Flask, Request, Response | |
application = app = Flask('wsgi') | |
@app.route('/') | |
def welcome(): | |
return 'welcome to appfog!' | |
@app.route('/env') | |
def env(): | |
return os.environ.get("VCAP_SERVICES", "{}") | |
@app.route('/mongo') | |
def mongotest(): | |
from pymongo import Connection | |
uri = mongodb_uri() | |
conn = Connection(uri) | |
coll = conn.db['ts'] | |
coll.insert(dict(now=int(time.time()))) | |
last_few = [str(x['now']) for x in coll.find(sort=[("_id", -1)], limit=10)] | |
body = "\n".join(last_few) | |
return Response(body, content_type="text/plain;charset=UTF-8") | |
def mongodb_uri(): | |
local = os.environ.get("MONGODB", None) | |
if local: | |
return local | |
services = json.loads(os.environ.get("VCAP_SERVICES", "{}")) | |
if services: | |
creds = services['mongodb-1.8'][0]['credentials'] | |
uri = "mongodb://%s:%s@%s:%d/%s" % ( | |
creds['username'], | |
creds['password'], | |
creds['hostname'], | |
creds['port'], | |
creds['db']) | |
print >> sys.stderr, uri | |
return uri | |
else: | |
raise Exception, "No services configured" | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment