Update server startup to use uvicorn - Added Python and Windows batch startup scripts with proper configuration

This commit is contained in:
Lukas Pupka-Lipinski 2025-10-06 09:14:11 +02:00
parent bf91104c7c
commit 4c9076af19
3 changed files with 153 additions and 1 deletions

63
start_server.bat Normal file
View File

@ -0,0 +1,63 @@
@echo off
:: AniWorld FastAPI Server Startup Script for Windows
:: This script activates the conda environment and starts the FastAPI server
echo Starting AniWorld FastAPI Server...
echo.
:: Check if conda is available
where conda >nul 2>nul
if %errorlevel% neq 0 (
echo Error: Conda is not available in PATH
echo Please install Anaconda/Miniconda or add conda to your PATH
pause
exit /b 1
)
:: Activate the AniWorld conda environment
echo Activating AniWorld conda environment...
call conda activate AniWorld
if %errorlevel% neq 0 (
echo Error: Failed to activate AniWorld environment
echo Please create the environment first: conda create -n AniWorld python=3.11
pause
exit /b 1
)
:: Check if uvicorn is installed
python -c "import uvicorn" 2>nul
if %errorlevel% neq 0 (
echo Error: uvicorn is not installed
echo Installing FastAPI dependencies...
pip install -r requirements.txt
if %errorlevel% neq 0 (
echo Error: Failed to install dependencies
pause
exit /b 1
)
)
:: Set default environment variables if not set
if not defined MASTER_PASSWORD (
echo Warning: MASTER_PASSWORD environment variable is not set
echo You can set it by running: set MASTER_PASSWORD=your_password
echo.
)
:: Start the FastAPI server
echo Starting FastAPI server on http://127.0.0.1:8000
echo Press Ctrl+C to stop the server
echo.
echo Available endpoints:
echo - Web Interface: http://127.0.0.1:8000
echo - API Documentation: http://127.0.0.1:8000/docs
echo - Alternative API Docs: http://127.0.0.1:8000/redoc
echo.
:: Start the server with development settings
python -m uvicorn src.server.fastapi_app:app --host 127.0.0.1 --port 8000 --reload --log-level info
:: If we get here, the server has stopped
echo.
echo Server stopped.
pause

89
start_server.py Normal file
View File

@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""
AniWorld FastAPI Server Startup Script
This script provides a convenient way to start the AniWorld FastAPI server
with proper configuration for both development and production environments.
"""
import os
import sys
import argparse
import uvicorn
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def main():
"""Main entry point for the server startup script."""
parser = argparse.ArgumentParser(description="Start AniWorld FastAPI Server")
# Server configuration arguments
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host to bind the server (default: 127.0.0.1)"
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Port to bind the server (default: 8000)"
)
parser.add_argument(
"--reload",
action="store_true",
help="Enable auto-reload for development (default: False)"
)
parser.add_argument(
"--workers",
type=int,
default=1,
help="Number of worker processes (default: 1)"
)
parser.add_argument(
"--log-level",
default="info",
choices=["debug", "info", "warning", "error", "critical"],
help="Log level (default: info)"
)
parser.add_argument(
"--env",
default="development",
choices=["development", "production"],
help="Environment mode (default: development)"
)
args = parser.parse_args()
# Configure uvicorn based on environment
if args.env == "development":
# Development configuration
uvicorn.run(
"src.server.fastapi_app:app",
host=args.host,
port=args.port,
reload=args.reload,
log_level=args.log_level,
access_log=True,
use_colors=True,
)
else:
# Production configuration
uvicorn.run(
"src.server.fastapi_app:app",
host=args.host,
port=args.port,
workers=args.workers,
log_level=args.log_level,
access_log=True,
server_header=False,
date_header=False,
)
if __name__ == "__main__":
main()

View File

@ -154,7 +154,7 @@ This document contains tasks for migrating the web application from Flask to Fas
### Server Configuration ### Server Configuration
- [ ] Update server startup to use `uvicorn` instead of Flask development server - [x] Update server startup to use `uvicorn` instead of Flask development server
- [ ] Configure production ASGI server (uvicorn, gunicorn with uvicorn workers) - [ ] Configure production ASGI server (uvicorn, gunicorn with uvicorn workers)
- [ ] Update any reverse proxy configuration (nginx, Apache) - [ ] Update any reverse proxy configuration (nginx, Apache)
- [ ] Test application startup and shutdown - [ ] Test application startup and shutdown