-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
87 lines (72 loc) · 2.81 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
import argparse
import orjson
import secrets
import os
import flask
from server.bp import bp
from server.website import Website
from server.backend import Backend_Api
from server.babel import create_babel
from flask_compress import Compress
from flask_caching import Cache
config = orjson.loads(open('config.json', 'r').read())
site_config = config['site_config']
url_prefix = config.pop('url_prefix')
app = flask.Flask(__name__)
compress = Compress(app)
app.secret_key = secrets.token_hex(16)
cache = Cache(app, config={'CACHE_TYPE': 'redis', 'CACHE_REDIS_HOST': 'localhost', 'CACHE_REDIS_PORT': 6379, 'CACHE_DEFAULT_TIMEOUT': 10})
create_babel(app)
site = Website(bp, url_prefix)
for route in site.routes:
bp.add_url_rule(
route,
view_func=site.routes[route]['function'],
methods=site.routes[route]['methods'],
)
backend_api = Backend_Api(bp, config)
for route in backend_api.routes:
bp.add_url_rule(
route,
view_func=backend_api.routes[route]['function'],
methods=backend_api.routes[route]['methods'],
)
app.register_blueprint(bp, url_prefix=url_prefix)
def run():
print(f"Running on {site_config['port']}{url_prefix}")
app.run(**site_config)
print(f"Closing port {site_config['port']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Start the server")
parser.add_argument("-p", "--port", type=int, default=site_config["port"],
help="Port number (default: %(default)d)")
parser.add_argument("-b", "--host", default="0.0.0.0",
help="Host address (default: %(default)s)")
arguments = parser.parse_args()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Start the server")
parser.add_argument("-p", "--port", type=int, default=site_config["port"],
help="Port number (default: %(default)d)")
parser.add_argument("-b", "--host", default="0.0.0.0",
help="Host address (default: %(default)s)")
arguments = parser.parse_args()
if "GUNICORN_ARGV" in dict(os.environ):
run_with_gunicorn = True
os.environ.pop("GUNICORN_ARGV")
port = int(os.getenv("PORT", site_config["port"]))
host = os.getenv("HOST", "0.0.0.0")
else:
run_with_gunicorn = False
if run_with_gunicorn:
print("Running with Gunicorn...")
print(f"Host: {host}")
print(f"Port: {port}")
from gunicorn.app.wsgiapp import WSGIApplication
options = {
'bind': f'{host}:{port}',
'workers': 2,
'reload': False,
}
WSGIApplication(app).run(**options)
else:
app.run(**arguments.__dict__)