forked from davidjsanders/python_cowbull_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (63 loc) · 2.74 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
import logging
# Import python_cowbull_server initialization package
# ---------------------------------------------------
# Import the app from the python_cowbull_server package, which will also
# execute_load the __init__.py for the package. In effect, this allows sets up
# key variables, configuration, and other general settings and also
# defines the Flask app there, making it importable everywhere without
# circular references.
from python_cowbull_server import app, error_handler
# Import the Flask controllers for this app
# -----------------------------------------
# This enables request handlers to be defined outside of app.py making them
# more object-oriented and easier to manage and maintain.
from flask_controllers import GameServerController, HealthCheck, Readiness, GameModes
# Import the Flask routes
# -----------------------
# Currently, there is only a V1, but you get the idea if there were a V2.
from Routes.V1 import V1
# Add cross origin scripting support
# ----------------------------------
@app.after_request
def allow_cors(resp):
resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
return resp
# Version 1 routes
# ----------------
# Setup the version 1 routes.
v1 = V1(error_handler=error_handler, app=app)
v1.game(controller=GameServerController)
error_handler.log(message="Added route v1.game", logger=logging.info)
v1.modes(controller=GameModes)
error_handler.log(message="Added route v1.modes", logger=logging.info)
v1.health(controller=HealthCheck)
error_handler.log(message="Added route v1.health", logger=logging.info)
v1.readiness(controller=Readiness)
error_handler.log(message="Added route v1.readiness", logger=logging.info)
# DEV USE ONLY! Built-in Web Server
#----------------------------------
# If app.py has been run using Python, initiate a Flask object; otherwise,
# any caller (e.g. uWSGI or gunicorn) should initiate workers and routes.
# The code below is useful because it allows changes to be checked using
# Flask's built-in webserver (before building a Docker image) simply by
# running "python app.py". If using this method, remember:
#
# 1. instruct the app on how to find redis by setting environment variables
# (see python_cowbull_server/__init__.py)
# 2. do not use for production workloads! The Flask webserver is suitable
# for dev / test workloads only.
#
if __name__ == "__main__":
error_handler.log(
module="app.py",
method="__main__",
message="Running application via Flask built-in server.",
logger=logging.info
)
app.run\
(
host=app.config["FLASK_HOST"],
port=int(app.config["FLASK_PORT"]),
debug=app.config["FLASK_DEBUG"]
)