Skip to content

Commit 456bc41

Browse files
committed
Create dockerfile and updated readme guide on docker build
1 parent 1adc44d commit 456bc41

File tree

4 files changed

+186
-125
lines changed

4 files changed

+186
-125
lines changed

apps/user-service/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

apps/user-service/Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM node:20-alpine AS base
2+
3+
ARG DB_CLOUD_URI
4+
ARG JWT_SECRET
5+
ENV DB_CLOUD_URI=${DB_CLOUD_URI}
6+
ENV JWT_SECRET=${JWT_SECRET}
7+
# ARG NODE_ENV=development
8+
# ENV NODE_ENV=${NODE_ENV}
9+
ENV ENV=PROD
10+
11+
FROM base as deps
12+
13+
# RUN apk add --no-cache libc6-compat
14+
WORKDIR /app
15+
16+
COPY package*.json ./
17+
18+
RUN npm install
19+
RUN npm audit fix --force
20+
21+
# Uncomment the following line if you are building code for production.
22+
# RUN npm ci --omit=dev
23+
24+
COPY . .
25+
26+
# Expose port 3001 so it can be mapped by Docker daemon.
27+
EXPOSE 3001
28+
29+
# Define the command to run your app using CMD which defines your runtime.
30+
CMD [ "npm", "start" ]

apps/user-service/README.md

Lines changed: 104 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
2. After setting up, go to the Database Deployment Page. You would see a list of the Databases you have set up. Select `Connect` on the cluster you just created earlier on for User Service.
1010

11-
![alt text](./GuideAssets/ConnectCluster.png)
11+
![alt text](./GuideAssets/ConnectCluster.png)
1212

1313
3. Select the `Drivers` option, as we have to link to a Node.js App (User Service).
1414

15-
![alt text](./GuideAssets/DriverSelection.png)
15+
![alt text](./GuideAssets/DriverSelection.png)
1616

1717
4. Select `Node.js` in the `Driver` pull-down menu, and copy the connection string.
1818

19-
Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster.
19+
Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster.
2020

21-
![alt text](./GuideAssets/ConnectionString.png)
21+
![alt text](./GuideAssets/ConnectionString.png)
2222

2323
5. In the `user-service` directory, create a copy of the `.env.sample` file and name it `.env`.
2424

@@ -45,6 +45,7 @@
4545
- Endpoint: http://localhost:3001/users
4646

4747
- Body
48+
4849
- Required: `username` (string), `email` (string), `password` (string)
4950

5051
```json
@@ -57,12 +58,12 @@
5758

5859
- Responses:
5960

60-
| Response Code | Explanation |
61-
|-----------------------------|-------------------------------------------------------|
62-
| 201 (Created) | User created successfully, created user data returned |
63-
| 400 (Bad Request) | Missing fields |
64-
| 409 (Conflict) | Duplicate username or email encountered |
65-
| 500 (Internal Server Error) | Database or server error |
61+
| Response Code | Explanation |
62+
| --------------------------- | ----------------------------------------------------- |
63+
| 201 (Created) | User created successfully, created user data returned |
64+
| 400 (Bad Request) | Missing fields |
65+
| 409 (Conflict) | Duplicate username or email encountered |
66+
| 500 (Internal Server Error) | Database or server error |
6667

6768
### Get User
6869

@@ -75,52 +76,52 @@
7576
- Endpoint: http://localhost:3001/users/{userId}
7677

7778
- Parameters
78-
- Required: `userId` path parameter
79-
- Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c`
79+
80+
- Required: `userId` path parameter
81+
- Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c`
8082

8183
- <a name="auth-header">Headers</a>
82-
83-
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
84-
85-
- Explanation: This endpoint requires the client to include a JWT (JSON Web Token) in the HTTP request header for authentication and authorization. This token is generated during the authentication process (i.e., login) and contains information about the user's identity. The server verifies this token to ensure that the client is authorized to access the data.
86-
87-
- Auth Rules:
88-
89-
- Admin users: Can retrieve any user's data. The server verifies the user associated with the JWT token is an admin user and allows access to the requested user's data.
90-
91-
- Non-admin users: Can only retrieve their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server returns the user's own data.
92-
84+
85+
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
86+
87+
- Explanation: This endpoint requires the client to include a JWT (JSON Web Token) in the HTTP request header for authentication and authorization. This token is generated during the authentication process (i.e., login) and contains information about the user's identity. The server verifies this token to ensure that the client is authorized to access the data.
88+
89+
- Auth Rules:
90+
91+
- Admin users: Can retrieve any user's data. The server verifies the user associated with the JWT token is an admin user and allows access to the requested user's data.
92+
- Non-admin users: Can only retrieve their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server returns the user's own data.
93+
9394
- Responses:
9495

95-
| Response Code | Explanation |
96-
|-----------------------------|----------------------------------------------------------|
97-
| 200 (OK) | Success, user data returned |
98-
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
99-
| 403 (Forbidden) | Access denied for non-admin users accessing others' data |
100-
| 404 (Not Found) | User with the specified ID not found |
101-
| 500 (Internal Server Error) | Database or server error |
96+
| Response Code | Explanation |
97+
| --------------------------- | -------------------------------------------------------- |
98+
| 200 (OK) | Success, user data returned |
99+
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
100+
| 403 (Forbidden) | Access denied for non-admin users accessing others' data |
101+
| 404 (Not Found) | User with the specified ID not found |
102+
| 500 (Internal Server Error) | Database or server error |
102103

103104
### Get All Users
104105

105106
- This endpoint allows retrieval of all users' data from the database.
106107
- HTTP Method: `GET`
107108
- Endpoint: http://localhost:3001/users
108109
- Headers
109-
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
110-
- Auth Rules:
111110

112-
- Admin users: Can retrieve all users' data. The server verifies the user associated with the JWT token is an admin user and allows access to all users' data.
113-
114-
- Non-admin users: Not allowed access.
111+
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
112+
- Auth Rules:
113+
114+
- Admin users: Can retrieve all users' data. The server verifies the user associated with the JWT token is an admin user and allows access to all users' data.
115+
- Non-admin users: Not allowed access.
115116

116117
- Responses:
117118

118-
| Response Code | Explanation |
119-
|-----------------------------|--------------------------------------------------|
120-
| 200 (OK) | Success, all user data returned |
121-
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
122-
| 403 (Forbidden) | Access denied for non-admin users |
123-
| 500 (Internal Server Error) | Database or server error |
119+
| Response Code | Explanation |
120+
| --------------------------- | ------------------------------------------------ |
121+
| 200 (OK) | Success, all user data returned |
122+
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
123+
| 403 (Forbidden) | Access denied for non-admin users |
124+
| 500 (Internal Server Error) | Database or server error |
124125

125126
### Update User
126127

@@ -131,9 +132,11 @@
131132
- Endpoint: http://localhost:3001/users/{userId}
132133

133134
- Parameters
135+
134136
- Required: `userId` path parameter
135137

136138
- Body
139+
137140
- At least one of the following fields is required: `username` (string), `email` (string), `password` (string)
138141

139142
```json
@@ -145,24 +148,24 @@
145148
```
146149

147150
- Headers
148-
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
149-
- Auth Rules:
150151

151-
- Admin users: Can update any user's data. The server verifies the user associated with the JWT token is an admin user and allows the update of requested user's data.
152-
153-
- Non-admin users: Can only update their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server updates the user's own data.
152+
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
153+
- Auth Rules:
154+
155+
- Admin users: Can update any user's data. The server verifies the user associated with the JWT token is an admin user and allows the update of requested user's data.
156+
- Non-admin users: Can only update their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server updates the user's own data.
154157

155158
- Responses:
156159

157-
| Response Code | Explanation |
158-
|-----------------------------|---------------------------------------------------------|
159-
| 200 (OK) | User updated successfully, updated user data returned |
160-
| 400 (Bad Request) | Missing fields |
161-
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
162-
| 403 (Forbidden) | Access denied for non-admin users updating others' data |
163-
| 404 (Not Found) | User with the specified ID not found |
164-
| 409 (Conflict) | Duplicate username or email encountered |
165-
| 500 (Internal Server Error) | Database or server error |
160+
| Response Code | Explanation |
161+
| --------------------------- | ------------------------------------------------------- |
162+
| 200 (OK) | User updated successfully, updated user data returned |
163+
| 400 (Bad Request) | Missing fields |
164+
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
165+
| 403 (Forbidden) | Access denied for non-admin users updating others' data |
166+
| 404 (Not Found) | User with the specified ID not found |
167+
| 409 (Conflict) | Duplicate username or email encountered |
168+
| 500 (Internal Server Error) | Database or server error |
166169

167170
### Update User Privilege
168171

@@ -173,9 +176,11 @@
173176
- Endpoint: http://localhost:3001/users/{userId}
174177

175178
- Parameters
179+
176180
- Required: `userId` path parameter
177181

178182
- Body
183+
179184
- Required: `isAdmin` (boolean)
180185

181186
```json
@@ -185,24 +190,25 @@
185190
```
186191

187192
- Headers
188-
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
189-
- Auth Rules:
190193

191-
- Admin users: Can update any user's privilege. The server verifies the user associated with the JWT token is an admin user and allows the privilege update.
192-
- Non-admin users: Not allowed access.
194+
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
195+
- Auth Rules:
196+
197+
- Admin users: Can update any user's privilege. The server verifies the user associated with the JWT token is an admin user and allows the privilege update.
198+
- Non-admin users: Not allowed access.
193199

194200
> :bulb: You may need to manually assign admin status to the first user by directly editing the database document before using this endpoint.
195201

196202
- Responses:
197203

198-
| Response Code | Explanation |
199-
|-----------------------------|-----------------------------------------------------------------|
200-
| 200 (OK) | User privilege updated successfully, updated user data returned |
201-
| 400 (Bad Request) | Missing fields |
202-
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
203-
| 403 (Forbidden) | Access denied for non-admin users |
204-
| 404 (Not Found) | User with the specified ID not found |
205-
| 500 (Internal Server Error) | Database or server error |
204+
| Response Code | Explanation |
205+
| --------------------------- | --------------------------------------------------------------- |
206+
| 200 (OK) | User privilege updated successfully, updated user data returned |
207+
| 400 (Bad Request) | Missing fields |
208+
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
209+
| 403 (Forbidden) | Access denied for non-admin users |
210+
| 404 (Not Found) | User with the specified ID not found |
211+
| 500 (Internal Server Error) | Database or server error |
206212

207213
### Delete User
208214

@@ -212,6 +218,7 @@
212218
- Parameters
213219

214220
- Required: `userId` path parameter
221+
215222
- Headers
216223

217224
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
@@ -221,22 +228,24 @@
221228
- Admin users: Can delete any user's data. The server verifies the user associated with the JWT token is an admin user and allows the deletion of requested user's data.
222229

223230
- Non-admin users: Can only delete their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server deletes the user's own data.
231+
224232
- Responses:
225233

226-
| Response Code | Explanation |
227-
|-----------------------------|---------------------------------------------------------|
228-
| 200 (OK) | User deleted successfully |
229-
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
230-
| 403 (Forbidden) | Access denied for non-admin users deleting others' data |
231-
| 404 (Not Found) | User with the specified ID not found |
232-
| 500 (Internal Server Error) | Database or server error |
234+
| Response Code | Explanation |
235+
| --------------------------- | ------------------------------------------------------- |
236+
| 200 (OK) | User deleted successfully |
237+
| 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT |
238+
| 403 (Forbidden) | Access denied for non-admin users deleting others' data |
239+
| 404 (Not Found) | User with the specified ID not found |
240+
| 500 (Internal Server Error) | Database or server error |
233241

234242
### Login
235243

236244
- This endpoint allows a user to authenticate with an email and password and returns a JWT access token. The token is valid for 1 day and can be used subsequently to access protected resources. For example usage, refer to the [Authorization header section in the Get User endpoint](#auth-header).
237245
- HTTP Method: `POST`
238246
- Endpoint: http://localhost:3001/auth/login
239247
- Body
248+
240249
- Required: `email` (string), `password` (string)
241250

242251
```json
@@ -248,25 +257,35 @@
248257

249258
- Responses:
250259

251-
| Response Code | Explanation |
252-
|-----------------------------|----------------------------------------------------|
253-
| 200 (OK) | Login successful, JWT token and user data returned |
254-
| 400 (Bad Request) | Missing fields |
255-
| 401 (Unauthorized) | Incorrect email or password |
256-
| 500 (Internal Server Error) | Database or server error |
260+
| Response Code | Explanation |
261+
| --------------------------- | -------------------------------------------------- |
262+
| 200 (OK) | Login successful, JWT token and user data returned |
263+
| 400 (Bad Request) | Missing fields |
264+
| 401 (Unauthorized) | Incorrect email or password |
265+
| 500 (Internal Server Error) | Database or server error |
257266

258267
### Verify Token
259268

260269
- This endpoint allows one to verify a JWT access token to authenticate and retrieve the user's data associated with the token.
261270
- HTTP Method: `GET`
262271
- Endpoint: http://localhost:3001/auth/verify-token
263272
- Headers
273+
264274
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
265275

266276
- Responses:
267277

268-
| Response Code | Explanation |
269-
|-----------------------------|----------------------------------------------------|
270-
| 200 (OK) | Token verified, authenticated user's data returned |
271-
| 401 (Unauthorized) | Missing/invalid/expired JWT |
272-
| 500 (Internal Server Error) | Database or server error |
278+
| Response Code | Explanation |
279+
| --------------------------- | -------------------------------------------------- |
280+
| 200 (OK) | Token verified, authenticated user's data returned |
281+
| 401 (Unauthorized) | Missing/invalid/expired JWT |
282+
| 500 (Internal Server Error) | Database or server error |
283+
284+
### Build Docker
285+
286+
```bash
287+
# Navigate to the user-service app directory
288+
cd apps/user-service
289+
# Build dockerfile after replacing the build arguments (Ensure that your docker daemon is running beforehand)
290+
docker build -t user-service --build-arg JWT_TOKEN='replace_with_jwt_token' --build-arg DB_CLOUD_URI='replace_with_db_uri_here' -f Dockerfile .
291+
```

0 commit comments

Comments
 (0)