-
Notifications
You must be signed in to change notification settings - Fork 12
/
run.py
113 lines (98 loc) · 3.75 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
"""Instantiates the Python Eve REST API Server.
Instantiates the Python Eve REST API Server for both local
and cloud (IBM Bluemix) execution. Provides a default catch-all
routing to provide API consumers with intentional responses
for all routes. Provides a redis cloud caching instance for
session management where desired.
"""
from settings import (REDIS_INSTANCE,
APP_HOST,
APP_PORT,
VCAP_CONFIG)
from flask.ext.bootstrap import Bootstrap
from eve import Eve
from eve_docs import eve_docs
from eve_swagger import swagger
from routes import home
from hooks.event import (before_returning_items,
after_returning_items)
from gevent import wsgi, monkey, socket
import os
from platform import python_version
__author__ = "Sanjay Joshi"
__copyright__ = "IBM Copyright 2015"
__credits__ = ["Sanjay Joshi"]
__license__ = "Apache 2.0"
__version__ = "1.0"
__maintainer__ = "Sanjay Joshi"
__email__ = "joshisa@us.ibm.com"
__status__ = "Prototype"
# Monkey Patching app behavior to make it greenlet non-blocking
# This is usually required by gevent for native bindings for things
# like Redis interactions, etc ...
monkey.patch_all()
socket.setdefaulttimeout(240)
# capture current working directory
PWD = os.environ.get("PWD")
# set static folder path for static data
static_folder = os.path.join(PWD, "macreduce/static")
# Detect if we are deployed within Bluemix or not and configure accordingly
if VCAP_CONFIG:
print('Welcome to Bluemix')
print('Running on Python version: ' + python_version())
app = Eve(static_folder=static_folder,
redis=REDIS_INSTANCE)
REDIS_INSTANCE.flushdb()
else:
print('We are not running in Bluemix! Dev Mode Enabled')
app = Eve(static_folder=static_folder,
redis=REDIS_INSTANCE)
print(' Enabling Debug ...')
app.debug = True
# Setup some default home page path rules for JSON and HTML
app.add_url_rule('/', 'index', home.index)
# app.add_url_rule('/<path:path>', 'nonresource', home.index)
# Setup a favicon url for the home page
app.add_url_rule('/favicon', 'favicon',
view_func=home.favicon, methods=['GET'])
app.add_url_rule('/populate', 'populate',
view_func=home.populate, methods=['GET'])
# Setup examples of event hooks
app.on_pre_GET_mac += \
before_returning_items
app.on_post_GET_mac += \
after_returning_items
app.config['SWAGGER_INFO'] = {
'title': 'Macreduce API',
'version': '1.0',
'description': 'Python-Eve Framework application backend deployed on IBM '
'Bluemix that provides a practical illustration of setting '
'up a python REST API to support mobile workloads and '
'integration with 3rd party API platforms.',
'termsOfService': 'Have fun and learn!',
'contact': {
'name': 'joshisa',
'url': 'http://ibm.biz/sanjay_joshi'
},
'license': {
'name': 'Apache 2.0',
'url': 'https://github.com/ibmjstart/bluemix-python-eve-sample/'
'blob/master/LICENSE',
}
}
# Bootstrap and start Flask app within the WSGI GEvent Process
if __name__ == '__main__':
# Required to enable the Eve-docs extension
Bootstrap(app)
# Example invocation for running the Flask Server by itself
# app.run(host=APP_HOST, port=int(APP_PORT))
# Register the Flask Eve-docs blueprint
app.register_blueprint(eve_docs, url_prefix='/docs')
# Register the Swagger Extension for Eve
app.register_blueprint(swagger)
# Starting the GEvent WSGI Server to host the Flask App
# GEvent should provide superior response times to the
# dev Flask server
ws = wsgi.WSGIServer((APP_HOST, int(APP_PORT)), app)
ws.serve_forever()