-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
app.py
94 lines (74 loc) · 2.8 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
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses>.
"""
import argparse
import sys
from config import PORT, HOST
from utils.entrypoints import optimizeEntrypoints
from utils.logger import createLogger
def linuxStartWeb():
"""
Start web service on linux
:return:
"""
from gunicorn.app.base import BaseApplication
from services.web_service import createApp
class FlaskGunicornApp(BaseApplication):
def __init__(self, flaskapp, options=None):
self.options = options or {}
self.application = flaskapp
super().__init__()
def load_config(self):
config = {key: value for key, value in self.options.items() if
key in self.cfg.settings and value is not None}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
app = createApp("web")
FlaskGunicornApp(app, options={
"bind": f"{HOST}:{PORT}",
"workers": 4,
"worker_connections": 1000,
"timeout": 30,
"keepalive": 2
}).run()
def main():
"""
Main function
:return:
"""
parser = argparse.ArgumentParser(description="WARP Clash API")
parser.add_argument("command", choices=["web", "background", "optimize"], help="Command to run")
args = parser.parse_args()
if args.command == "web":
logger = createLogger("app_web")
from services.web_service import createApp
app = createApp("web", logger=logger)
# If windows, use app.run()
if sys.platform == "win32":
app.run(host=HOST, port=PORT)
# If linux, use gunicorn
else:
linuxStartWeb()
elif args.command == "background":
logger = createLogger("app_background")
from services.scheduled_service import main
main(logger=logger)
elif args.command == "optimize":
# Windows is not supported for this command
if sys.platform == 'win32':
print('This command is not supported on Windows.')
return
optimizeEntrypoints()
if __name__ == "__main__":
main()