forked from vvbbnn00/WARP-Clash-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (66 loc) · 2.32 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
import argparse
import os
import sys
from config import PORT, HOST
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":
# Fix ./scripts/get_entrypoint.sh if it has CRLF
file = open('./scripts/get_entrypoints.sh', 'r')
data = file.read().replace('\r\n', '\n')
file.close()
file = open('./scripts/get_entrypoints.sh', 'w')
file.write(data)
file.close()
# Run ./scripts/get_entrypoint.sh
os.system("bash ./scripts/get_entrypoints.sh")
if __name__ == "__main__":
main()