Skip to content

Commit

Permalink
feat(api): use PORT from environment variables (#252)
Browse files Browse the repository at this point in the history
The application now tries to get the PORT from the environment variables.
If it's not found, it logs an info message and uses the default PORT=80.
If the PORT is not an integer, it raises a ValueError.
  • Loading branch information
failandimprove1 authored and antopiahk committed May 15, 2024
1 parent c88b262 commit 9c7a2c3
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/api/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os
import uvicorn
import logging

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -61,12 +64,25 @@ def create_app() -> FastAPI:
)
return app


def run():
app = create_app()
sandbox_app = sandbox.create_app()
app.mount("/sandbox", sandbox_app)

uvicorn.run(app, host="0.0.0.0", port=8000, log_config="logging.yaml")
try:
PORT = os.environ.get("PORT")
if not PORT:
logging.info(
"PORT not found in environment variables. Using default PORT=80"
)
PORT = 80

PORT = int(PORT)
except Exception:
raise ValueError("PORT is not an integer")

uvicorn.run(app, host="0.0.0.0", port=PORT, log_config="logging.yaml")


if __name__ == "__main__":
Expand Down

0 comments on commit 9c7a2c3

Please sign in to comment.