增加web_api
This commit is contained in:
117
server_waitress.py
Normal file
117
server_waitress.py
Normal file
@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
GasFlux Web API Server using Waitress WSGI server.
|
||||
|
||||
This is the production server entry point for the GasFlux Web API.
|
||||
Includes full GasFlux processing capabilities with task pool management.
|
||||
|
||||
Features:
|
||||
- File upload and processing (.xlsx, .xls)
|
||||
- Asynchronous task processing with background workers
|
||||
- Task pool management with pagination and filtering
|
||||
- Real-time task status monitoring
|
||||
- Report generation and file download
|
||||
- Health monitoring and statistics
|
||||
|
||||
API Endpoints:
|
||||
- /upload - File upload and processing initiation
|
||||
- /task/<task_id> - Task status management
|
||||
- /tasks - Task pool management (NEW)
|
||||
- /download/<filename> - File download
|
||||
- /reports - Report listing
|
||||
- /health - Health monitoring
|
||||
- /stats - System statistics
|
||||
- /config - Configuration info
|
||||
- / - Web interface
|
||||
|
||||
Usage:
|
||||
python server_waitress.py
|
||||
|
||||
Or package with PyInstaller:
|
||||
pyinstaller --onefile --name GasFluxAPI server_waitress.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the project root and src directory to PYTHONPATH
|
||||
project_root = Path(__file__).parent.absolute()
|
||||
src_dir = project_root / "src"
|
||||
|
||||
sys.path.insert(0, str(project_root))
|
||||
sys.path.insert(0, str(src_dir))
|
||||
|
||||
# Set environment variables for production
|
||||
os.environ['FLASK_APP'] = 'src/gasflux/app.py'
|
||||
os.environ['FLASK_ENV'] = 'production'
|
||||
|
||||
def main():
|
||||
"""Main entry point for the GasFlux Web API server."""
|
||||
print("Starting GasFlux Web API with Waitress...")
|
||||
|
||||
try:
|
||||
# Import the Flask app
|
||||
import gasflux.app as gasflux_app
|
||||
from src.gasflux.app import Config
|
||||
|
||||
app = gasflux_app.app
|
||||
print("✓ GasFlux app imported successfully")
|
||||
print("✓ Task pool management enabled")
|
||||
print("✓ All blueprints loaded: upload, tasks, task_pool, download, reports, health, stats, config, web")
|
||||
|
||||
# Import waitress
|
||||
from waitress import serve
|
||||
print("✓ Waitress WSGI server imported")
|
||||
|
||||
# Server configuration from environment variables
|
||||
host = Config.HOST
|
||||
port = Config.PORT
|
||||
threads = Config.THREADS
|
||||
connection_limit = Config.CONNECTION_LIMIT
|
||||
channel_timeout = Config.CHANNEL_TIMEOUT
|
||||
|
||||
print(f"Starting Waitress server on {host}:{port}")
|
||||
print(f"- Threads: {threads}")
|
||||
print(f"- Connection limit: {connection_limit}")
|
||||
print(f"- Channel timeout: {channel_timeout}s")
|
||||
print("Press Ctrl+C to stop the server")
|
||||
print("=" * 50)
|
||||
print("Available API endpoints:")
|
||||
print(" POST /upload - Upload files for processing")
|
||||
print(" GET /task/<id> - Get task status")
|
||||
print(" PUT /task/<id> - Update task status")
|
||||
print(" DEL /task/<id> - Delete task")
|
||||
print(" GET /tasks - List tasks (paginated)")
|
||||
print(" GET /tasks/stats - Task pool statistics")
|
||||
print(" GET /tasks/active - Active tasks")
|
||||
print(" GET /tasks/queue - Queued tasks")
|
||||
print(" GET /download/<file> - Download files")
|
||||
print(" GET /reports - List reports")
|
||||
print(" GET /health - Health check")
|
||||
print(" GET /stats - System stats")
|
||||
print(" GET /config - Configuration")
|
||||
print(" GET / - Web interface")
|
||||
print("=" * 50)
|
||||
print(f"Configuration: {Config.to_dict()}")
|
||||
|
||||
# Start the server with valid Waitress parameters
|
||||
serve(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
threads=threads,
|
||||
connection_limit=connection_limit,
|
||||
channel_timeout=channel_timeout,
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nServer stopped by user")
|
||||
except Exception as e:
|
||||
print(f"✗ Error starting GasFlux app: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user