Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file added .dockerignore
Empty file.
44 changes: 44 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build and Push to ECR

on:
push:
branches:
- main

env:
AWS_REGION: us-east-1

permissions:
id-token: write
contents: read

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: arn:aws:iam::569948922137:role/GitHubActionRole
aws-region: ${{ env.AWS_REGION }}
role-session-name: GitHubActionsSession

- name: Log in to Amazon ECR
run: |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com

- name: Build Docker Image
run: |
docker build -t ${{ secrets.ECR_REPOSITORY }} .

- name: Tag Docker Image
run: |
docker tag ${{ secrets.ECR_REPOSITORY }}:latest ${{ secrets.ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/${{ secrets.ECR_REPOSITORY }}:latest

- name: Push to Amazon ECR
run: |
docker push ${{ secrets.ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/${{ secrets.ECR_REPOSITORY }}:latest
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:16-slim As build
WORKDIR /app
COPY package*.json ./

RUN npm install --production

FROM node:16-slim
WORKDIR /app

COPY --from=build /app /app
COPY . /app

RUN npm ci --production
Expose 5000
CMD ["node", "index.js"]

142 changes: 0 additions & 142 deletions index.html

This file was deleted.

74 changes: 28 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,74 @@ const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Using import or export module
import express from "express";
import { MongoClient } from 'mongodb';
import bodyParser from 'body-parser';
import os from 'os';
import { publicIpv4 } from 'public-ip';

import { publicIp, publicIpv4, publicIpv6 } from 'public-ip';
// MongoDB URL (Docker container ya localhost ke liye)
const mongoUrl = 'mongodb://mongo-db:27017/'; // Docker ke liye
// const mongoUrl = 'mongodb://localhost:27017/'; // Localhost ke liye

// // Using require
// const express = require("express");
// const { MongoClient } = require('mongodb');
// const bodyParser = require("body-parser");
// const os = require('os');
const client = new MongoClient(mongoUrl);
const db = client.db('mydatabase');
const collection = db.collection('mycollection');


// For mongodb
const mongoUrl= 'mongodb://3.81.12.106:27017/'// Replace with mongodb server IP
const client = new MongoClient(mongoUrl);

const db = client.db('mydatabase'); // Name of your database
const collection = db.collection('mycollection'); // Name of your collection

// Database connection function
// MongoDB connection function
async function connectToMongoDB() {
try {
await client.connect();
console.log('Connected to MongoDB server');
} catch (error) {
console.error('Error connecting to MongoDB Server:', error);
}
}

}
connectToMongoDB();

// await client.close(); // Close the connection

const app = express();
const PORT = process.env.PORT || 5000;

app.use(express.json());

// Static files serve from 'public' directory
app.use(express.static(__dirname + '/public'));

// Home page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
res.sendFile(__dirname + '/public/index.html');
});

// Parse incoming requests with JSON payloads
app.use(express.json());
app.use(express.static('/')); // Serve static files from root directory also we can use 'public' directory

// Insert data to MongoDB server
// Insert data to MongoDB
app.post('/insertData', async (req, res) => {
const data = req.body;

try {
// Check for duplicates
const existingData = await collection.findOne({ email: data.email });

if (existingData) {
return res.send(' email already exists, user adding fail!!');
// return res.status(400).send(' email already exists');
return res.send('Email already exists, user adding failed!');
}

// Insert the data
await collection.insertOne(data);
res.status(200).send(' added successfully');
// console.log('User added successfully....')


res.status(200).send('Added successfully!');
} catch (error) {
// console.error('Error inserting data:', error);
return res.status(500).send(' add Error');
return res.status(500).send('Error adding data');
}
});

// Get data from MongoDB server
// Fetch data from MongoDB
app.get('/fetchData', async (req, res) => {
const data = await collection.find({}).limit(12).sort({ _id: -1 }).toArray()
const data = await collection.find({}).limit(12).sort({ _id: -1 }).toArray();
res.json(data);
// console.log('User fetch successfully....')
});


// Find host and ip address
app.get('/hostinfo', async (req, res) => {

const hostname = os.hostname(); // Get the server's hostname
const hostname = os.hostname();
const networkInterfaces = os.networkInterfaces();
let privateIp = '';

// Find the private IP address
// Find private IP
for (const iface in networkInterfaces) {
for (let i = 0; i < networkInterfaces[iface].length; i++) {
if (networkInterfaces[iface][i].family === 'IPv4' && !networkInterfaces[iface][i].internal) {
Expand All @@ -104,16 +83,19 @@ app.get('/hostinfo', async (req, res) => {
}
if (privateIp) break;
}

let publicIpAddress = await publicIpv4();

const hostinfo = {
hostname,
privateIp,
publicIpAddress
};

res.json(hostinfo);
});

// Server listening
app.listen(PORT, () => {
console.log('Server is running on', PORT);
});
console.log('Server is running on port', PORT);
});
11 changes: 11 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
server {
listen 80;
server_name localhost;

root /usr/share/nginx/html;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}
1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading