Skip to content

CivicMesh/backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CivicMesh Backend

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.


Overview

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.


Features

User Management

  • User registration
  • Secure password hashing using bcrypt
  • Credential verification during login

Issue and Post Management

  • 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

Image Handling

  • Upload images for posts
  • Store image data as binary in the database
  • Retrieve images by post ID

Authentication

  • HTTP Basic authentication for protected routes
  • JWT utilities included in auth.py for possible future integration

Database

  • PostgreSQL database with three main tables:
    • users
    • posts
    • images

Tech Stack

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)

Project Structure

civicmesh-backend/
│── auth.py
│── database.py
│── main.py
│── models.py
│── requirements.txt
│── README.md

main.py

Contains the core FastAPI application and routes, including:

  • User registration
  • Login handling
  • Post creation
  • Filtering and retrieving posts
  • Updating posts
  • Image upload and retrieval

auth.py

Includes:

  • JWT configuration
  • User creation under /auth/
  • Password hashing utilities
  • OAuth2PasswordBearer declaration

database.py

Defines the database connection, SQLAlchemy engine, session handling, and declarative base.

models.py

Defines SQLAlchemy ORM models for:

  • User
  • Post
  • Image

Installation and Setup

1. Clone the Repository

git clone https://github.com/your-username/civicmesh-backend.git
cd civicmesh-backend

2. Create and Activate a Virtual Environment

python3 -m venv venv
source venv/bin/activate      # macOS/Linux
venv\Scripts�ctivate         # Windows

3. Install Dependencies

pip install -r requirements.txt

4. Configure the Database

Update the PostgreSQL connection URL in database.py:

URL_DATABASE = "postgresql://username:password@host:port/dbname"

5. Run the Server

uvicorn main:app --reload

The API will run at:

http://localhost:8000

API documentation pages (generated by FastAPI):

http://localhost:8000/docs
http://localhost:8000/redoc

API Endpoints

User Endpoints

Method Endpoint Description
POST /users/ Create a new user
GET /login/ Authenticate user with username and password

Post Endpoints

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

Image Endpoints

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

Database Schema

Users Table

Field Type
id Integer (Primary Key)
first_name String
last_name String
username String (Unique)
password String (Hashed)

Posts Table

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

Images Table

Field Type
id Integer (Primary Key)
post_id Integer (Foreign Key: posts.id)
image_data Binary
image_url String

Example Requests

Create User

curl -X POST http://localhost:8000/users/ -H "Content-Type: application/json" -d '{
  "first_name": "John",
  "last_last": "Doe",
  "username": "john123",
  "password": "password123"
}'

Login

curl -X GET "http://localhost:8000/login/?username=john123&password=password123"

Create Post

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
}'

Future Improvements

  • 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

Contributors

  • Backend Development: Nagasharan Sathish, Abhaya Neupane, Yashas Suresh
  • Mobile Application Development: Nagasharan Sathish, Abhaya Neupane, Yashas Suresh

About

Backend for the civicMesh

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages