Skip to content

Commit 71c55fe

Browse files
production ready script added.
1 parent 83ee602 commit 71c55fe

6 files changed

Lines changed: 129 additions & 5 deletions

File tree

api/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ EXPOSE ${API_PORT}
88

99
WORKDIR /api
1010

11+
# Install wget for container healthchecks
12+
RUN apk add --no-cache wget
13+
1114
COPY package*.json ./
1215
RUN npm ci --only=production || npm i --only=production
1316

api/src/api/routers/survey.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ router.post('/register', async (req, res) => {
134134
// Set token in HTTP-only cookie
135135
res.cookie('authToken', token, {
136136
httpOnly: true, // Prevents JavaScript access
137-
secure: false, // Allow HTTP in development
137+
secure: process.env.NODE_ENV === 'production', // HTTPS-only in production
138138
sameSite: 'lax', // CSRF protection
139139
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days in milliseconds
140140
path: '/' // Available across all paths
@@ -206,7 +206,7 @@ router.post('/login', async (req, res) => {
206206
// Set token in HTTP-only cookie
207207
res.cookie('authToken', token, {
208208
httpOnly: true, // Prevents JavaScript access (XSS protection)
209-
secure: false, // Allow HTTP in development
209+
secure: process.env.NODE_ENV === 'production', // HTTPS-only in production
210210
sameSite: 'lax', // CSRF protection
211211
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days in milliseconds
212212
path: '/' // Available across all paths

deploy.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/bin/bash
2+
#
3+
# Production Deployment Script for ABACWS Survey
4+
#
5+
# This script builds and runs all necessary containers for the survey application.
6+
# It assumes you are running it from the root of the project directory.
7+
#
8+
9+
# --- Configuration: SET YOUR SECRETS HERE ---
10+
export JWT_SECRET="YOUR_SUPER_LONG_AND_RANDOM_JWT_SECRET_HERE"
11+
export SESSION_SECRET="ANOTHER_VERY_LONG_AND_RANDOM_SESSION_SECRET_HERE"
12+
export API_KEY="YOUR_SECURE_AND_COMPLEX_API_KEY_HERE"
13+
# -----------------------------------------
14+
15+
# Function to check if a command exists
16+
command_exists () {
17+
type "$1" &> /dev/null ;
18+
}
19+
20+
# Check for secrets
21+
if [[ "$JWT_SECRET" == "YOUR_SUPER_LONG_AND_RANDOM_JWT_SECRET_HERE" || "$SESSION_SECRET" == "ANOTHER_VERY_LONG_AND_RANDOM_SESSION_SECRET_HERE" || "$API_KEY" == "YOUR_SECURE_AND_COMPLEX_API_KEY_HERE" ]]; then
22+
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
23+
echo "!!! ERROR: Default secrets detected. Please edit this script !!!"
24+
echo "!!! and set your production secrets before running. !!!"
25+
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
26+
exit 1
27+
fi
28+
29+
echo "--- Starting Production Deployment ---"
30+
31+
# Step 1: Create Docker Network
32+
echo "1/6: Creating Docker network 'survey-network'..."
33+
docker network inspect survey-network >/dev/null 2>&1 || docker network create survey-network
34+
echo "Network 'survey-network' is ready."
35+
echo ""
36+
37+
# Step 2: Create Persistent Data Directories
38+
echo "2/6: Creating host directories for persistent data..."
39+
mkdir -p ./Mongo/mongo-data
40+
mkdir -p ./api/src/api/data
41+
echo "Data directories are ready."
42+
echo ""
43+
44+
# Step 3: Build Docker Images
45+
echo "3/6: Building Docker images from source..."
46+
docker build -t abacws-api -f api/Dockerfile api
47+
docker build -t abacws-visualiser -f visualiser/Dockerfile visualiser
48+
docker build -t rasa-frontend-prod -f rasa-frontend/Dockerfile.prod ./rasa-frontend
49+
docker build -t abacws-sender -f telemetry/Dockerfile .
50+
echo "All images built successfully."
51+
echo ""
52+
53+
# Step 4: Stop and Remove Old Containers (for clean re-deployments)
54+
echo "4/6: Stopping and removing any old containers..."
55+
docker rm -f abacws-survey-mongo abacws-api abacws-visualiser rasa-frontend abacws-sender >/dev/null 2>&1 || true
56+
echo "Old containers removed."
57+
echo ""
58+
59+
# Step 5: Run Containers
60+
echo "5/6: Starting all service containers..."
61+
62+
# MongoDB
63+
docker run -d --name abacws-survey-mongo --restart always \
64+
-v "$(pwd)/Mongo/mongo-data":/data/db \
65+
--network survey-network --network-alias survey-mongo \
66+
mongo
67+
68+
# API
69+
docker run -d --name abacws-api --hostname apihost --restart always \
70+
-e API_PORT=5000 \
71+
-e MONGO_URL=mongodb://survey-mongo:27017 \
72+
-e MONGODB_URI=mongodb://survey-mongo:27017/abacws \
73+
-e JWT_SECRET="$JWT_SECRET" \
74+
-e SESSION_SECRET="$SESSION_SECRET" \
75+
-e API_KEY="$API_KEY" \
76+
-e NODE_ENV=production \
77+
-p 5000:5000 \
78+
-v "$(pwd)/api/src/api/data":/api/src/api/data \
79+
--network survey-network --network-alias api \
80+
abacws-api
81+
82+
# Visualiser
83+
docker run -d --name abacws-visualiser --hostname visualiserhost --restart always \
84+
-p 8090:80 -e WEB_PORT=80 -e API_HOST=api:5000 \
85+
--network survey-network --network-alias visualiser \
86+
abacws-visualiser
87+
88+
# Frontend (Production)
89+
docker run -d --name rasa-frontend --hostname rasa-frontend-host --restart always \
90+
-p 3000:80 -e WEB_PORT=80 -e API_HOST=api:5000 -e VIS_HOST=visualiser:80 \
91+
--network survey-network \
92+
rasa-frontend-prod
93+
94+
# Telemetry Sender (Optional)
95+
docker run -d --name abacws-sender --restart always \
96+
-e API_BASE=http://api:5000/api \
97+
-e INTERVAL_SECONDS=30 \
98+
-e API_HEALTH=http://api:5000/health \
99+
-e VIS_HEALTH=http://visualiser:80/health \
100+
-p 8088:8088 --network survey-network \
101+
abacws-sender
102+
103+
echo "All containers are starting."
104+
echo ""
105+
106+
# Step 6: Final Instructions
107+
echo "6/6: Deployment complete!"
108+
echo ""
109+
echo "--- Next Steps ---"
110+
echo "1. Verify containers are running with: docker ps"
111+
echo "2. Check logs for any errors with: docker logs <container_name>"
112+
echo "3. Expose the frontend to the internet using ngrok:"
113+
echo " ngrok http 3000"
114+
echo ""
115+
echo "Your survey should be available at the ngrok URL."
116+
echo "--------------------"
117+

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ services:
7777
- /app/node_modules
7878
environment:
7979
- NODE_ENV=development
80-
- REACT_APP_API_URL=http://localhost:5000/api
81-
- REACT_APP_VISUALIZER_URL=http://localhost:8090
80+
- REACT_APP_API_URL=/api
81+
- REACT_APP_VISUALIZER_URL=/visualiser
8282
depends_on:
8383
- api
8484
container_name: rasa-frontend-bldg1

rasa-frontend/src/components/UserBar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export default function UserBar() {
99

1010
const logout = async () => {
1111
try {
12-
await fetch('http://localhost:8080/api/logout', { method: 'POST', credentials: 'include' });
12+
// Call API logout via same-origin proxy
13+
await fetch('/api/survey/logout', { method: 'POST', credentials: 'include' });
1314
} catch (e) {
1415
// ignore network errors for logout
1516
}

visualiser/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ ENV API_HOST=api:5000
1919

2020
EXPOSE ${WEB_PORT}
2121

22+
# Install wget for container healthchecks
23+
RUN apk add --no-cache wget
24+
2225
# Copy frontend build
2326
WORKDIR /app
2427
COPY --from=node /app/build /app

0 commit comments

Comments
 (0)