-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
107 lines (65 loc) · 1.81 KB
/
app.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
#!/usr/bin/env python
"""Gourde example."""
import argparse
import flask
from gourde import Gourde
# Optional API.
try:
import flask_restplus
except ImportError:
flask_restplus = None
class Error(Exception):
"""All local errors."""
pass
# This could be as simple as :
# gourde = Gourde(app)
# app = gourde.app
# More complicated example:
gourde = Gourde(__name__)
app = gourde.app
# Override the default index
@app.route("/")
def index():
return flask.render_template("index.html")
# Add a new page.
@app.route("/example")
def example():
return flask.render_template("example.html")
# Create a custom health check callbback.
def is_healthy():
"""Custom "health" check."""
import random
if random.random() > 0.5:
raise Error()
return True
if flask_restplus:
class HelloWorld(flask_restplus.Resource):
def get(self):
return {"hello": "world"}
def initialize_api(flask_app):
"""Initialize an API."""
if not flask_restplus:
return
api = flask_restplus.Api(version="1.0", title="My Example API")
api.add_resource(HelloWorld, "/hello")
blueprint = flask.Blueprint("api", __name__, url_prefix="/api")
api.init_app(blueprint)
flask_app.register_blueprint(blueprint)
def initialize_app(flask_app, args):
"""Initialize the App."""
# Setup gourde with the args.
gourde.setup(args)
# Register a custom health check.
gourde.is_healthy = is_healthy
# Add an optional API
initialize_api(flask_app)
def main():
# Setup a custom parser.
parser = argparse.ArgumentParser(description="Example")
parser = Gourde.get_argparser(parser)
args = parser.parse_args()
initialize_app(app, args)
# Start the application.
gourde.run()
if __name__ == "__main__":
main()