Skip to content

Commit

Permalink
Db connection issue (#29)
Browse files Browse the repository at this point in the history
* WIP: backend issue
  • Loading branch information
makwanji authored Aug 5, 2024
1 parent 22ea0fc commit d3b0ba0
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 44 deletions.
11 changes: 11 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Database connection - App
DB_DATABASE=healthnestdb
DB_HOST=healthnest-db
DB_PASSWORD=hna123
DB_USER=healthnest

# Database password
MYSQL_DATABASE=healthnestdb
MYSQL_PASSWORD=hna123
MYSQL_ROOT_PASSWORD=root123
MYSQL_USER=healthnest
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Start the server
```bash
npm start
```
<br>
<br>

To Test backend:
http://3.1.222.229:8081/doctors
### Go-to the Frontend folder

```bash
Expand Down
4 changes: 0 additions & 4 deletions backend/.env

This file was deleted.

4 changes: 0 additions & 4 deletions backend/.env.sample

This file was deleted.

14 changes: 6 additions & 8 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ COPY server.js .
COPY db.js .
COPY routes/ ./routes/
COPY Controller/ ./Controller
# COPY . .

# Expose the port your app runs on
EXPOSE 49350:8081
# Build the application (if needed)
# RUN npm run build

# Set environment variables
ENV DB_HOST="mysql"
ENV DB_USER="example_user"
ENV DB_PASSWORD="example_password"
ENV DB_DATABASE="test"
# Expose the port your app runs on
EXPOSE 3000

# Command to run your application
CMD ["node", "server.js"]
CMD ["node", "server.js"]
6 changes: 3 additions & 3 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ db.connect((err) => {

const doctorRoutes = require("./routes/doctorRoutes");
const patientRoutes = require("./routes/patinetRoutes");
const { saveDoctorProfile } = require('../backend/Controller/doctorController');
const { saveDoctorProfile } = require('./Controller/doctorController');

app.use("/doctors", doctorRoutes);
app.use("/patients", patientRoutes);
Expand Down Expand Up @@ -220,7 +220,7 @@ app.get('/appointments', (req, res) => {
}

// Return appointments data
return res.json(appointments);
return res.json(appointments);
});
});

Expand Down Expand Up @@ -270,7 +270,7 @@ app.get("/users", (req, res) => {
app.delete("/users/:id", (req, res) => {
const userId = req.params.id;
const sql = "DELETE FROM login WHERE login_id = ?";

db.query(sql, [userId], (err, result) => {
if (err) {
console.error("Database error:", err); // Log the detailed error
Expand Down
15 changes: 8 additions & 7 deletions database/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Use the official MySQL 5.7 image as the base image
FROM mysql:8.4.0

# Set environment variables
ENV MYSQL_ROOT_PASSWORD example_password
ENV MYSQL_DATABASE test
ENV MYSQL_USER example_user
ENV MYSQL_PASSWORD example_password
# Copy custom configuration file to the container (optional)
# COPY ./my-custom.cnf /etc/mysql/conf.d/
COPY tables.sql /docker-entrypoint-initdb.d

# Copy initialization scripts (if any)
COPY . /docker-entrypoint-initdb.d
# Expose the default MySQL port
EXPOSE 3306

# Command to run the MySQL server
CMD ["mysqld"]
File renamed without changes.
40 changes: 25 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,59 @@ version: '3.8'
services:
frontend:
image: enblitztechnologies/healthnest:frontend
container_name: healthnest-frontend
ports:
- "3000:3000"
- "8000:3000"
depends_on:
- backend
networks:
- healthnest-network
environment:
- REACT_APP_BACKEND_URL=http://backend:8081
- WEBSOCKET_URL=ws://backend:8081/ws
- REACT_APP_BACKEND_URL=http://healthnest-backend:8081
- WEBSOCKET_URL=ws://healthnest-backend:8081/ws

backend:
image: enblitztechnologies/healthnest:backend
# image: enblitztechnologies/healthnest:backend
image: backend:latest
container_name: healthnest-backend
ports:
- "8081:8081"
depends_on:
mysql:
condition: service_healthy
environment:
DB_HOST: mysql
DB_USER: example_user
DB_PASSWORD: example_password
DB_DATABASE: test
DB_HOST: ${DB_HOST}
DB_USER: ${MYSQL_USER}
DB_PASSWORD: ${MYSQL_PASSWORD}
DB_DATABASE: ${MYSQL_DATABASE}
networks:
- healthnest-network
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:8081/ || exit 1"]
test: [ "CMD-SHELL", "curl -f http://localhost:8081/ || exit 1" ]
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
command: ["sh", "-c", "sleep 20 && npm start"]
command: [ "sh", "-c", "sleep 20 && npm start" ]

mysql:
image: enblitztechnologies/healthnest:mysql
container_name: healthnest-mysql
ports:
- "3307:3306" # Change this line to avoid port conflict
- "3307:3306" # Change this line to avoid port conflict
environment:
MYSQL_DATABASE: test
MYSQL_USER: example_user
MYSQL_PASSWORD: example_password
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- mysql-data:/var/lib/mysql
healthcheck:
test: ["CMD-SHELL", "mysqladmin ping -h localhost -p$MYSQL_PASSWORD"]
test:
[
"CMD-SHELL",
"mysqladmin ping -h localhost -p$MYSQL_PASSWORD"
]
interval: 10s
timeout: 5s
retries: 3
Expand All @@ -57,6 +66,7 @@ services:
volumes:
mysql-data:


networks:
healthnest-network:
driver: bridge
2 changes: 1 addition & 1 deletion frontend/src/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

export const BASE_URL = "http://localhost:8081";
export const BASE_URL = "http://healthnest-backend:8081";

5 changes: 5 additions & 0 deletions scripts/build-backend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cd backend
docker build -t backend .
docker tag backend:latest enblitztechnologies/healthnest:backend
cd ..
5 changes: 5 additions & 0 deletions scripts/build-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cd database
docker build -t db .
docker tag db:latest enblitztechnologies/healthnest:mysql
cd ..
5 changes: 5 additions & 0 deletions scripts/build-frontend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cd frontend
docker build -t frontend .
docker tag frontend:latest enblitztechnologies/healthnest:frontend
cd ..

0 comments on commit d3b0ba0

Please sign in to comment.