diff --git a/.env.example b/.env.example index 6fa99709..38c52a3b 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,7 @@ +# Base URL for the API +FASTAPI_BASE_URL = 1. With Docker: "http://web:8000", 2. Local: "http://localhost:8000" +LOG_LEVEL = "info" + # User needs to add their Enphase API details ENPHASE_SYSTEM_ID = 'user_enphase_system_id' ENPHASE_CLIENT_ID = 'user_enphase_client_id' diff --git a/Dockerfile b/Dockerfile index 0a6482a0..05b40231 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,14 +4,26 @@ FROM python:3.11-slim # Set the working directory in the container WORKDIR /app -# Copy the entire project directory (including quartz_solar_forecast) -COPY . /app +# Install system dependencies +RUN apt-get update && apt-get install -y \ + gcc \ + && rm -rf /var/lib/apt/lists/* + +# Copy the pyproject.toml file +COPY pyproject.toml . + +# Install the project and its dependencies +RUN pip install --no-cache-dir . -# Install the quartz_solar_forecast package in editable mode -RUN pip install -e . +# Install wait-for-it script +ADD https://github.com/vishnubob/wait-for-it/raw/master/wait-for-it.sh /usr/local/bin/wait-for-it +RUN chmod +x /usr/local/bin/wait-for-it + +# Copy the entire project directory +COPY . /app -# Expose port 8000 to the outside world -EXPOSE 8000 +# Expose ports 8000 (API) and 8501 (Streamlit) to the outside world +EXPOSE 8000 8501 -# Run the application using python main.py -CMD ["python", "api/main.py"] +# The CMD will be provided by docker-compose.yml +CMD ["sh", "-c", "wait-for-it open-meteo-api:8080 -- $CMD"] \ No newline at end of file diff --git a/api/app/api.py b/api/app/api.py index 44104487..3fc5e8e5 100644 --- a/api/app/api.py +++ b/api/app/api.py @@ -28,6 +28,10 @@ allow_headers=["*"] ) +@app.get("/health") +async def root(): + return {"status": "ok"} + @app.post("/forecast/") def forecast(forecast_request: ForecastRequest): site = forecast_request.site diff --git a/dashboards/dashboard_2/app.py b/dashboards/dashboard_2/app.py index 2e758421..dc65855d 100644 --- a/dashboards/dashboard_2/app.py +++ b/dashboards/dashboard_2/app.py @@ -20,7 +20,7 @@ st.session_state.redirect_url = "" # Set up the base URL for the FastAPI server -FASTAPI_BASE_URL = "http://localhost:8000" +FASTAPI_BASE_URL = os.getenv("FASTAPI_BASE_URL") # Get the directory of the current script script_dir = os.path.dirname(os.path.abspath(__file__)) diff --git a/docker-compose.yaml b/docker-compose.yaml index 59cf7ea9..6aeed6f3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,5 @@ x-shared_environment: &shared_environment - LOG_LEVEL: ${LOG_LEVEL:-info} + LOG_LEVEL: ${LOG_LEVEL} services: open-meteo-api: @@ -28,17 +28,39 @@ services: build: context: . dockerfile: Dockerfile - container_name: fastapi-app + container_name: quartz-solar-forecast-api ports: - "8000:8000" - env_file: - - .env volumes: - .:/app - depends_on: - - open-meteo-api - - open-meteo-sync restart: always + environment: + - LOG_LEVEL=${LOG_LEVEL} + command: python api/main.py + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 40s + + streamlit: + build: + context: . + dockerfile: Dockerfile + container_name: quartz-solar-forecast-frontend + ports: + - "8501:8501" + volumes: + - .:/app + restart: always + environment: + - LOG_LEVEL=${LOG_LEVEL} + - FASTAPI_BASE_URL=${FASTAPI_BASE_URL} + command: streamlit run dashboards/dashboard_2/app.py + depends_on: + web: + condition: service_healthy volumes: - data: + data: \ No newline at end of file