A FastAPI application with PostgreSQL database support, following best practices.
- FastAPI with async/await support
- PostgreSQL database connection with SQLAlchemy
- Alembic for database migrations
- Environment-based configuration
- Health check endpoint
- Docker containerization
- Google Cloud Run deployment ready
- Proper logging and error handling
- Type hints throughout
- Python 3.9+
- PostgreSQL 12+
- pip
- Clone the repository:
git clone <repository-url>
cd hackaton-self-improving-engine- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
cp env.example .env
# Edit .env with your database credentialsImportant: Make sure to create a .env file with your database URL before running the application.
Edit the .env file with your database configuration:
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
DEBUG=True
LOG_LEVEL=INFO
HOST=0.0.0.0
PORT=8000- Create a PostgreSQL database:
createdb dbname- Run migrations:
alembic upgrade headpython main.pyOr using uvicorn directly:
uvicorn main:app --reloaduvicorn main:app --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
Build the Docker image:
docker build -t self-improving-engine-api .Run the container:
docker run -p 8080:8080 --env-file .env self-improving-engine-apiFor automatic deployments using Cloud Build triggers, see CLOUDBUILD.md.
Manual deployment:
gcloud builds submit --config=cloudbuild.yamlFor simple deployment without Cloud Build, see DEPLOYMENT.md.
Quick deployment:
chmod +x deploy.sh
./deploy.shGET /- Welcome message
GET /health- Health check endpoint with database connectivity status
Once the application is running, you can access:
- Interactive API docs:
http://localhost:8000/docs - Alternative API docs:
http://localhost:8000/redoc
alembic revision --autogenerate -m "description"alembic upgrade headalembic downgrade -1.
├── alembic/ # Alembic migration files
│ ├── versions/ # Migration versions
│ ├── env.py # Alembic environment config
│ └── script.py.mako # Migration template
├── main.py # FastAPI application entry point
├── config.py # Application configuration
├── database.py # Database connection and session management
├── requirements.txt # Python dependencies
├── alembic.ini # Alembic configuration
├── env.example # Environment variables template
├── Dockerfile # Docker configuration
├── cloudbuild.yaml # Google Cloud Build configuration
├── app.yaml # App Engine configuration (optional)
├── deploy.sh # Deployment script
├── startup.sh # Container startup script
├── DEPLOYMENT.md # Deployment documentation
├── CLOUDBUILD.md # Cloud Build configuration guide
└── README.md # This file
- Create your model in
database.pyor a separatemodels.pyfile:
from sqlalchemy import Column, Integer, String
from database import Base
class YourModel(Base):
__tablename__ = "your_table"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)- Create a migration:
alembic revision --autogenerate -m "add your_model table"- Apply the migration:
alembic upgrade headAdd your endpoints in main.py or create separate router files for better organization.
MIT