Allow configuring FastAPI port via environment

This commit is contained in:
Alexa Amundson
2025-11-16 02:11:36 -06:00
parent a32601cf72
commit ffbc7eb03f
6 changed files with 14 additions and 7 deletions

View File

@@ -284,7 +284,7 @@ async def serve_frontend():
```bash ```bash
cd backend cd backend
docker-compose up -d # Start PostgreSQL + Redis docker-compose up -d # Start PostgreSQL + Redis
python run.py # Start FastAPI server on :8000 python run.py # Start FastAPI server (defaults to :8000 unless PORT is set)
``` ```
2. **Access UI:** 2. **Access UI:**

View File

@@ -56,9 +56,10 @@ docker run -d -p 6379:6379 redis:7-alpine
### 4. Run Application ### 4. Run Application
```bash ```bash
python run.py # PORT defaults to 8000 if not set
python run.py # respects the PORT env variable
# Or using uvicorn directly: # Or using uvicorn directly:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 uvicorn app.main:app --reload --host 0.0.0.0 --port ${PORT:-8000}
``` ```
Access the API: Access the API:

View File

@@ -23,4 +23,4 @@ COPY . .
EXPOSE 8000 EXPOSE 8000
# Run application # Run application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"]

View File

@@ -1,5 +1,7 @@
# BlackRoad OS Backend Makefile # BlackRoad OS Backend Makefile
PORT ?= 8000
.PHONY: help install dev run test clean docker-build docker-up docker-down .PHONY: help install dev run test clean docker-build docker-up docker-down
help: help:
@@ -23,7 +25,7 @@ dev:
python run.py python run.py
run: run:
uvicorn app.main:app --host 0.0.0.0 --port 8000 uvicorn app.main:app --host 0.0.0.0 --port $(PORT)
test: test:
pytest -v pytest -v

View File

@@ -76,7 +76,7 @@ nano .env
docker run -d -p 5432:5432 -e POSTGRES_USER=blackroad -e POSTGRES_PASSWORD=password -e POSTGRES_DB=blackroad_db postgres:15-alpine docker run -d -p 5432:5432 -e POSTGRES_USER=blackroad -e POSTGRES_PASSWORD=password -e POSTGRES_DB=blackroad_db postgres:15-alpine
docker run -d -p 6379:6379 redis:7-alpine docker run -d -p 6379:6379 redis:7-alpine
# Run the application # Run the application (PORT defaults to 8000 if unset)
python run.py python run.py
``` ```

View File

@@ -1,11 +1,15 @@
"""Run the application""" """Run the application"""
import os
import uvicorn import uvicorn
if __name__ == "__main__": if __name__ == "__main__":
port = int(os.getenv("PORT", "8000"))
uvicorn.run( uvicorn.run(
"app.main:app", "app.main:app",
host="0.0.0.0", host="0.0.0.0",
port=8000, port=port,
reload=True, reload=True,
log_level="info" log_level="info"
) )