https://github.com/nikhil25803/blogging-platform
The Blogging Platform API utilizes a microservices architecture with NodeJS, Express.js, Postgres, and Redis, facilitating CRUD operations, authentication, keyword search, and recommendation features. It emphasizes performance optimization, security measures, and scalability via Docker orchestration and thorough unit testing.
https://github.com/nikhil25803/blogging-platform
docker docker-compose expressjs javascript makefile nodejs postgresql prisma redis
Last synced: 6 months ago
JSON representation
The Blogging Platform API utilizes a microservices architecture with NodeJS, Express.js, Postgres, and Redis, facilitating CRUD operations, authentication, keyword search, and recommendation features. It emphasizes performance optimization, security measures, and scalability via Docker orchestration and thorough unit testing.
- Host: GitHub
- URL: https://github.com/nikhil25803/blogging-platform
- Owner: nikhil25803
- License: gpl-3.0
- Created: 2024-01-25T05:59:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-17T06:29:56.000Z (over 2 years ago)
- Last Synced: 2025-01-23T06:09:08.938Z (over 1 year ago)
- Topics: docker, docker-compose, expressjs, javascript, makefile, nodejs, postgresql, prisma, redis
- Language: JavaScript
- Homepage:
- Size: 169 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Blogging Platform API
The Blogging Platform API utilizes a microservices architecture with NodeJS, Express.js, Postgres, and Redis, facilitating CRUD operations, authentication, keyword search, and recommendation features. It emphasizes performance optimization, security measures, and scalability via Docker orchestration and thorough unit testing.
## Tech Stacks
      
## Microservices Architecture
A microservices architecture leveraging Docker containers orchestrates `PostgreSQL`, `Node.js API`, and `Redis` instances for scalable and modular application development.
## Features
- Token-based authentication and authorization for users.
- CRUD operation on blogs.
- Endpoint to retrieve the **latest N blog posts**, with N being a configurable parameter.
- A search functionality that allows users to **search** for blog posts based on **keywords in the title or content**.
- A feature to track and return the **most popular blog posts** based on the number of views.
- Implemented a feature that **recommends related blog** posts based on the content of the currently viewed post based on the `cosine similarity` concept.
- Added middleware to **compress API responses** for improved performance.
- Optimized database queries for efficiency and considered **asynchronous processing** where applicable.
- Wrote **comprehensive unit tests** covering the new features and algorithms
- Implemented advanced security measures such as **rate limiting**, **request validation**, and protection against common web vulnerabilities.
## Project setup
### Clone the repository
```bash
https://github.com/nikhil25803/blogging-platform
```
### Configure environment variables
Add a `.env` and add values mentioned in `.env.template`
```bash
PORT=...
JWT_ACCESS_TOKEN=...
REDIS_HOST=...
REDIS_PORT=...
POSTGRESDB_USER=...
POSTGRESDB_ROOT_PASSWORD=...
POSTGRESDB_DATABASE=...
POSTGRESDB_LOCAL_PORT=...
POSTGRESDB_DOCKER_PORT=...
DATABASE_URL=postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}
```
### Using Docker
- Start up the services
```bash
docker-compose up
```
- Stop and remove the containers created
```bash
docker-compose down --rmi all
```
- You can now access the server on `localhost:8000`
### Manual
- Install dependencies
```bash
npm i
```
- Migrate schema to database using prisma
```bash
npx prisma migrate dev --name init
```
- **Note** - Make sure the DB and Redis are running properly, and make changes in the `.env` file properly.
- Run test
This command will run and test all the API endpoints. Will clear and reset the database
```bash
npm run test
```
- Run the server
```
npm run dev
```
## Recommendation Engine
A simplified approach using TF-IDF (Term Frequency-Inverse Document Frequency) for content analysis and **cosine similarity** for finding related posts. This approach is a common and relatively straightforward method for content-based recommendation.
A glimpse into code used to generate content-based recommendations.
```js
// Function to calculate cosine similarity between two vectors
const cosineSimilarity = (vectorA, vectorB) => {
// Calculate dot product
const dotProduct = Object.keys(vectorA).reduce((acc, term) => {
if (vectorB[term]) {
acc += vectorA[term] * vectorB[term];
}
return acc;
}, 0);
// Calculate Euclidean norms
const normA = Math.sqrt(
Object.values(vectorA).reduce((acc, val) => acc + val ** 2, 0)
);
const normB = Math.sqrt(
Object.values(vectorB).reduce((acc, val) => acc + val ** 2, 0)
);
// Calculate cosine similarity
const similarity = dotProduct / (normA * normB);
return similarity;
};
const blogsRecommendation = async (blogid) => {
// Fetch content of current blogs
const blogData = await prisma.blog.findFirst({
where: {
bid: blogid,
},
});
// Get content of the blog
const blogContent = blogData.content.toLowerCase();
// Current post vector data
tfidf.addDocument(blogContent, -1);
const cuurentBlogVector = tfidf.documents[0];
// Get content of all existing blogs except current blog
const existingBLogsData = await prisma.blog.findMany({
where: {
bid: {
not: blogid,
},
},
});
// List to store recommended blogs based on cosine similarity calculation
const recommendedBLogs = [];
existingBLogsData.forEach((blog, index) => {
tfidf.addDocument(blog.content, index);
const currentDocumentVector = tfidf.documents[index + 1];
// Calculate cosine similarity
const cosineSimilarityValue = cosineSimilarity(
cuurentBlogVector,
currentDocumentVector
);
// Add blog in recommendation blog in this list if cosine score is > 0.2
if (cosineSimilarityValue >= 0.2) {
recommendedBLogs.push(blog);
}
});
return recommendedBLogs;
};
```