diff --git a/README.md b/README.md index 350937b..a7580a7 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ This is a FastAPI application that provides a simple generic REST API service wi │ └── item.py # Item schema ├── alembic.ini # Alembic configuration ├── main.py # Application entry point -└── requirements.txt # Project dependencies +├── requirements.txt # Project dependencies +├── start.sh # Production startup script +└── supervisor.conf # Supervisor configuration ``` ## Setup and Installation @@ -77,12 +79,39 @@ PYTHONPATH=$PWD alembic upgrade head 5. Start the application: +**Development mode:** + ```bash uvicorn main:app --reload ``` The application will be available at http://127.0.0.1:8000. +**Production mode with Supervisor:** + +The application comes with a Supervisor configuration file for production deployment: + +```bash +# Make the start script executable +chmod +x start.sh + +# Copy the supervisor configuration to the supervisor config directory +cp supervisor.conf /etc/supervisor/conf.d/fastapi-app.conf + +# Restart supervisor to load the new configuration +supervisorctl reread +supervisorctl update + +# Check the status of the application +supervisorctl status app-8001 + +# View logs if needed +tail -f /var/log/supervisor/app-8001_stdout.log +tail -f /var/log/supervisor/app-8001_stderr.log +``` + +The application will be available at http://127.0.0.1:8001 when running with Supervisor. + ## API Documentation Once the application is running, you can access the automatically generated API documentation: diff --git a/main.py b/main.py index 8ece19c..233e974 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,13 @@ +import os import uvicorn from fastapi import FastAPI from app.api.api import api_router from app.core.config import settings +# Initialize database at startup +from app.db.init_db import init_db +init_db() + app = FastAPI( title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json", @@ -12,5 +17,13 @@ app = FastAPI( app.include_router(api_router, prefix=settings.API_V1_STR) +# Add startup event to ensure proper initialization +@app.on_event("startup") +async def startup_event(): + # Any additional initialization can go here + print("Application startup complete") + if __name__ == "__main__": - uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) + # Get port from environment variable or default to 8000 + port = int(os.environ.get("PORT", 8000)) + uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True) diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..855822f --- /dev/null +++ b/start.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Start script for the FastAPI application + +# Set environment variables if needed +export PYTHONPATH=/app + +# Navigate to the application directory +cd /app + +# Start the application with uvicorn +exec uvicorn main:app --host 0.0.0.0 --port 8001 --workers 4 --no-access-log \ No newline at end of file diff --git a/supervisor.conf b/supervisor.conf new file mode 100644 index 0000000..4af7204 --- /dev/null +++ b/supervisor.conf @@ -0,0 +1,14 @@ +[program:app-8001] +command=/app/start.sh +directory=/app +user=root +autostart=true +autorestart=true +startretries=5 +numprocs=1 +startsecs=1 +process_name=%(program_name)s_%(process_num)02d +stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log +stderr_logfile_maxbytes=10MB +stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log +stdout_logfile_maxbytes=10MB \ No newline at end of file