Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
server_main.py
Go to the documentation of this file.
1r"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\
5# ...............)..(.')
6# ..............(../..)
7# ...............\‍(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: server_main.py
14# CREATION DATE: 11-10-2025
15# LAST Modified: 22:21:9 14-01-2026
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: The file allowing the server to be run as a standalone.
21# // AR
22# +==== END CatFeeder =================+
23"""
24
25
26import sys
27from sys import argv
28
29try:
30 from .libs import Server, CONST, FinalClass
31except ImportError:
32 from libs import Server, CONST, FinalClass
33
34
35class Main(metaclass=FinalClass):
36 """_summary_
37 This class is a bootstrapper for launching the server in standalone mode.
38 """
39
40 def __init__(self, success: int = 0, error: int = 84) -> None:
41 self.argc = len(argv)
42 self.hosthost: str = "0.0.0.0"
43 self.port: int = 5000
44 self.success: int = success
45 self.error: int = error
46 self.app_name: str = "Asperguide"
47 self.debug: bool = False or CONST.DEBUG
48
49 def process_args(self) -> None:
50 """_summary_
51 Check the arguments that are input (if any)
52 """
53 i = 1
54 while i < self.argc:
55 arg = argv[i].lower()
56 if "--help" in arg or "-h" == arg:
57 print("Usage: python3 ./server [OPTIONS]")
58 print("Options:")
59 print(
60 " --host=<host> The host to bind the server to (default: '0.0.0.0')"
61 )
62 print(
63 " --port=<port>, -p <port> The port to bind the server to (default: 5000)"
64 )
65 print(
66 " --success=<number>, -s <number> Change the success exit code (default: 0)"
67 )
68 print(
69 " --error=<number>, -e <number> Change the error exit code (default: 1)"
70 )
71 print(
72 " --debug Enable debug mode"
73 )
74 print(
75 " --config Specify the configuration file to load when the server is started (default: config.toml)"
76 )
77 print(
78 " --env Specify the environement file to load when the server is started (default: .env)"
79 )
80 print(
81 " --help, -h Show this help message"
82 )
83 sys.exit(self.success)
84 elif "--host" in arg:
85 if '=' in arg:
86 self.hosthost = arg.split("=")[1]
87 else:
88 if i+1 < self.argc:
89 self.hosthost = argv[i + 1]
90 i += 1
91 elif "--port" in arg or '-p' in arg:
92 if '=' in arg:
93 self.port = int(arg.split("=")[1])
94 else:
95 if i+1 < self.argc:
96 self.port = int(argv[i + 1])
97 i += 1
98 elif "--success" in arg or "-s" in arg:
99 if '=' in arg:
100 self.success = int(arg.split("=")[1])
101 else:
102 if i+1 < self.argc:
103 self.success = int(argv[i + 1])
104 i += 1
105 elif "--error" in arg or "-e" in arg:
106 if '=' in arg:
107 self.error = int(arg.split("=")[1])
108 else:
109 if i+1 < self.argc:
110 self.error = int(argv[i + 1])
111 i += 1
112 elif "--debug" in arg or "-d" in arg:
113 self.debug = True
114
115 else:
116 print(f"Unknown argument: {arg}")
117 i += 1
118
119 def main(self) -> None:
120 """_summary_
121 This method is the entry point of the server.
122 """
123 if self.argc > 1:
124 self.process_args()
125 SI = Server(
126 host=self.hosthost,
127 port=self.port,
128 success=self.success,
129 error=self.error,
130 app_name=self.app_name,
131 debug=self.debug
132 )
133 try:
134 status = SI.main()
135 except KeyboardInterrupt:
136 print("\nCtrl+C caught! Exiting the program gracefully.")
137 del SI
138 status = self.success
139 except RuntimeError as e:
140 print(
141 f"A potentially handled error has forced the server to stop: {e}")
142 status = self.error
143 except Exception as e:
144 print(f"An error occurred: {e}")
145 raise RuntimeError(
146 "The server is exiting with a fatal failure"
147 ) from e
148 print(f"The server is exiting with a status of: {status}")
149 sys.exit(status)
150
151 else:
152 print(f"Usage: python3 {sys.argv[0]} --help")
153 sys.exit(self.success)
154
155
156if __name__ == "__main__":
157 MI = Main(
158 success=CONST.SUCCESS,
159 error=CONST.ERROR
160 )
161 MI.main()
None __init__(self, int success=0, int error=84)