Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import uvicorn
from app.main import app
import os
import sys
from pathlib import Path

def main():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Docstring

The main function lacks a docstring explaining its purpose and behavior. This reduces code maintainability as future developers won't understand the function's role without reading its implementation.

Suggested change
def main():
def main():
"""
Configure and start the uvicorn server with environment-specific settings.
Uses environment variables for configuration with sensible defaults.
"""
Standards
  • PEP 257
  • Clean Code

# Set project root as working directory
current_dir = Path(__file__).resolve().parent
sys.path.append(str(current_dir))

# Read environment-based settings
env = os.getenv("ENV", "development").lower()
port = int(os.getenv("PORT", 8000))
Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Error Handling

Converting port to int without error handling can crash the application if PORT environment variable contains non-numeric value. This creates a reliability issue during deployment with misconfigured environment.

Suggested change
env = os.getenv("ENV", "development").lower()
port = int(os.getenv("PORT", 8000))
try:
port = int(os.getenv("PORT", 8000))
except ValueError:
print("Error: PORT environment variable must be a number")
port = 8000
Standards
  • ISO-25010-Reliability
  • Error Handling Best Practices

reload = env == "development"
workers = int(os.getenv("UVICORN_WORKERS", 1 if reload else 4))

if __name__ == "__main__":
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8000,
reload=True,
log_level="info"
)
port=port,
reload=reload,
workers=workers,
log_level="debug" if reload else "info",
proxy_headers=True, # Trust X-Forwarded-For if behind proxy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Consideration: Setting forwarded_allow_ips="*" allows all forwarded IPs which could pose security risks in production environments. Consider restricting this to specific, trusted IP ranges or networks in production deployments.

Copy link
Collaborator Author

@arvi18 arvi18 Jul 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legit reply from a legit author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legit reply from a legit reviewer

forwarded_allow_ips="*", # Allow all forwarded IPs
timeout_keep_alive=30 # Optional tuning
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrestricted Forwarded IPs

Setting forwarded_allow_ips to '*' trusts all IP addresses in X-Forwarded-For headers. This enables IP spoofing attacks where malicious clients can forge their source IP address.

Suggested change
)
forwarded_allow_ips=os.getenv("TRUSTED_PROXIES", "127.0.0.1"), # Only trust specific proxy IPs
Standards
  • OWASP-A01
  • CWE-284


if __name__ == "__main__":

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legit review comment from a legit reviewer

main()