-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
100 lines (87 loc) · 2.89 KB
/
run.py
File metadata and controls
100 lines (87 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
"""
Web Scraper API Server
This script runs the FastAPI web scraper application with configurable settings.
Use environment variables or command line arguments to configure the server.
For Render deployment:
- PORT is automatically set by Render
- Other environment variables can be set in render.yaml or the Render dashboard
"""
import argparse
import os
import logging
import uvicorn
from pathlib import Path
def parse_arguments():
"""Parse command line arguments for server configuration."""
parser = argparse.ArgumentParser(
description="Run the Web Scraper API server",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--host",
default=os.getenv("HOST", "0.0.0.0"),
help="Host interface to bind to"
)
parser.add_argument(
"--port",
type=int,
# Render sets the PORT environment variable automatically
default=int(os.getenv("PORT", "8000")),
help="Port to bind to"
)
parser.add_argument(
"--reload",
action="store_true",
default=os.getenv("RELOAD", "").lower() in ("true", "1", "yes"),
help="Enable auto-reload on code changes (development mode)"
)
parser.add_argument(
"--workers",
type=int,
default=int(os.getenv("WORKERS", "1")),
help="Number of worker processes"
)
parser.add_argument(
"--log-level",
choices=["debug", "info", "warning", "error", "critical"],
default=os.getenv("LOG_LEVEL", "info").lower(),
help="Logging level"
)
return parser.parse_args()
def setup_logging(log_level):
"""Configure basic logging."""
numeric_level = getattr(logging, log_level.upper(), None)
logging.basicConfig(
level=numeric_level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
def main():
"""Run the FastAPI application with Uvicorn."""
# Parse command line arguments
args = parse_arguments()
# Setup logging
setup_logging(args.log_level)
# Check if running in cloud environment
is_render = "RENDER" in os.environ
environment = "Render" if is_render else "development"
# Display startup message
print(f"Starting Web Scraper API on http://{args.host}:{args.port} ({environment} environment)")
print(f"API documentation available at http://{args.host}:{args.port}/docs")
# In production environments, disable reload
if is_render:
args.reload = False
# Adjust worker count based on dyno size
if not os.getenv("WORKERS"):
args.workers = 2 # Conservative default for Render free tier
# Run the server
uvicorn.run(
"app.main:app",
host=args.host,
port=args.port,
reload=args.reload,
workers=args.workers,
log_level=args.log_level
)
if __name__ == "__main__":
main()