File tree Expand file tree Collapse file tree 5 files changed +43
-20
lines changed Expand file tree Collapse file tree 5 files changed +43
-20
lines changed Original file line number Diff line number Diff line change @@ -8,11 +8,10 @@ QUESTION_DB_USERNAME=user
8
8
QUESTION_DB_PASSWORD = password
9
9
10
10
# User Service
11
- USER_SERVICE_CLOUD_URI = mongodb+srv://admin:<db_password>@cluster0.uo0vu.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
12
- USER_SERVICE_LOCAL_URI = mongodb://127.0.0.1:27017/peerprepUserServiceDB
13
-
14
- # Will use cloud MongoDB Atlas database
15
- ENV = PROD
11
+ USER_DB_CLOUD_URI = <FILL-THIS-IN>
12
+ USER_DB_LOCAL_URI = mongodb://user-db:27017/user
13
+ USER_DB_USERNAME = user
14
+ USER_DB_PASSWORD = password
16
15
17
16
# Secret for creating JWT signature
18
17
JWT_SECRET = you-can-replace-this-with-your-own-secret
Original file line number Diff line number Diff line change @@ -18,4 +18,8 @@ services:
18
18
command : npm run dev
19
19
volumes :
20
20
- /app/node_modules
21
- - ./services/user:/app
21
+ - ./services/user:/app
22
+
23
+ user-db :
24
+ ports :
25
+ - 27018:27017
Original file line number Diff line number Diff line change @@ -49,15 +49,34 @@ services:
49
49
ports :
50
50
- 8082:8082
51
51
environment :
52
- USER_SERVICE_CLOUD_URI : ${USER_SERVICE_CLOUD_URI}
53
- USER_SERVICE_LOCAL_URI : ${USER_SERVICE_LOCAL_URI}
54
- ENV : ${ENV}
52
+ DB_CLOUD_URI : ${USER_DB_CLOUD_URI}
53
+ DB_LOCAL_URI : ${USER_DB_LOCAL_URI}
54
+ DB_USERNAME : ${USER_DB_USERNAME}
55
+ DB_PASSWORD : ${USER_DB_PASSWORD}
55
56
JWT_SECRET : ${JWT_SECRET}
57
+ networks :
58
+ - user-db-network
59
+ restart : always
60
+
61
+ user-db :
62
+ container_name : user-db
63
+ image : mongo:7.0.14
64
+ environment :
65
+ MONGO_INITDB_ROOT_USERNAME : ${USER_DB_USERNAME}
66
+ MONGO_INITDB_ROOT_PASSWORD : ${USER_DB_PASSWORD}
67
+ volumes :
68
+ - user-db:/data/db
69
+ networks :
70
+ - user-db-network
71
+ command : --quiet
56
72
restart : always
57
73
58
74
volumes :
59
75
question-db :
76
+ user-db :
60
77
61
78
networks :
62
79
question-db-network :
80
+ driver : bridge
81
+ user-db-network :
63
82
driver : bridge
Original file line number Diff line number Diff line change 1
- # User Service
2
- USER_SERVICE_CLOUD_URI = <cloud_uri>
3
- USER_SERVICE_LOCAL_URI = mongodb://127.0.0.1:27017/peerprepUserServiceDB
4
- PORT = 8082
5
-
6
- # Will use cloud MongoDB Atlas database
7
- ENV = PROD
1
+ # This is a sample environment configuration file.
2
+ # Copy this file to .env and replace the placeholder values with your own.
3
+ DB_CLOUD_URI = <FILL-THIS-IN>
4
+ DB_LOCAL_URI = mongodb://user-db:27017/user
5
+ PORT = 8083
8
6
9
7
# Secret for creating JWT signature
10
8
JWT_SECRET = you-can-replace-this-with-your-own-secret
Original file line number Diff line number Diff line change @@ -3,14 +3,17 @@ import 'dotenv/config';
3
3
import { connect } from 'mongoose' ;
4
4
5
5
export async function connectToDB ( ) {
6
- const mongoDBUri =
7
- process . env . ENV === 'PROD' ? process . env . USER_SERVICE_CLOUD_URI : process . env . USER_SERVICE_LOCAL_URI ;
6
+ const mongoUri = process . env . NODE_ENV === 'production' ? process . env . DB_CLOUD_URI : process . env . DB_LOCAL_URI ;
8
7
9
- if ( ! mongoDBUri ) {
8
+ if ( ! mongoUri ) {
10
9
throw new Error ( 'MongoDB URI not specified' ) ;
11
10
}
12
11
13
- await connect ( mongoDBUri ) ;
12
+ await connect ( mongoUri , {
13
+ authSource : 'admin' ,
14
+ user : process . env . DB_USERNAME ,
15
+ pass : process . env . DB_PASSWORD ,
16
+ } ) ;
14
17
}
15
18
16
19
export async function createUser ( username : string , email : string , password : string ) {
You can’t perform that action at this time.
0 commit comments