Terarea  2
The automation project
Loading...
Searching...
No Matches
server.py
Go to the documentation of this file.
7
8from display_tty import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
9from .sql import SQL
10from .bucket import Bucket
11from .actions import ActionsMain
12from .components import Endpoints, ServerPaths, RuntimeData, ServerManagement, CONST, BackgroundTasks, Crons, OAuthAuthentication, MailManagement
13from .boilerplates import BoilerplateIncoming, BoilerplateNonHTTP, BoilerplateResponses
14
15
16class Server:
17 """_summary_
18 """
19
20 def __init__(self, host: str = "0.0.0.0", port: int = 5000, success: int = 0, error: int = 84, app_name: str = "Area", debug: bool = False) -> None:
21 """_summary_
22 This is the class Server, a class that contains the structures used to allow the uvicorn and fastapi combo to run successfully.
23 Args:
24 host (str, optional): _description_. Defaults to "0.0.0.0".
25 port (int, optional): _description_. Defaults to 5000.
26 character_folder (str, optional): _description_. Defaults to "".
27 usr_db_path (str, optional): _description_. Defaults to "".
28 success (int, optional): _description_. Defaults to 0.
29 error (int, optional): _description_. Defaults to 84.
30 app_name (str, optional): _description_. Defaults to "Desktop Pets".
31 debug (bool, optional): _description_. Defaults to False.
32 """
33 # --------------------- The inherited arguments ---------------------
34 self.host: str = host
35 self.port: int = port
36 self.success: int = success
37 self.error: int = error
38 self.debug: bool = debug
39 # ------------------------ The logging function ------------------------
40 self.disp: Disp = Disp(
41 TOML_CONF,
42 FILE_DESCRIPTOR,
43 SAVE_TO_FILE,
44 FILE_NAME,
45 debug=self.debug,
46 logger=self.__class__.__name__
47 )
48 # ------------------------ Shared Runtime data ------------------------
50 host=self.host,
51 port=self.port,
52 app_name=app_name,
53 error=self.error,
54 success=self.success
55 )
56 # ----- The classes that need to be tracked for the server to run -----
57 self.runtime_data_initialised.background_tasks_initialised = BackgroundTasks(
58 success=self.success,
59 error=self.error,
60 debug=self.debug
61 )
62 self.runtime_data_initialised.crons_initialised = Crons(
64 success=self.success,
65 error=self.error,
66 debug=self.debug
67 )
68 self.disp.log_debug("Initialising database link.", "__init__")
69 self.runtime_data_initialised.database_link = SQL(
70 url=CONST.DB_HOST,
71 port=CONST.DB_PORT,
72 username=CONST.DB_USER,
73 password=CONST.DB_PASSWORD,
74 db_name=CONST.DB_DATABASE,
75 success=self.success,
76 error=self.error,
77 debug=self.debug
78 )
79 self.runtime_data_initialised.server_management_initialised = ServerManagement(
81 error=self.error,
82 success=self.success,
83 debug=self.debug
84 )
85 self.runtime_data_initialised.boilerplate_responses_initialised = BoilerplateResponses(
87 debug=self.debug
88 )
89 self.runtime_data_initialised.boilerplate_incoming_initialised = BoilerplateIncoming(
91 error=self.error,
92 success=self.success,
93 debug=self.debug
94 )
95 self.runtime_data_initialised.boilerplate_non_http_initialised = BoilerplateNonHTTP(
97 error=self.error,
98 success=self.success,
99 debug=self.debug
100 )
101 self.runtime_data_initialised.paths_initialised = ServerPaths(
103 error=self.error,
104 success=self.success,
105 debug=self.debug
106 )
107 self.runtime_data_initialised.bucket_link = Bucket(
108 error=self.error,
109 success=self.success,
110 debug=self.debug
111 )
112 self.runtime_data_initialised.endpoints_initialised = Endpoints(
114 error=self.error,
115 success=self.success,
116 debug=self.debug
117 )
118 self.runtime_data_initialised.oauth_authentication_initialised = OAuthAuthentication(
120 success=success,
121 error=error,
122 debug=debug
123 )
124 self.runtime_data_initialised.actions_main_initialised = ActionsMain(
126 error=self.error,
127 success=self.success,
128 debug=self.debug
129 )
130 self.runtime_data_initialised.mail_management_initialised = MailManagement(
131 error=self.error,
132 success=self.success,
133 debug=self.debug
134 )
135
136 def __del__(self) -> None:
137 """_summary_
138 The destructor of the class.
139 """
140 self.disp.log_info("The server is shutting down.", "__del__")
141 self.stop_server()
142
143 def main(self) -> int:
144 """_summary_
145 The main function of the server.
146 This is the one in charge of starting the server.
147
148 Returns:
149 int: _description_
150 """
151 self.runtime_data_initialised.server_management_initialised.initialise_classes()
152 self.runtime_data_initialised.paths_initialised.load_default_paths_initialised()
153 self.runtime_data_initialised.paths_initialised.inject_routes()
154 self.runtime_data_initialised.crons_initialised.inject_crons()
155 status = self.runtime_data_initialised.background_tasks_initialised.safe_start()
156 if status != self.success:
157 self.disp.log_error(
158 "Error: background tasks failed to start.",
159 "main"
160 )
161 return status
162 try:
163 self.runtime_data_initialised.server.run()
164 except Exception as e:
165 self.disp.log_error(f"Error: {e}", "main")
166 return self.error
167 return self.success
168
169 def is_running(self) -> bool:
170 """_summary_
171 The function in charge of checking if the server is running.
172
173 Returns:
174 bool: _description_: Returns True if the server is running.
175 """
176 return self.runtime_data_initialised.server_management_initialised.is_server_running()
177
178 def stop_server(self) -> None:
179 """_summary_
180 The function in charge of stopping the server.
181 """
182 title = "stop_server"
183 self.disp.log_info("Stopping server", title)
184 if self.runtime_data_initialised.server_management_initialised is not None:
185 del self.runtime_data_initialised.server_management_initialised
186 self.runtime_data_initialised.server_management_initialised = None
187 if self.runtime_data_initialised.crons_initialised is not None:
188 del self.runtime_data_initialised.crons_initialised
189 self.runtime_data_initialised.crons_initialised = None
190 self.disp.log_info("Server stopped", title)
None stop_server(self)
Definition server.py:178
None __init__(self, str host="0.0.0.0", int port=5000, int success=0, int error=84, str app_name="Area", bool debug=False)
Definition server.py:20
RuntimeData runtime_data_initialised
Definition server.py:49
None __del__(self)
Definition server.py:136
bool is_running(self)
Definition server.py:169