Skip to content

Commit

Permalink
Merge pull request #1
Browse files Browse the repository at this point in the history
work-branch
  • Loading branch information
sameer05515 authored Apr 30, 2024
2 parents 9857d17 + 81f4518 commit 325a397
Show file tree
Hide file tree
Showing 344 changed files with 323,633 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled class file
*.class
.vscode

# Log file
*.log
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# microservices-playground
# microservices-playground
In this repo, I wish to create different microservices test projects. create docker images and orchestrate using K8s.


# References
- https://minikube.sigs.k8s.io/docs/start/
26 changes: 26 additions & 0 deletions example-base-01/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Example base 01
It contains folder structure as follows
```
example-base-01 ==base folder
|- backend = root for all backend services
|- frontend = root for frontend project. In this example we have single frontend project. It communicates with microservices,
|- nginx.conf =
```

# docker compose with custom yml file
## to build
```
docker-compose -f docker-compose-with-networks.yml build --no-cache
```
## to up
```
docker-compose -f docker-compose-with-networks.yml up
```

# Frontend development
## To initialize a React.js project directly inside the "frontend" folder without creating any subfolders
```
npx create-react-app .
```

This command will initialize a new React.js project directly in the "frontend" folder without creating any subfolders.
2 changes: 2 additions & 0 deletions example-base-01/backend/topic/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
# npm-debug.log
1 change: 1 addition & 0 deletions example-base-01/backend/topic/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FRONTEND_SERVICE_URL= http://localhost:3002
25 changes: 25 additions & 0 deletions example-base-01/backend/topic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

package-lock.json
20 changes: 20 additions & 0 deletions example-base-01/backend/topic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node runtime as the base image
FROM node:lts-buster-slim AS development

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install --force

# Copy the entire project to the working directory
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Command to run the application
CMD ["npm", "start"]
29 changes: 29 additions & 0 deletions example-base-01/backend/topic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "topic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.10",
"express": "^4.19.2",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.6.5",
"mysql": "^2.18.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
38 changes: 38 additions & 0 deletions example-base-01/backend/topic/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const app = express();
const port = 3000; // You can change this if needed
const path = require('path');

// Set the view engine to EJS
app.set('view engine', 'ejs');
// Set the directory for views
app.set('views', path.join(__dirname, 'views'));

// Define the frontend service URL
const frontendServiceUrl = process.env.FRONTEND_SERVICE_URL || 'http://localhost:3002';

// Middleware to set CORS headers
app.use((req, res, next) => {
// Allow requests from frontend service origin
res.setHeader('Access-Control-Allow-Origin', frontendServiceUrl); // Externalized URL
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); // Allow specific HTTP methods
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Allow specific headers
next();
});

// Define your routes
app.get('/', (req, res) => {
console.log(`process.env.FRONTEND_SERVICE_URL : ${process.env.FRONTEND_SERVICE_URL}`);
res.render('index', {frontendServiceUrl:process.env.FRONTEND_SERVICE_URL});
});

app.get('/topics', (req, res) => {
console.log(`process.env.FRONTEND_SERVICE_URL : ${process.env.FRONTEND_SERVICE_URL}`);
const topics = ['Node.js', 'Express', 'JavaScript', 'EJS'];
res.json({ topics });
});

// Start the server
app.listen(port, () => {
console.log(`Topics Server is running on http://localhost:${port}`);
});
12 changes: 12 additions & 0 deletions example-base-01/backend/topic/src/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to Topics microservice</h1>
<h2><%= frontendServiceUrl %></h2>
</body>
</html>
2 changes: 2 additions & 0 deletions example-base-01/backend/word/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
# npm-debug.log
1 change: 1 addition & 0 deletions example-base-01/backend/word/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FRONTEND_SERVICE_URL= http://localhost:3002
25 changes: 25 additions & 0 deletions example-base-01/backend/word/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

package-lock.json
20 changes: 20 additions & 0 deletions example-base-01/backend/word/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node runtime as the base image
FROM node:lts-buster-slim AS development

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install --force

# Copy the entire project to the working directory
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Command to run the application
CMD ["npm", "start"]
29 changes: 29 additions & 0 deletions example-base-01/backend/word/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "word",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.10",
"express": "^4.19.2",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.6.5",
"mysql": "^2.18.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
38 changes: 38 additions & 0 deletions example-base-01/backend/word/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const app = express();
const port = 3001; // You can change this if needed
const path = require('path');

// Set the view engine to EJS
app.set('view engine', 'ejs');
// Set the directory for views
app.set('views', path.join(__dirname, 'views'));

// Define the frontend service URL
const frontendServiceUrl = process.env.FRONTEND_SERVICE_URL || 'http://localhost:3002';

// Middleware to set CORS headers
app.use((req, res, next) => {
// Allow requests from frontend service origin
res.setHeader('Access-Control-Allow-Origin', frontendServiceUrl); // Externalized URL
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); // Allow specific HTTP methods
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Allow specific headers
next();
});

// Define your routes
app.get('/', (req, res) => {
console.log(`process.env.FRONTEND_SERVICE_URL : ${process.env.FRONTEND_SERVICE_URL}`);
res.render('index', {frontendServiceUrl:process.env.FRONTEND_SERVICE_URL});
});

app.get('/words', (req, res) => {
console.log(`process.env.FRONTEND_SERVICE_URL : ${process.env.FRONTEND_SERVICE_URL}`);
const words = ['Apple', 'Banana', 'Car', 'Donkey'];
res.json({ words });
});

// Start the server
app.listen(port, () => {
console.log(`Words Server is running on http://localhost:${port}`);
});
13 changes: 13 additions & 0 deletions example-base-01/backend/word/src/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to Words microservice</h1>

<h2><%= frontendServiceUrl %></h2>
</body>
</html>
29 changes: 29 additions & 0 deletions example-base-01/docker-compose-basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3'

services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- topics
- words

topics:
build: ./backend/topic
ports:
- "3000:3000"

words:
build: ./backend/word
ports:
- "3001:3001"

frontend:
build: ./frontend
ports:
- "3002:3002"
environment:
- PORT= 3002
37 changes: 37 additions & 0 deletions example-base-01/docker-compose-with-networks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: '3'

services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- my_network
depends_on:
- topics
- words

topics:
build: ./backend/topic
networks:
- my_network

words:
build: ./backend/word
networks:
- my_network

frontend:
build: ./frontend
ports:
- "3002:3002"
environment:
- PORT= 3002
networks:
- my_network

networks:
my_network:
driver: bridge
Loading

0 comments on commit 325a397

Please sign in to comment.