From 4fc7a276a1d5dc435cc3797ab0b0510feaafa200 Mon Sep 17 00:00:00 2001 From: Nial <48334675+nmcc1212@users.noreply.github.com> Date: Fri, 17 May 2024 10:26:14 +0100 Subject: [PATCH] create docker image for mongo api and Update MongoDB connection to use environment variable --- newAPI/.dockerignore | 1 + newAPI/Dockerfile | 18 ++++++++++++++++++ newAPI/Jenkinsfile | 0 newAPI/docker-compose.yml | 25 +++++++++++++++++++++++++ newAPI/oneTimeRun.ts | 8 +++----- newAPI/package-lock.json | 18 ++++++++++++------ newAPI/package.json | 1 + newAPI/src/index.ts | 9 ++++----- 8 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 newAPI/.dockerignore create mode 100644 newAPI/Dockerfile create mode 100644 newAPI/Jenkinsfile create mode 100644 newAPI/docker-compose.yml diff --git a/newAPI/.dockerignore b/newAPI/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/newAPI/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/newAPI/Dockerfile b/newAPI/Dockerfile new file mode 100644 index 0000000..3e33849 --- /dev/null +++ b/newAPI/Dockerfile @@ -0,0 +1,18 @@ +FROM node:18 + +# Set the working directory in the container + +# Copy the package.json and package-lock.json (if available) +COPY package*.json ./ + +# Install the dependencies +RUN npm install + +# Copy the rest of the application files +COPY . . + +# Expose the port the app runs on +EXPOSE 3101 + +# Command to run the application +CMD ["npm", "run", "dev"] diff --git a/newAPI/Jenkinsfile b/newAPI/Jenkinsfile new file mode 100644 index 0000000..e69de29 diff --git a/newAPI/docker-compose.yml b/newAPI/docker-compose.yml new file mode 100644 index 0000000..b1d7b4f --- /dev/null +++ b/newAPI/docker-compose.yml @@ -0,0 +1,25 @@ +version: '3.8' + +services: + api: + build: . + container_name: api + ports: + - "3101:3101" + depends_on: + - mongo + environment: + - MONGO_URI=mongodb://mongo:27017/newapi + - NODE_OPTIONS=--max_old_space_size=8192 + command: npm run dev + + mongo: + image: mongo + container_name: mongo-db + ports: + - "27017:27017" + volumes: + - mongo-data:/data/db + +volumes: + mongo-data: diff --git a/newAPI/oneTimeRun.ts b/newAPI/oneTimeRun.ts index 1eeae43..e900b91 100644 --- a/newAPI/oneTimeRun.ts +++ b/newAPI/oneTimeRun.ts @@ -2,13 +2,11 @@ import bcrypt from "bcrypt"; import mongoose from "mongoose"; import User from "./src/schemas/usersSchema"; +import { env } from "process"; async function main() { - await mongoose.connect("mongodb://100.125.70.69:27017/socialAPI", { - authSource: "admin", - user: "root", - pass: "password", - }); + const mongoUri = env.MONGO_URI || "mongodb://localhost:27017/socialAPI"; + await mongoose.connect(mongoUri); console.log("Connected to database"); const users = await User.find(); for (const user of users) { diff --git a/newAPI/package-lock.json b/newAPI/package-lock.json index 822922d..91a07b4 100644 --- a/newAPI/package-lock.json +++ b/newAPI/package-lock.json @@ -13,6 +13,7 @@ "@types/bcrypt": "^5.0.2", "babel-jest": "^29.7.0", "bcrypt": "^5.1.1", + "bcryptjs": "^2.4.3", "express": "^4.18.2", "express-validator": "^7.0.1", "mongoose": "^8.1.1" @@ -2857,6 +2858,11 @@ "node": ">= 10.0.0" } }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -3388,9 +3394,9 @@ } }, "node_modules/detect-libc": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz", - "integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz", + "integrity": "sha512-bwy0MGW55bG41VqxxypOsdSdGqLwXPI/focwgTYCFMbdUiBAxLg9CFzG08sz2aqzknwiX7Hkl0bQENjg8iLByw==", "engines": { "node": ">=8" } @@ -6335,9 +6341,9 @@ } }, "node_modules/tar": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.0.tgz", - "integrity": "sha512-/Wo7DcT0u5HUV486xg675HtjNd3BXZ6xDbzsCUZPt5iw8bTQ63bP0Raut3mvro9u+CUyq7YQd8Cx55fsZXxqLQ==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.1.tgz", + "integrity": "sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==", "dependencies": { "chownr": "^2.0.0", "fs-minipass": "^2.0.0", diff --git a/newAPI/package.json b/newAPI/package.json index f857c72..9836db0 100644 --- a/newAPI/package.json +++ b/newAPI/package.json @@ -22,6 +22,7 @@ "@types/bcrypt": "^5.0.2", "babel-jest": "^29.7.0", "bcrypt": "^5.1.1", + "bcryptjs": "^2.4.3", "express": "^4.18.2", "express-validator": "^7.0.1", "mongoose": "^8.1.1" diff --git a/newAPI/src/index.ts b/newAPI/src/index.ts index b4dc275..c8eee3b 100644 --- a/newAPI/src/index.ts +++ b/newAPI/src/index.ts @@ -8,6 +8,7 @@ import userRouter from "./routes/userRoutes"; import loggerMiddleware from "./middlewares/logger"; import { create } from "domain"; import http from 'http'; +import { env } from "process"; // const app = express(); const port = 3101; @@ -18,12 +19,10 @@ app.use(loggerMiddleware); app.use("/posts", postRouter); app.use("/users", userRouter); +const mongoUri = env.MONGO_URI || "mongodb://localhost:27017/socialAPI"; + async function main() { - await mongoose.connect("mongodb://100.125.70.69:27017/socialAPI", { - authSource: "admin", - user: "root", - pass: "password", - }); + await mongoose.connect(mongoUri); console.log("Connected to MongoDB"); createHttpServer(); }