-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (124 loc) · 4.4 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import sys
PROJECT_ROOT = os.path.abspath(
".."
if os.path.abspath(".").split("/")[-1]
in [
"lib",
"api",
"helpers",
"scripts",
"tests",
"extensions",
"docs",
"frontend",
]
else "."
)
sys.path.append(PROJECT_ROOT)
import json
import flask
import base64
import logging
from api import user, uploader, render, posts, static_files, storage
from helpers import formatter
from extensions import database, cache
from typing import NoReturn, Union
logger: Union[logging.Logger, None] = None
def create_app(config: object, mysql_config: object) -> flask.app:
app = flask.Flask(
__name__,
)
# The database extension
database.DataBase(mysql_config, app)
# Enables the cache extension
cache.Cache(config, app)
app.cache.create_store("signup")
app.cache.create_store("uploads")
# Register blueprints
app.register_blueprint(user.blueprint)
app.register_blueprint(uploader.blueprint)
app.register_blueprint(render.blueprint)
app.register_blueprint(posts.blueprint)
app.register_blueprint(static_files.blueprint)
app.register_blueprint(storage.blueprint)
# Initiate logging
app.logger = logger
app.config.update(config.config)
if "SECRET_KEY" not in config.config:
app.config["SECRET_KEY"] = "bg6/X`k!`-|iyh,?fbms,z0034VSjH5g"
logger.warn(
"Secret key not specified in config file. Using default security key."
+ " This is very dangerous in production mode"
)
return app
def run_development_server(config: object, mysql: object) -> NoReturn:
app = create_app(config, mysql)
try:
app.run(
config.HOST,
int(config.PORT),
)
except KeyboardInterrupt:
logger.info("Server shutting down...")
except SystemExit:
app.cache._invalidator.kill()
raise
def run_production_server(config: object, mysql: object) -> NoReturn:
app = create_app(config, mysql)
if sys.platform == "win32":
from gevent.pywsgi import WSGIServer
server = WSGIServer((config.HOST, int(config.PORT)), app)
try:
logger.info(
"Starting server on {}://{}:{}".format("http", config.HOST, config.PORT)
)
server.serve_forever()
except KeyboardInterrupt:
logger.info("Server shutting down...")
else:
from meinheld import server
logger.warning(
"This is not the recommended way to go with on non-windows systems. "
"Please use gunicorn instead"
)
server.listen(
(config.HOST, int(config.PORT)),
)
try:
logger.info(
"Starting server on {}://{}:{}".format("http", config.HOST, config.PORT)
)
server.run(app)
except KeyboardInterrupt:
logger.info("Server shutting down...")
def deserialize(encoded: str) -> object:
class EmtpyClass:
pass
cls = EmtpyClass()
decoded = json.loads(base64.b64decode(encoded.encode()).decode("utf-8"))
for i in decoded:
setattr(cls, i, decoded[i])
return cls
def main() -> NoReturn:
global logger
if "--log-file" in sys.argv:
formatter.init(PROJECT_ROOT, sys.argv[sys.argv.index("--log-file") + 1])
if "--flask-config" not in sys.argv or "--mysql-config" not in sys.argv:
logger.error("Flask and mysql config are not specified in commandline.")
logger = logging.getLogger("server")
flask_config = deserialize(sys.argv[sys.argv.index("--flask-config") + 1])
mysql_config = deserialize(sys.argv[sys.argv.index("--mysql-config") + 1])
if flask_config.MODE == "development":
run_development_server(flask_config, mysql_config)
else:
run_production_server(flask_config, mysql_config)
if __name__ == "__main__":
# If the functions were called as modules, then screen is already initialised
# but if the code reaches here, it means that it is not initialised.
from helpers import init_console, exception
exception.init()
init_console.init()
main()
# Code shouldn't reach here if it is being run as a script
logger: logging.Logger = logging.getLogger("server")