mirror of
https://github.com/sstent/foodplanner.git
synced 2026-01-25 11:11:36 +00:00
added alembic database migrations, json import/export
This commit is contained in:
13
Dockerfile
13
Dockerfile
@@ -28,6 +28,9 @@ FROM python:3.11-slim
|
|||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Create a non-root user
|
||||||
|
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
@@ -40,8 +43,14 @@ ENV PATH="/app/venv/bin:$PATH"
|
|||||||
# Copy application code
|
# Copy application code
|
||||||
COPY . .
|
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 port (as defined in main.py)
|
||||||
EXPOSE 8999
|
EXPOSE 8999
|
||||||
|
|
||||||
# Run the application with uvicorn
|
# Run the application with uvicorn - fix host to 0.0.0.0
|
||||||
CMD ["uvicorn", "main:app", "--host", "0.0.0", "--port", "8999"]
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8999"]
|
||||||
15
main.py
15
main.py
@@ -36,7 +36,14 @@ DATABASE_URL = f"sqlite:///{DATABASE_PATH}/meal_planner.db"
|
|||||||
logging.info(f"Database URL: {DATABASE_URL}")
|
logging.info(f"Database URL: {DATABASE_URL}")
|
||||||
|
|
||||||
# Ensure the database directory exists
|
# Ensure the database directory exists
|
||||||
|
logging.info(f"Creating database directory at: {DATABASE_PATH}")
|
||||||
|
try:
|
||||||
os.makedirs(DATABASE_PATH, exist_ok=True)
|
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"
|
# 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 {})
|
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")
|
app = FastAPI(title="Meal Planner")
|
||||||
templates = Jinja2Templates(directory="templates")
|
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
|
# Database Models
|
||||||
class Food(Base):
|
class Food(Base):
|
||||||
__tablename__ = "foods"
|
__tablename__ = "foods"
|
||||||
|
|||||||
Reference in New Issue
Block a user