An open API service indexing awesome lists of open source software.

https://github.com/nobokumar/spotsync-server


https://github.com/nobokumar/spotsync-server

Last synced: 25 days ago
JSON representation

Awesome Lists containing this project

README

          

# ๐Ÿš— SpotSync API

Smart Parking & EV Charging Reservation System built with Go, Echo, GORM, PostgreSQL, and JWT authentication.

## ๐ŸŒ Live Demo

* **Backend API:** https://spotsync-server-285t.onrender.com
---

## ๐Ÿ“Œ Project Overview

SpotSync is a centralized parking management platform designed for busy airports and malls. The system supports regular parking and EV charging reservations while ensuring that parking zones never exceed their capacity, even under concurrent booking requests.

The project implements Clean Architecture principles and uses database transactions with row-level locking (`FOR UPDATE`) to prevent race conditions during reservations.

---

## โœจ Features

### ๐Ÿ” Authentication

* User registration
* User login
* JWT authentication
* Password hashing with bcrypt
* Role-based authorization

### ๐Ÿš— Parking Zones

* Create parking zones (Admin)
* View all parking zones
* View single parking zone
* Dynamic available spot calculation

### ๐Ÿ“… Reservations

* Reserve parking spots
* View personal reservations
* Cancel reservations
* View all reservations (Admin)
* Concurrency-safe booking system using transactions and row locking

---

## ๐Ÿ›  Tech Stack

| Technology | Usage |
| ---------- | ------------------ |
| Go 1.24+ | Backend language |
| Echo | HTTP framework |
| GORM | ORM |
| PostgreSQL | Database |
| JWT | Authentication |
| bcrypt | Password hashing |
| Validator | Request validation |

---

## ๐Ÿ— Domain Driven Design

The project follows Domain Driven Design principles:

```text
SpotSync-server/
โ”‚
โ”œโ”€โ”€ cmd/
โ”‚ โ””โ”€โ”€ main.go
โ”‚
โ”œโ”€โ”€ auth/
โ”‚ โ””โ”€โ”€ jwt.go
โ”‚
โ”œโ”€โ”€ config/
โ”‚ โ””โ”€โ”€ config.go
โ”‚
โ”œโ”€โ”€ database/
โ”‚ โ””โ”€โ”€ postgres.go
โ”‚
โ”œโ”€โ”€ domain/
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ users/
โ”‚ โ”‚ โ”œโ”€โ”€ dto/
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ request.go
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ response.go
โ”‚ โ”‚ โ”œโ”€โ”€ entity.go # User model
โ”‚ โ”‚ โ”œโ”€โ”€ repository.go
โ”‚ โ”‚ โ”œโ”€โ”€ service.go
โ”‚ โ”‚ โ”œโ”€โ”€ handler.go
โ”‚ โ”‚ โ””โ”€โ”€ routes.go
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ parking_zones/
โ”‚ โ”‚ โ”œโ”€โ”€ dto/
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ request.go
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ response.go
โ”‚ โ”‚ โ”œโ”€โ”€ entity.go # ParkingZone model
โ”‚ โ”‚ โ”œโ”€โ”€ repository.go
โ”‚ โ”‚ โ”œโ”€โ”€ service.go
โ”‚ โ”‚ โ”œโ”€โ”€ handler.go
โ”‚ โ”‚ โ””โ”€โ”€ routes.go
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ reservations/
โ”‚ โ”œโ”€โ”€ dto/
โ”‚ โ”‚ โ”œโ”€โ”€ request.go
โ”‚ โ”‚ โ””โ”€โ”€ response.go
โ”‚ โ”œโ”€โ”€ entity.go # Reservation model
โ”‚ โ”œโ”€โ”€ repository.go
โ”‚ โ”œโ”€โ”€ service.go
โ”‚ โ”œโ”€โ”€ handler.go
โ”‚ โ””โ”€โ”€ routes.go
โ”‚
โ”œโ”€โ”€ middleware/
โ”‚ โ”œโ”€โ”€ auth.go
โ”‚ โ””โ”€โ”€ admin.go
โ”‚
โ”œโ”€โ”€ server/
โ”‚ โ””โ”€โ”€ http.go
โ”‚
โ”œโ”€โ”€ utils/
โ”‚ โ”œโ”€โ”€ error.go
โ”‚ โ””โ”€โ”€ parse.go
โ”‚
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ go.sum
โ””โ”€โ”€ README.md
```

### Layer Responsibilities

| Layer | Responsibility |
| ---------- | ------------------------------- |
| DTO | Request and response structures |
| Handler | HTTP request handling |
| Service | Business logic |
| Repository | Database operations |
| Models | Database entities |

Dependencies flow in one direction:

```text
Handler
โ†“
Service
โ†“
Repository
โ†“
Database
```

---

## ๐Ÿ”’ Concurrency Handling

The reservation system prevents overbooking by using:

* Database Transactions
* Row-Level Locking (`FOR UPDATE`)
* Atomic reservation creation

This guarantees that two users cannot reserve the last available spot simultaneously.

Example:

```go
tx.Clauses(
clause.Locking{
Strength: "UPDATE",
},
).First(&zone, zoneID)
```

---

## โš™๏ธ Environment Variables

Create a `.env` file:

```env
DSN=dataBase_connection
PORT=8080
JWT_SECRET=your-super-secret-key
```
---

## ๐Ÿš€ Running Locally

### 1. Clone the Repository

```bash
git clone https://github.com/noboKumar/SpotSync-server.git

cd SpotSync-server
```

### 2. Install Dependencies

```bash
go mod tidy
```

### 3. Create Environment Variables

```bash
cp .env
```

Update the values inside `.env`.

### 5. Run the Application

```bash
go run cmd/main.go
```

or

```bash
go run main.go
```

depending on your project structure.

The server will start at:

```text
http://localhost:8080
```

---

## ๐Ÿ”ฅ Development Mode (Hot Reload)

Install Air:

```bash
go install github.com/air-verse/air@latest
```

Run:

```bash
air
```

---

## ๐Ÿ“š API Endpoints

### Authentication

| Method | Endpoint | Access |
| ------ | ----------------------- | ------ |
| POST | `/api/v1/auth/register` | Public |
| POST | `/api/v1/auth/login` | Public |

---

### Parking Zones

| Method | Endpoint | Access |
| ------ | ------------------- | ------ |
| POST | `/api/v1/zones` | Admin |
| GET | `/api/v1/zones` | Public |
| GET | `/api/v1/zones/:id` | Public |

---

### Reservations

| Method | Endpoint | Access |
| ------ | -------------------------------------- | ------------- |
| POST | `/api/v1/reservations` | Authenticated |
| GET | `/api/v1/reservations/my-reservations` | Authenticated |
| DELETE | `/api/v1/reservations/:id` | Authenticated |
| GET | `/api/v1/reservations` | Admin |

---

## ๐Ÿงช Testing API

You can test the API using:

* Postman
* Thunder Client
* Insomnia
* cURL

Example:

```bash
curl http://localhost:8080/api/v1/zones
```

---

## ๐Ÿ“œ License

This project was developed as part of an academic assignment and is intended for educational purposes.