FastAPI • PostgreSQL • SQLAlchemy
This repository contains the backend API for CivicMesh, a community-driven mobile application developed to help residents report local issues, volunteer to resolve them, and track progress. The backend provides user authentication, issue management, image uploads, and database operations.
The backend is built using FastAPI, with SQLAlchemy for ORM and PostgreSQL as the database. It exposes RESTful API endpoints that support:
- User creation and authentication
- Issue (post) creation and retrieval
- Filtering and updating posts
- Image upload and retrieval
- Basic authentication for protected routes
This API is intended for use with a React Native mobile frontend.
- User registration
- Secure password hashing using bcrypt
- Credential verification during login
- Create new posts (community issues)
- Retrieve posts by user, category, active status, or ID
- Update existing posts
- Support for geolocation fields (latitude and longitude)
- Automatic timestamp generation
- Upload images for posts
- Store image data as binary in the database
- Retrieve images by post ID
- HTTP Basic authentication for protected routes
- JWT utilities included in
auth.pyfor possible future integration
- PostgreSQL database with three main tables:
userspostsimages
| Component | Technology |
|---|---|
| Backend Framework | FastAPI |
| ORM | SQLAlchemy |
| Database | PostgreSQL |
| Authentication | HTTP Basic + bcrypt |
| Image Storage | PostgreSQL (LargeBinary) |
| Dependencies | Listed in requirements.txt |
| Deployment | Render (CI/CD) |
civicmesh-backend/
│── auth.py
│── database.py
│── main.py
│── models.py
│── requirements.txt
│── README.md
Contains the core FastAPI application and routes, including:
- User registration
- Login handling
- Post creation
- Filtering and retrieving posts
- Updating posts
- Image upload and retrieval
Includes:
- JWT configuration
- User creation under
/auth/ - Password hashing utilities
- OAuth2PasswordBearer declaration
Defines the database connection, SQLAlchemy engine, session handling, and declarative base.
Defines SQLAlchemy ORM models for:
- User
- Post
- Image
git clone https://github.com/your-username/civicmesh-backend.git
cd civicmesh-backendpython3 -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts�ctivate # Windowspip install -r requirements.txtUpdate the PostgreSQL connection URL in database.py:
URL_DATABASE = "postgresql://username:password@host:port/dbname"uvicorn main:app --reloadThe API will run at:
http://localhost:8000
API documentation pages (generated by FastAPI):
http://localhost:8000/docs
http://localhost:8000/redoc
| Method | Endpoint | Description |
|---|---|---|
| POST | /users/ |
Create a new user |
| GET | /login/ |
Authenticate user with username and password |
| Method | Endpoint | Description |
|---|---|---|
| POST | /posts/ |
Create a new post |
| GET | /posts/all/ |
Retrieve all posts |
| GET | /posts/{post_id} |
Retrieve a specific post |
| GET | /posts/by_user/{user_id} |
Retrieve posts created by a specific user |
| GET | /posts/category/{category} |
Retrieve posts matching a specific category |
| GET | /posts/active/ |
Retrieve all active posts |
| PUT | /posts/{post_id} |
Update an existing post |
| Method | Endpoint | Description |
|---|---|---|
| POST | /upload-image/{post_id} |
Upload an image for a post |
| GET | /image/{post_id} |
Fetch the image associated with a post |
| Field | Type |
|---|---|
| id | Integer (Primary Key) |
| first_name | String |
| last_name | String |
| username | String (Unique) |
| password | String (Hashed) |
| Field | Type |
|---|---|
| id | Integer (Primary Key) |
| title | String |
| body | Text |
| user_id | Integer (Foreign Key: users.id) |
| category | String |
| subcategory | String |
| created_at | DateTime |
| longitude | Float |
| latitude | Float |
| image_url | String |
| is_active | Boolean |
| Field | Type |
|---|---|
| id | Integer (Primary Key) |
| post_id | Integer (Foreign Key: posts.id) |
| image_data | Binary |
| image_url | String |
curl -X POST http://localhost:8000/users/ -H "Content-Type: application/json" -d '{
"first_name": "John",
"last_last": "Doe",
"username": "john123",
"password": "password123"
}'curl -X GET "http://localhost:8000/login/?username=john123&password=password123"curl -X POST http://localhost:8000/posts/ -H "Content-Type: application/json" -d '{
"title": "Broken streetlight",
"body": "Streetlight near Block C is not working.",
"user_id": 1,
"category": "Infrastructure",
"longitude": 42.123,
"latitude": -93.456
}'- Implement full JWT authentication
- Add role-based access control (admin, volunteer, resident)
- Migrate image storage to AWS S3 or Firebase Storage
- Add notification support for post updates
- Integrate WebSocket-based real-time updates
- Backend Development: Nagasharan Sathish, Abhaya Neupane, Yashas Suresh
- Mobile Application Development: Nagasharan Sathish, Abhaya Neupane, Yashas Suresh