Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import os
  2. from urlparse import urlparse
  3. from flask import Flask
  4. from pymongo import Connection
  5.  
  6. MONGO_URL = os.environ.get('MONGOHQ_URL')
  7.  
  8. if MONGO_URL:
  9. # Get a connection
  10. connection = Connection(MONGO_URL)
  11. # Get the database
  12. db = connection[urlparse(MONGO_URL).path[1:]]
  13. else:
  14. # Not on an app with the MongoHQ add-on, do some localhost action
  15. connection = Connection('localhost', 27017)
  16. db = connection['MyDB']
  17.  
  18. app = Flask(__name__)
  19. app.debug = True
  20.  
  21. @app.route('/')
  22. def hello():
  23. myObj = db.analytics.find_one({'event':'page_views'})
  24. if not myObj:
  25. myObj = {'event':'page_views', 'count':1}
  26. else:
  27. myObj['count'] += 1
  28. db.analytics.save(myObj)
  29. return 'Hello World! ' + str(myObj['count'])
  30.  
  31. if __name__ == '__main__':
  32. # Bind to PORT if defined, otherwise default to 5000.
  33. port = int(os.environ.get('PORT', 5000))
  34. app.run(host='0.0.0.0', port=port)
Add Comment
Please, Sign In to add comment