A production-ready FastAPI application with PostgreSQL database, JWT authentication, and user/post management.
- ✅ User registration and authentication
- ✅ JWT token-based authorization
- ✅ Post creation, reading, updating, and deletion
- ✅ Vote system for posts
- ✅ PostgreSQL database with Alembic migrations
- ✅ CORS middleware enabled
- ✅ Password hashing with bcrypt
- Python 3.9+
- PostgreSQL database
-
Clone the repository
-
Create a virtual environment:
python -m venv .venv .venv\Scripts\activate # Windows source .venv/bin/activate # Linux/Mac
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile based on.env.example:cp .env.example .env
-
Update
.envwith your database credentials and secret key -
Run database migrations:
alembic upgrade head
-
Start the development server:
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
Set the following environment variables in your Render dashboard:
DATABASE_HOSTNAME- Your PostgreSQL hostDATABASE_PORT- Database port (usually 5432)DATABASE_PASSWORD- Database passwordDATABASE_NAME- Database nameDATABASE_USERNAME- Database usernameSECRET_KEY- A strong random string for JWT (generate with:openssl rand -hex 32)ALGORITHM- HS256ACCESS_TOKEN_EXPIRE_MINUTES- 30
- Connect your GitHub repository to Render
- Create a new Web Service
- Set the build command:
pip install -r requirements.txt - Set the start command:
uvicorn app.main:app --host=0.0.0.0 --port=8000 - Add environment variables
- Create a PostgreSQL database in Render and link it
- Deploy!
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
.
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI app and routes
│ ├── models.py # Database models
│ ├── schemas.py # Pydantic schemas
│ ├── database.py # Database connection
│ ├── config.py # Configuration settings
│ ├── oauth2.py # JWT authentication
│ ├── utils.py # Password hashing utilities
│ └── routers/ # API route modules
│ ├── auth.py
│ ├── post.py
│ ├── user.py
│ └── vote.py
├── alembic/ # Database migrations
├── requirements.txt # Python dependencies
├── Procfile # Render deployment config
└── .env.example # Environment variables template
MIT