-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e2e78e
commit 1d1065b
Showing
11 changed files
with
88 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
# This is a sample environment configuration file. | ||
# Copy this file to .env and replace the placeholder values with your own. | ||
QUESTION_DB_CLOUD_URI=<FILL-THIS-IN> | ||
QUESTION_DB_LOCAL_URI=mongodb://question-db:27017/question | ||
QUESTION_DB_USERNAME=user | ||
QUESTION_DB_PASSWORD=password | ||
QUESTION_DB_PASSWORD=password | ||
QUESTION_CORS_ORIGIN=* | ||
QUESTION_PORT=8081 | ||
|
||
NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# This is a sample environment configuration file. | ||
# Copy this file to .env and replace the placeholder values with your own. | ||
DB_CLOUD_URI=<FILL-THIS-IN> | ||
DB_LOCAL_URI=mongodb://question-db:27017/question | ||
DB_USERNAME=user | ||
DB_PASSWORD=password | ||
CORS_ORIGIN=* | ||
PORT=8081 | ||
NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
import app from './app'; | ||
import { PORT } from './config'; | ||
import { connectToDB, upsertManyQuestions } from './models'; | ||
import { getDemoQuestions } from './utils/data'; | ||
|
||
app.listen(PORT, () => console.log(`App is running on port ${PORT}.`)); | ||
const port = process.env.PORT || 8081; | ||
|
||
connectToDB() | ||
.then(async () => { | ||
console.log('MongoDB connected successfully'); | ||
return await getDemoQuestions(); | ||
}) | ||
.then(questions => upsertManyQuestions(questions)) | ||
.then(() => { | ||
console.log('Questions synced successfully'); | ||
app.listen(port, () => console.log(`Question service is listening on port ${port}.`)); | ||
}) | ||
.catch(error => { | ||
console.error('Failed to start server'); | ||
console.error(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
import mongoose from 'mongoose'; | ||
import { MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_URL, MONGODB_DATABASE } from '../config'; | ||
import { IQuestion, Question } from './questionModel'; | ||
|
||
export const connectDb = () => { | ||
const connectionString = `${MONGODB_URL}/${MONGODB_DATABASE}`; | ||
export async function connectToDB() { | ||
const mongoURI = process.env.NODE_ENV === 'production' ? process.env.DB_CLOUD_URI : process.env.DB_LOCAL_URI; | ||
|
||
mongoose | ||
.connect(connectionString, { | ||
authSource: 'admin', | ||
user: MONGODB_USERNAME, | ||
pass: MONGODB_PASSWORD, | ||
}) | ||
.then(() => console.log('MongoDB connected successfully')) | ||
.catch(error => { | ||
console.error('MongoDB connection error:', error.message); | ||
process.exit(1); | ||
}); | ||
}; | ||
if (!mongoURI) { | ||
throw Error('MongoDB URI not specified'); | ||
} else if (!process.env.DB_USERNAME || !process.env.DB_PASSWORD) { | ||
throw Error('MongoDB credentials not specified'); | ||
} | ||
|
||
await mongoose.connect(mongoURI, { | ||
authSource: 'admin', | ||
user: process.env.DB_USERNAME, | ||
pass: process.env.DB_PASSWORD, | ||
}); | ||
} | ||
|
||
export async function upsertManyQuestions(questions: IQuestion[]) { | ||
const ops = questions.map(item => ({ | ||
updateOne: { | ||
filter: { id: item.id }, | ||
update: item, | ||
upsert: true, | ||
}, | ||
})); | ||
await Question.bulkWrite(ops); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import fs from 'fs/promises'; | ||
import { IQuestion } from '../models/questionModel'; | ||
|
||
export async function getDemoQuestions(): Promise<IQuestion[]> { | ||
const data = await fs.readFile('./src/data/questions.json', { encoding: 'utf8' }); | ||
return JSON.parse(data); | ||
} |