This project provides tools for managing FastAPI servers, including:
- An interactive CLI tool in Python for managing servers.
- An interactive PowerShell script for managing servers.
- Singleton Pattern: Ensures that only one instance of the FastAPI server is managed through the CLI.
- Interactive Management: Starts the server once, then manages its state (start, stop, view status) through commands.
- Customizable Options: Ability to specify the port and host for starting servers.
- Asynchronous Management: Uses
asynciofor asynchronous server start and stop operations (in Python CLI). - Status Tracking: Ability to view the status of the server and all its running ports.
- Collision Prevention: Uses a mutex (in the PowerShell script) to ensure that only one instance of the script can be run at a time.
- Interactive Menu: Provides a user-friendly command-line interface through PowerShell.
- Port Checking: Checks the availability of ports before starting servers (in the PowerShell script).
- Error Handling: Logs errors during script execution.
- Python 3.7+
- Windows (for the PowerShell script)
- PowerShell 5.1 or higher (for the PowerShell script)
- Installed dependencies (for Python CLI):
typeruvicornfastapipydanticloguru(or a similar logging module)
- Clone the repository (if applicable) or copy the files.
- For Python CLI (main.py):
- It is recommended to create a virtual environment.
- Install the necessary dependencies, for example, via:
pip install -r requirements.txt, or manually (ifrequirements.txtis missing)pip install typer uvicorn fastapi pydantic loguru.
- For the PowerShell script (server_manager.ps1):
- Ensure that
python.exeis installed and accessible through thePATHenvironment variable.
- Ensure that
-
Make sure the
main.py(Python CLI) file is in your working directory. -
Run the CLI using Python:
python main.py <command> [options]
-
start: Initializes and starts the FastAPI server.--port: (Optional) The port to start the server on (default: 8000).--host: (Optional) The host address for the server (default: 0.0.0.0).
Example:
python main.py start --port 8080 python main.py start --port 8081 --host 127.0.0.1
Note: If the server is already initialized, a message will be displayed, and the server cannot be reinitialized.
-
stop: Stops the FastAPI server on the specified port.--port: (Required) The port of the server to stop.
Example:
python main.py stop --port 8080
Note: You cannot stop the server if it has not been started.
-
stop-all: Stops all running FastAPI servers.Example:
python main.py stop-all
Note: You cannot stop the server if it has not been started.
-
status: Displays the status of the server and all its running ports.Example:
python main.py status
Note: This will only display information if the server has been started.
-
--help: Displays help information for the commands.Example:
python main.py --help python main.py start --help
-
Start Server: Start the server on port 8000:
python main.py start --port 8000
-
View Status: View the status of the server and its running ports:
python main.py status
-
Attempt to Restart: Attempt to start the server on a different port (e.g., 8081):
python main.py start --port 8081
A message will be displayed in the console indicating that the server has already been started.
-
Stop Server on port 8000: Stop the server on port 8000:
python main.py stop --port 8000
-
View Status after Stop: View the status of the server:
python main.py status
A message will be displayed indicating that the server is not running on port 8000.
-
Stop All Servers:
python main.py stop-all
-
View Status after Stopping All Servers:
python main.py status
A message will be displayed in the console indicating that the server has not been initialized.
-
Make sure the
server_manager.ps1(PowerShell Script) andmain.py(Python CLI) files are in the same directory or specify the correct path tomain.pyin the$pythonScriptPathvariable. -
Run PowerShell as an administrator.
-
Navigate to the directory where the
server_manager.ps1file is located. -
Execute the script:
.\server_manager.ps1
After starting the script, you will see the menu:
FastAPI Server Manager
----------------------
1. Start Server
2. Stop Server
3. Stop All Servers
4. Get Server Status
5. Exit
Select an option by entering the corresponding number (1-5) and press Enter.
-
1. Start Server:- Prompts for a port (default: 8000).
- Prompts for a host (default: 0.0.0.0).
- You can skip entering the host or port, in which case the default values will be used.
- Checks if the port is available.
- Calls the Python CLI
main.pyto start the FastAPI server.
-
2. Stop Server:- Prompts for the port of the server to stop.
- Calls the Python CLI
main.pyto stop the FastAPI server on the specified port.
-
3. Stop All Servers:- Calls the Python CLI
main.pyto stop all running FastAPI servers.
- Calls the Python CLI
-
4. Get Server Status:- Calls the Python CLI
main.pyto display the status of all running FastAPI servers.
- Calls the Python CLI
-
5. Exit:- Exits the script.
- Start Server:
- Select
1. - Enter a port, e.g.,
8080(or leave it blank to use the default port8000). - Enter a host, e.g.,
127.0.0.1(or leave it blank to use the default host0.0.0.0).
- Select
- Stop Server:
- Select
2. - Enter the port, e.g.,
8080.
- Select
- Stop All Servers
- Select
3.
- Select
- View Status:
- Select
4.
- Select
- Exit
- Select
5.
- Select
The script uses a mutex to ensure that only one instance of the script can be run at any time. If you try to run a second instance, it will exit with an error.
FastApiServer: Singleton class representing the FastAPI application and managing server startup (inmain.py).main.py: Contains the CLI commands and logic for interactive server management in Python.server_manager.ps1: PowerShell script providing an interactive interface for managing the FastAPI server through the Python CLI.typer: Library used for creating the command-line interface (inmain.py).uvicorn: ASGI web server for running the FastAPI application (inmain.py).Test-NetConnection: PowerShell cmdlet for checking ports (inserver_manager.ps1).System.Threading.Mutex: .NET class for implementing a mutex (inserver_manager.ps1).python.exe: Invokes Python to run CLI commands inmain.py(inserver_manager.ps1).
python.exe: Must be installed on the system and accessible via thePATHenvironment variable (for PowerShell).fastapi: Web framework for creating APIs (in Python).typer: Library for creating command-line interfaces (in Python).uvicorn: ASGI web server (in Python).pydantic: Library for data validation (in Python).loguru: Library for logging (in Python).
[LICENCE]