From c8bcc59b520c45696c6795bc7e832c737d45ea5c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 14 Jun 2024 12:07:34 -0700 Subject: [PATCH] Added docker compose, fixed all file paths, updated readme, fixed Async function bcrypt compare --- README.md | 90 +++++++++++++------ Server/.dockerignore | 1 + Server/.gitignore | 3 +- Server/controllers/authController.js | 1 + Server/docker/build_images.sh | 20 +++++ Server/docker/docker-compose.yaml | 15 +++- .../{mongo/Dockerfile => mongoDB.Dockerfile} | 0 .../{redis/Dockerfile => redis.Dockerfile} | 0 .../{server/Dockerfile => server.Dockerfile} | 2 +- Server/models/user.js | 5 +- 10 files changed, 101 insertions(+), 36 deletions(-) create mode 100644 Server/.dockerignore create mode 100755 Server/docker/build_images.sh rename Server/docker/{mongo/Dockerfile => mongoDB.Dockerfile} (100%) rename Server/docker/{redis/Dockerfile => redis.Dockerfile} (100%) rename Server/docker/{server/Dockerfile => server.Dockerfile} (85%) diff --git a/README.md b/README.md index 41c727c24..40c8ea677 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,15 @@ BlueWave uptime monitoring application 1. [Installation (Client)](#client) 2. [Configuration(Client)](#config-client) - [Environment](#env-vars-client) -3. [Installation (Server)](#server) -4. [Configuration(Server)](#config-server) - - [Environment](#env-vars-server) - - [Database](#databases) - - [Docker Images](#docker-images) -5. [Endpoints](#endpoints) +3. [Getting Started (Server)](#server) + - [Docker Compose Quickstart](#docker-quickstart) + - [Manual Installation](#manual-install) + - [Install Server](#install-server) + - [Environment](#env-vars-server) + - [Database](#databases) + - [(Optional) Dockerised Databases](#optional-docker-databases) + - [Start Server](#start-server) +4. [Endpoints](#endpoints) ###### Auth - POST [/api/v1/auth/register](#post-register) - POST [/api/v1/auth/login](#post-login) @@ -50,8 +53,8 @@ BlueWave uptime monitoring application - GET [/api/v1/alerts/{alertId}](#get-alert-alert-id) - POST [/api/v1/alerts/edit/{alertId}](#edit-alert) - POST [/api/v1/alerts/delete/{alertId}](#delete-alert) -6. [Error Handling](#error-handling) -7. [Contributors](#contributors) +5. [Error Handling](#error-handling) +6. [Contributors](#contributors) --- @@ -80,17 +83,43 @@ BlueWave uptime monitoring application --- -### Server +### Getting Started (Server) -#### Installation +#### Docker Quickstart + +The fastest way to start the server is to use our Dockerfiles and [Docker Compose](https://docs.docker.com/compose/). + +To get the server up and running you need to: + +1. In the `Server/docker` directory run the build script `build_images.sh` to build docker images for the server, Redis database, and MongoDB database. +2. In the `Server/docker` directory, create a `.env` file with the [requried environtmental variables](#env-vars-server). Sample file: + +``` +CLIENT_HOST="http://localhost:5173" +JWT_SECRET="my_secret" +DB_TYPE="MongoDB" +DB_CONNECTION_STRING="mongodb://mongodb:27017/uptime_db" +REDIS_HOST="redis" +REDIS_PORT=6379 +SYSTEM_EMAIL_ADDRESS="" +SENDGRID_API_KEY="" +LOGIN_PAGE_URL=" + +#### Manual Install + +##### Install Server 1. Change directory to the `Server` directory 2. Install all dependencies by running `npm install`
-#### Configuration - ##### Environmental Variables Configure the server with the following environmental variables: @@ -110,48 +139,51 @@ Configure the server with the following environmental variables:
-##### Databases +##### Databases This project requires a number of databases to run: 1. Main database for the application. This project includes an implementation for a MongoDB database as well as a MongoDB Docker image. 2. A Redis database is required for the Queue implementation in the PingService. This project includes a Redis docker image. -You may run your own databases locally, or you may use the docker images included in the project to get up and running quickly. +You may use the included Dockerfiles to spin up databases quickly if you wish. -###### (Optional) Running Docker Images +###### (Optional) Dockerised Databases -Docker images are located in `./Server/docker` +Dockerfiles for the server and databases are located in the `./Server/docker` directory
MongoDB Image -Located in `./Server/docker/mongo` +Location: `./Server/docker/mongoDB.Dockerfile` -The `./Server/docker/mongo/mongo_data` folder should be mounted to the MongoDB container in order to persist data. +The `./Server/docker/mongo/data` directory should be mounted to the MongoDB container in order to persist data. -From the `mongo` folder run +From the `Server/docker` directory run -1. Build the image: `docker build -t .` -2. Run the docker image: `docker run -d -p 27017:27017 -v $(pwd)/../mongo/mongo_data:/data/db --name uptime_database_mongo uptime_database_mongo` +1. Build the image: `docker build -f mongoDB.Dockerfile -t uptime_database_mongo .` +2. Run the docker image: `docker run -d -p 27017:27017 -v $(pwd)/mongo/data:/data/db --name uptime_database_mongo uptime_database_mongo`
Redis Image -Located in `./Server/docker/redis` +Location `./Server/docker/redislDockerfile` -the `./Server/docker/redis/redis_data` folder should be mounted to the Redis container in order to persist data. +the `./Server/docker/redis/data` directory should be mounted to the Redis container in order to persist data. -From the `Redis` folder run +From the `Server/docker` directory run -1. Build the image: `docker build -t ` -2. Run the image: `docker run -d -p 6379:6379 -v $(pwd)/../redis/redis_data:/data --name uptime_redis uptime_redis` +1. Build the image: `docker build -f redis.Dockerfile -t uptime_redis .` +2. Run the image: `docker run -d -p 6379:6379 -v $(pwd)/redis/data:/data --name uptime_redis uptime_redis`
-
-#### Starting the Development Server +##### Starting the Development Server -1. run `npm run dev` to start the development server +- run `npm run dev` to start the development server + +OR + +- run `node index.js` to start server --- diff --git a/Server/.dockerignore b/Server/.dockerignore new file mode 100644 index 000000000..d88f9054e --- /dev/null +++ b/Server/.dockerignore @@ -0,0 +1 @@ +./docker \ No newline at end of file diff --git a/Server/.gitignore b/Server/.gitignore index b509f7595..09d6b130b 100644 --- a/Server/.gitignore +++ b/Server/.gitignore @@ -3,4 +3,5 @@ node_modules *.log *.sh docker/mongo/data/* -docker/redis/data/* \ No newline at end of file +docker/redis/data/* +!docker/build_images.sh \ No newline at end of file diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 7730219ea..dcf816973 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -88,6 +88,7 @@ const loginController = async (req, res, next) => { const user = await req.db.getUserByEmail(req, res); // Compare password + const match = await user.comparePassword(req.body.password); if (match !== true) { throw new Error("Incorrect password"); diff --git a/Server/docker/build_images.sh b/Server/docker/build_images.sh new file mode 100755 index 000000000..94f1087a5 --- /dev/null +++ b/Server/docker/build_images.sh @@ -0,0 +1,20 @@ + +#!/bin/bash + +# Change directory to root Server directory for correct Docker Context +cd .. + +# MongoDB +mongoDB="./docker/mongoDB.Dockerfile" + +# Redis +redis="./docker/redis.Dockerfile" + +# Server +server="./docker/server.Dockerfile" + +docker build -f $mongoDB -t uptime_database_mongo . +docker build -f $redis -t uptime_redis . +docker build -f $server -t uptime_server . + +echo "All images built" \ No newline at end of file diff --git a/Server/docker/docker-compose.yaml b/Server/docker/docker-compose.yaml index ac882732e..e878a5338 100644 --- a/Server/docker/docker-compose.yaml +++ b/Server/docker/docker-compose.yaml @@ -1,4 +1,4 @@ -version: '3' +version: "3" services: server: image: uptime_server:latest @@ -10,6 +10,15 @@ services: - redis - mongodb redis: - image: uptime_redis + image: uptime_redis:latest + ports: + - "6379:6379" + volumes: + - ./redis/data:/data mongodb: - image: uptime_database_mongo \ No newline at end of file + image: uptime_database_mongo:latest + command: ["mongod", "--quiet"] + ports: + - "27017:27017" + volumes: + - ./mongo/data:/data/db diff --git a/Server/docker/mongo/Dockerfile b/Server/docker/mongoDB.Dockerfile similarity index 100% rename from Server/docker/mongo/Dockerfile rename to Server/docker/mongoDB.Dockerfile diff --git a/Server/docker/redis/Dockerfile b/Server/docker/redis.Dockerfile similarity index 100% rename from Server/docker/redis/Dockerfile rename to Server/docker/redis.Dockerfile diff --git a/Server/docker/server/Dockerfile b/Server/docker/server.Dockerfile similarity index 85% rename from Server/docker/server/Dockerfile rename to Server/docker/server.Dockerfile index 611a325a5..8a9bf04e3 100644 --- a/Server/docker/server/Dockerfile +++ b/Server/docker/server.Dockerfile @@ -1,4 +1,4 @@ -FROM node:20-alpine +FROM node:20 WORKDIR /app diff --git a/Server/models/user.js b/Server/models/user.js index 7f3945354..a40824399 100644 --- a/Server/models/user.js +++ b/Server/models/user.js @@ -46,8 +46,9 @@ UserSchema.pre("save", async function (next) { next(); }); -UserSchema.methods.comparePassword = function (submittedPassword) { - return bcrypt.compare(submittedPassword, this.password); +UserSchema.methods.comparePassword = async function (submittedPassword) { + res = await bcrypt.compare(submittedPassword, this.password); + return res; }; module.exports = mongoose.model("User", UserSchema);