Terarea  2
The automation project
Loading...
Searching...
No Matches
server_main.py
Go to the documentation of this file.
1"""
2The file allowing the server to be run as a standalone.
3"""
4
5import sys
6from sys import argv
7
8try:
9 from .lib import Server, CONST
10except ImportError:
11 from lib import Server, CONST
12
13
14class Main:
15 """_summary_
16 This class is a bootstrapper for launching the server in standalone mode.
17 """
18
19 def __init__(self, success: int = 0, error: int = 84) -> None:
20 self.argc = len(argv)
21 self.hosthost: str = "0.0.0.0"
22 self.port: int = 5000
23 self.success: int = success
24 self.error: int = error
25 self.app_name: str = "Area"
26 self.debug: bool = False
27
28 def process_args(self) -> None:
29 """_summary_
30 Check the arguments that are input (if any)
31 """
32 i = 1
33 while i < self.argc:
34 arg = argv[i].lower()
35 if "--help" in arg or "-h" == arg:
36 print("Usage: python3 ./server [OPTIONS]")
37 print("Options:")
38 print(
39 " --host=<host> The host to bind the server to (default: '0.0.0.0')"
40 )
41 print(
42 " --port=<port>, -p <port> The port to bind the server to (default: 5000)"
43 )
44 print(
45 " --success=<number>, -s <number> Change the success exit code (default: 0)"
46 )
47 print(
48 " --error=<number>, -e <number> Change the error exit code (default: 1)"
49 )
50 print(
51 " --debug Enable debug mode"
52 )
53 print(
54 " --help, -h Show this help message"
55 )
56 sys.exit(self.success)
57 elif "--host" in arg:
58 if '=' in arg:
59 self.hosthost = arg.split("=")[1]
60 else:
61 self.hosthost = argv[i + 1]
62 i += 1
63 elif "--port" in arg or '-p' in arg:
64 if '=' in arg:
65 self.port = int(arg.split("=")[1])
66 else:
67 self.port = int(argv[i + 1])
68 i += 1
69 elif "--success" in arg or "-s" in arg:
70 if '=' in arg:
71 self.success = int(arg.split("=")[1])
72 else:
73 self.success = int(argv[i + 1])
74 i += 1
75 elif "--error" in arg or "-e" in arg:
76 if '=' in arg:
77 self.error = int(arg.split("=")[1])
78 else:
79 self.error = int(argv[i + 1])
80 i += 1
81 elif "--debug" in arg or "-d" in arg:
82 self.debug = True
83
84 else:
85 print(f"Unknown argument: {arg}")
86 i += 1
87
88 def main(self) -> None:
89 """_summary_
90 This method is the entry point of the server.
91 """
92 if self.argc > 1:
93 self.process_args()
94 SI = Server(
95 host=self.hosthost,
96 port=self.port,
97 success=self.success,
98 error=self.error,
99 app_name=self.app_name,
100 debug=self.debug
101 )
102 try:
103 status = SI.main()
104 except KeyboardInterrupt:
105 print("\nCtrl+C caught! Exiting the program gracefully.")
106 del SI
107 status = self.success
108 except Exception as e:
109 print(f"An error occurred: {e}")
110 status = self.error
111 print(f"The server is exiting with a status of: {status}")
112 sys.exit(status)
113
114 else:
115 print("Usage: python3 ./server --help")
116 sys.exit(self.success)
117
118
119if __name__ == "__main__":
120 MI = Main(
121 success=CONST.SUCCESS,
122 error=CONST.ERROR
123 )
124 MI.main()
None process_args(self)
None __init__(self, int success=0, int error=84)