Skip to content

Commit afdf601

Browse files
committed
Adjusted docker files and corrected react fetches to pull from nginx /api
1 parent 3eba547 commit afdf601

21 files changed

Lines changed: 89 additions & 80 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ backend/challenge_archives/*.zip
1111
.DS_Store
1212
.env
1313
.env.*
14+
/docker/.env
1415

1516
npm-debug.log*
1617
yarn-debug.log*

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install .
1919
npm start
2020
```
2121

22-
Move the `.env` file into the project root directory, then run the following to run the backend locally.
22+
Move the web related `.env` file into the project root directory, then run the following to run the backend locally.
2323
```bash
2424
nodejs backend/server.mjs
2525
```
@@ -28,14 +28,18 @@ nodejs backend/server.mjs
2828
This project uses Docker :whale: for production set-up
2929
```bash
3030
sudo apt update && sudo apt install docker.io docker-compose
31+
# The Docker building process uses the build result
32+
# from `npm run build` the build will fail if build does not exist
33+
npm run build
3134
```
3235
The following commands will build the docker via compose which builds the multi-docker system.
36+
You will also need to move the docker related `.env` into the docker folder (dir of Dockerfile) before running the following:
3337
```bash
3438
cd docker
3539
docker network create traefik
3640
docker-compose -p khi up --build
3741
```
38-
The following commands will build ONLY the khi website docker.
42+
The following commands will build ONLY the khi website docker image that you later start via `docker run -d ...`.
3943
```bash
4044
docker build -f docker/Dockerfile -t image_name .
4145
docker run -d -p 8080:80 --name docker_name image_name

backend/db.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ function MongoURI() {
1212
const db_pass = process.env.DB_PASS;
1313
const db_name = process.env.DB_NAME;
1414

15-
return "mongodb://" + db_user + ":" + db_pass + "@localhost:27017/" + db_name;
15+
return "mongodb://" + db_user + ":" + db_pass + "@khi_db:27017/" + db_name + "?authSource=admin";
1616
}
1717

18+
console.log(`[*] Attempting to Connect to: ${MongoURI()}`);
1819
// Connect to MongoDB
1920
mongoose.connect(MongoURI()).then(() => {
2021
console.log('Connected to MongoDB');

backend/server.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ const app = express();
6767
#################################################################
6868
*/
6969
const host = "0.0.0.0"
70-
const port = process.env.REACT_APP_BACKEND_PORT;
70+
const port = 4000;
7171

7272
// Allows data to be sent from post requests
7373
app.use(express.urlencoded({ extended: true }));
7474
app.use(express.json()); // middleware to handle JSON
7575
app.use(cookieParser()); // Access cookies
7676

7777
const allowedOrigins = [
78-
`http://localhost:${process.env.FRONT_END_PORT}`, // local dev
79-
`http://${process.env.LAN_HOST}:${process.env.FRONT_END_PORT}`, // LAN live frontend
78+
`http://localhost:80`, // local dev
79+
`http://localhost:8080`,
8080
// 'https://khi.io' // production site
8181
];
8282

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM nginx:latest
22
WORKDIR /
33

4-
# setup for mongo container:
4+
# setup for mongo container manually:
55
# docker run -d --name khi_db -e MONGO_INITDB_ROOT_USERNAME=env.user -e MONGO_INITDB_ROOT_PASSWORD=env.password -e MONGO_INITDB_DATABASE=env.db -v mongodb_data:/data/db -p 27017:27017 mongo:latest
66
RUN apt-get update && apt-get install -y supervisor sudo nodejs npm net-tools
77

docker/docker-compose.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ services:
44
container_name: khi_db
55
restart: always
66
environment:
7-
- MONGO_INITDB_ROOT_USERNAME=dbu
8-
- MONGO_INITDB_ROOT_PASSWORD=YFt3fG6wWYJJx8Rj9NssVVmF
7+
- MONGO_INITDB_ROOT_USERNAME=${DB_USER}
8+
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
99
- MONGO_INITDB_DATABASE=khi2025
1010
volumes:
1111
- db_data:/data/db
12+
- ./init_mongo.sh:/docker-entrypoint-initdb.d/init.sh:ro
1213
healthcheck:
1314
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
1415
interval: 10s
@@ -27,7 +28,7 @@ services:
2728
ports:
2829
- "8080:80"
2930
environment:
30-
- DATABASE_URL=mongodb://dbu:YFt3fG6wWYJJx8Rj9NssVVmF@khi_db:27017/khi2025
31+
- DATABASE_URL=mongodb://${DB_USER}:${DB_PASS}@khi_db:27017/khi2025?authSource=admin
3132
- ORIGIN=http://0.0.0.0:80
3233
labels:
3334
- "traefik.enable=true"

docker/init_mongo.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
username="admin" # OPTIONAL to change
4+
password="admin" # CHANGE THIS IN PRODUCTION
5+
salt="" # SALT .env
6+
hashed=$(echo -n "${salt}${password}" | sha256sum | awk '{print $1}')
7+
8+
mongosh -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin "$MONGO_INITDB_DATABASE" <<EOF
9+
db.admins.insertOne({
10+
username: "$username",
11+
password: "$hashed",
12+
isProtected: true,
13+
created_at: new Date()
14+
});
15+
EOF > /root/init_mongo.log

src/components/admin_navbar.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import React, { useEffect, useState } from 'react';
22
import { Link } from 'react-router-dom'; // needed to interact with the React App BrowserRouter
33

4-
function GetBackendHost() {
5-
const BACKEND_HOST = process.env.REACT_APP_BACKEND_HOST;
6-
const BACKEND_PORT = process.env.REACT_APP_BACKEND_PORT;
7-
return `${BACKEND_HOST}:${BACKEND_PORT}`;
8-
}
9-
104
async function LogoutAdmin() {
11-
const response = await fetch(`http://${GetBackendHost()}/admin/logout`, {
5+
const response = await fetch(`/api/admin/logout`, {
126
method: "POST",
137
headers: {
148
"Content-Type": "application/json",
@@ -35,7 +29,7 @@ const HandleLogout = async (event) => {
3529

3630
async function VerifyAdminSession() {
3731
try {
38-
const response = await fetch(`http://${GetBackendHost()}/admin/verify`, {
32+
const response = await fetch(`/api/admin/verify`, {
3933
method: "GET",
4034
credentials: 'include'
4135
});
@@ -94,4 +88,4 @@ function AdminNavbar() {
9488
}
9589
export default AdminNavbar;
9690

97-
export { VerifyAdminSession, LogoutAdmin, GetBackendHost };
91+
export { VerifyAdminSession, LogoutAdmin };

src/components/leader_view.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import '../App.css';
22
import showPasswordPrompt from '../components/prompt.js';
33

44
const LeaderView = ({ TEAM_DATA, UpdateTeamName, newTeamName,
5-
SetNewTeamName, BackendHost, SetTeamUpdateMsg }) => {
5+
SetNewTeamName, SetTeamUpdateMsg }) => {
66

77
const RemoveMember = async (member_name) => {
88
try {
9-
const response = await fetch(`http://${BackendHost}/team/remove-member`, {
9+
const response = await fetch(`/api/team/remove-member`, {
1010
method: "POST",
1111
headers: {
1212
"Content-Type": "application/json"
@@ -34,7 +34,7 @@ const LeaderView = ({ TEAM_DATA, UpdateTeamName, newTeamName,
3434
};
3535
const AddMember = async (req_id, checksum) => {
3636
try {
37-
const response = await fetch(`http://${BackendHost}/team/add-member`, {
37+
const response = await fetch(`/api/team/add-member`, {
3838
method: "POST",
3939
headers: {
4040
"Content-Type": "application/json"
@@ -65,7 +65,7 @@ const LeaderView = ({ TEAM_DATA, UpdateTeamName, newTeamName,
6565

6666
const LeaderLeaving = async (passwd) => {
6767
try {
68-
const response = await fetch(`http://${BackendHost}/team/replace-leader`, {
68+
const response = await fetch(`/api/team/replace-leader`, {
6969
method: "POST",
7070
headers: {
7171
"Content-Type": "application/json"

src/components/navbar.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import React, { useEffect, useState } from 'react';
22
import { Link } from 'react-router-dom'; // needed to interact with the React App BrowserRouter
33

4-
function GetBackendHost() {
5-
const BACKEND_HOST = process.env.REACT_APP_BACKEND_HOST;
6-
const BACKEND_PORT = process.env.REACT_APP_BACKEND_PORT;
7-
return `${BACKEND_HOST}:${BACKEND_PORT}`;
8-
}
94

105
async function LogoutUser() {
11-
const response = await fetch(`http://${GetBackendHost()}/logout`, {
6+
const response = await fetch(`/api/logout`, {
127
method: "POST",
138
headers: {
149
"Content-Type": "application/json",
@@ -37,7 +32,7 @@ const HandleLogout = async (event) => {
3732
async function VerifySession() {
3833
// ask the backend if this session is valid
3934
try {
40-
const response = await fetch(`http://${GetBackendHost()}/user/verify`, {
35+
const response = await fetch(`/api/user/verify`, {
4136
method: "GET",
4237
credentials: 'include' // ensures cookies are sent
4338
});
@@ -113,4 +108,4 @@ function Navbar() {
113108
}
114109
export default Navbar;
115110

116-
export { VerifySession, LogoutUser, GetBackendHost };
111+
export { VerifySession, LogoutUser };

0 commit comments

Comments
 (0)