Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
create docker image for mongo api and Update MongoDB connection to us…
Browse files Browse the repository at this point in the history
…e environment variable
  • Loading branch information
nmcc1212 committed May 17, 2024
1 parent 112f4c6 commit 4fc7a27
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions newAPI/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 18 additions & 0 deletions newAPI/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Empty file added newAPI/Jenkinsfile
Empty file.
25 changes: 25 additions & 0 deletions newAPI/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 3 additions & 5 deletions newAPI/oneTimeRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 12 additions & 6 deletions newAPI/package-lock.json

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

1 change: 1 addition & 0 deletions newAPI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions newAPI/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit 4fc7a27

Please sign in to comment.