Server Main Entry Point
Overview
server_main.py is the primary entry point for running the Cat Feeder backend server in standalone mode. It provides command-line argument parsing and bootstraps the Server class.
Location: backend/src/server_main.py
Purpose
- Command-Line Interface: Parse startup arguments (host, port, debug mode)
- Server Bootstrap: Create and launch the Server instance
- Argument Validation: Validate and process CLI options
- Help System: Provide usage information
Main Class
class Main(metaclass=FinalClass):
"""Bootstrapper for launching the server in standalone mode."""
def __init__(self, success: int = 0, error: int = 84):
self.argc = len(argv)
self.host: str = "0.0.0.0"
self.port: int = 5000
self.success: int = success
self.error: int = error
self.app_name: str = "Cat Feeder"
self.debug: bool = False
def process_args(self) -> None:
"""Parse command-line arguments."""
def main(self) -> int:
"""Launch the server."""
self.process_args()
server = Server(
host=self.host,
port=self.port,
app_name=self.app_name,
debug=self.debug
)
return server.main()
Command-Line Arguments
Supported Options
| Option | Short | Description | Default |
--host=<host> | - | Server bind address | 0.0.0.0 |
--port=<port> | -p <port> | Server port | 5000 |
--success=<n> | -s <n> | Success exit code | 0 |
--error=<n> | -e <n> | Error exit code | 84 |
--debug | - | Enable debug mode | false |
--help | -h | Show help message | - |
Usage Examples
# Default settings (0.0.0.0:5000)
python -m backend.src
# Custom host and port
python -m backend.src --host=localhost --port=8000
# Short form for port
python -m backend.src -p 8080
# Debug mode
python -m backend.src --debug
# Custom exit codes
python -m backend.src --success=0 --error=1
# Show help
python -m backend.src --help
Execution Flow
- Parse Arguments:
process_args() processes CLI options
- Create Server: Instantiate
Server with parsed config
- Launch Server: Call
server.main() to start Uvicorn
- Return Exit Code: Exit with success/error code
Import Handling
The module handles both direct and package imports:
try:
from .libs import Server, CONST, FinalClass
except ImportError:
from libs import Server, CONST, FinalClass
This allows running as:
Integration with Server
def main(self) -> int:
"""Main entry point."""
self.process_args()
server = Server(
host=self.host,
port=self.port,
success=self.success,
error=self.error,
app_name=self.app_name,
debug=self.debug
)
try:
return server.main()
except KeyboardInterrupt:
print("\nServer stopped by user")
return self.success
except Exception as e:
print(f"Server error: {e}")
return self.error
Design Patterns
Final Class Pattern
Uses FinalClass metaclass to prevent inheritance:
class Main(metaclass=FinalClass):
pass
Rationale: Main is a bootstrapper that should not be extended.
Related Documentation