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

https://github.com/zlarkisz/social-network


https://github.com/zlarkisz/social-network

api backend docker jwt nestjs postgresql redis swagger unit-tests

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# ๐ŸŒ Social Network API

## ๐Ÿ“ Description
The Social Network API is a backend application built with NestJS, designed to support user registration, post creation, and interactions between users. It leverages PostgreSQL for data storage, Redis for caching, and Docker for containerization. Swagger documentation is integrated to ensure easy API exploration.

---

## โœจ Features

### ๐Ÿ‘ฅ User Management
- User registration with hashed passwords (bcrypt).
- Secure JWT-based authentication with token expiration.

### ๐Ÿ“ Post Management
- Create posts associated with authorized users.
- Retrieve posts with caching implemented using Redis.

### โš ๏ธ Error Handling
- Proper error responses for unauthorized access (401) and missing parameters (403).

### ๐Ÿ“– Swagger Documentation
- API endpoints are documented and accessible via Swagger at `/api`.

### ๐Ÿณ Containerized Deployment
- Fully containerized application using Docker Compose.

### ๐Ÿงช Testing
- Unit and integration tests for CRUD operations, authentication, and caching.

---

## ๐Ÿ”ง Prerequisites
- **Node.js**: v20.17.0
- **pnpm**: v9.14.4
- **Docker**: Ensure Docker and Docker Compose are installed and running

---

## ๐Ÿ“ฆ Installation

1. Clone the repository:
```bash
git clone
cd social-network
```

2. Install dependencies:
```bash
pnpm install
```

3. Set up environment variables:
Create a `.env` file in the root directory with the following content:
```env
DB_HOST=db
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=admin
DB_NAME=postgres
JWT_SECRET=your_jwt_secret
```

4. Run the application using Docker Compose:
```bash
docker-compose up -d
```

---

## ๐Ÿ“š API Documentation
Once the application is running, Swagger documentation is available at:
```
http://localhost:3000/api
```

### ๐Ÿ“Œ Endpoints
#### ๐Ÿ” Authentication
- `POST /auth/get-token`: Authenticate a user and retrieve a JWT token.

#### ๐Ÿ‘ค Users
- `POST /users`: Register a new user.
- `GET /users`: Retrieve a list of all users (protected).
- `GET /users/:id`: Retrieve details of a specific user (protected).

#### ๐Ÿ“ Posts
- `POST /posts`: Create a new post (protected).
- `GET /posts`: Retrieve all posts with caching (protected).
- `GET /posts/user/:userId`: Retrieve posts by a specific user ID (protected).

---

## ๐Ÿงช Testing
Run the tests to ensure the application is functioning correctly:
```bash
pnpm test
```
### ๐Ÿงช Tests include:
- Unit tests for authentication and CRUD operations.
- Integration tests for major API flows.

---

## ๐Ÿ“ Project Structure
```
src/
โ”œโ”€โ”€ modules/
โ”‚ โ”œโ”€โ”€ auth/
โ”‚ โ”œโ”€โ”€ users/
โ”‚ โ”œโ”€โ”€ posts/
โ”œโ”€โ”€ app.module.ts
โ”œโ”€โ”€ main.ts
โ”œโ”€โ”€ guards/
โ”œโ”€โ”€ interceptors/
โ”œโ”€โ”€ decorators/
```

---

## ๐Ÿ› ๏ธ Key Technologies
- **NestJS**: Backend framework
- **PostgreSQL**: Database
- **Redis**: Caching
- **Docker Compose**: Container orchestration
- **Swagger**: API documentation
- **Jest**: Testing framework

---

## ๐Ÿš€ Future Improvements
- Add more robust role-based access control.
- Enhance test coverage with edge cases.
- Improve Redis caching strategies for specific user queries.