From 9162f601e00eb9e79667a1cb712d2347ab4b3d9f Mon Sep 17 00:00:00 2001 From: sstent Date: Sun, 28 Sep 2025 13:51:59 -0700 Subject: [PATCH] added alembic database migrations, json import/export --- Dockerfile | 13 +++++++++++-- main.py | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c471ea2..ecb78f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,6 +28,9 @@ FROM python:3.11-slim RUN apt-get update && apt-get install -y \ && rm -rf /var/lib/apt/lists/* +# Create a non-root user +RUN groupadd -r appuser && useradd -r -g appuser appuser + # Set working directory WORKDIR /app @@ -40,8 +43,14 @@ ENV PATH="/app/venv/bin:$PATH" # Copy application code COPY . . +# Change ownership of the app directory +RUN chown -R appuser:appuser /app + +# Switch to non-root user +USER appuser + # Expose port (as defined in main.py) EXPOSE 8999 -# Run the application with uvicorn -CMD ["uvicorn", "main:app", "--host", "0.0.0", "--port", "8999"] \ No newline at end of file +# Run the application with uvicorn - fix host to 0.0.0.0 +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8999"] \ No newline at end of file diff --git a/main.py b/main.py index f5f71c0..7740c1b 100644 --- a/main.py +++ b/main.py @@ -36,7 +36,14 @@ DATABASE_URL = f"sqlite:///{DATABASE_PATH}/meal_planner.db" logging.info(f"Database URL: {DATABASE_URL}") # Ensure the database directory exists -os.makedirs(DATABASE_PATH, exist_ok=True) +logging.info(f"Creating database directory at: {DATABASE_PATH}") +try: + os.makedirs(DATABASE_PATH, exist_ok=True) + logging.info(f"Database directory created successfully") +except Exception as e: + logging.error(f"Failed to create database directory: {e}") + raise + # For production, use PostgreSQL: DATABASE_URL = "postgresql://username:password@localhost/meal_planner" engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {}) @@ -47,6 +54,14 @@ Base = declarative_base() app = FastAPI(title="Meal Planner") templates = Jinja2Templates(directory="templates") +# Get the port from environment variable or default to 8999 +PORT = int(os.getenv("PORT", 8999)) + +# This will be called if running directly with Python +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=PORT) + # Database Models class Food(Base): __tablename__ = "foods"