Skip to content

Commit

Permalink
Production & Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasCode92 committed Apr 8, 2024
1 parent 3e08806 commit fc11cd9
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*/node_modules
server/public
.git
11 changes: 0 additions & 11 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,5 @@
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},

// Workbench
"workbench.colorTheme": "GitHub Dark Default",
"workbench.colorCustomizations": {
"activityBarBadge.background": "#1f6feb",
"icon.foreground": "#1f6feb",
"statusBar.foreground": "#1f6feb",
"statusBarItem.remoteBackground": "#26a641",
"statusBarItem.remoteForeground": "#0e4429",
"statusBarItem.remoteHoverForeground": "#000"
}
}
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

COPY client/package*.json client/
RUN npm run install:client --omit=dev

COPY server/package*.json server/
RUN npm run install:server --omit=dev

COPY client/ client/
RUN npm run build --prefix client

COPY server/ server/

USER node

CMD ["npm", "start", "--prefix", "server"]

EXPOSE 8000
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This project is part of the [Complete NodeJS Developer](https://www.udemy.com/co
**TL;DR**

```bash
# Include your MongoDB connection string (MONGO_URL) a server/.env file.
# Include your MongoDB connection string (MONGO_URL) a server/.env.local file.

npm run install # install client and server dependencies
npm run watch # start the client and server applications
Expand All @@ -26,7 +26,7 @@ The project consists of 3 npm packages
- **_nasa-project_**: This is the root project that manages both the client and server components.

**Architecture Diagram**<br />
<img src="docs/architecture_diagram_v3.png" style="height: 350px" />
<img src="docs/architecture_diagram_v4.png" style="height: 350px" />

To initiate the application in development mode, execute the following command in the project's root directory: `npm run watch`<br />
This command will launch the server on port 8000 and the client on port 3000. Make sure all the (client and server) dependencies are installed with `npm run install`.
Expand All @@ -40,7 +40,7 @@ To exclusively access the frontend, execute the command `npm run client` in the

The server is implemented as a Node.js and Express application, utilizing Kepler data as its primary source for identifying habitable planets. The process is detailed in the [planet-explorer](https://github.com/ThomasCode92/planet-explorer) repository.<br />
In addition to utilizing Kepler data, a [MongoDB](https://www.mongodb.com/) database is used to keep track of all missions created and aborted by the user. This information is accessible to the user through a user-friendly API, providing comprehensive data about their mission launches.<br />
To establish a connection with the database, insert your connection string, identified as _MONGO_URL_, into a `.env` file within the server project. For running tests, a separate test database is required. Therefore, add a _MONGO_TEST_URL_ within the same file.
To establish a connection with the database, insert your connection string, identified as _MONGO_URL_, into a `.env.local` file within the server project. For running tests, a separate test database is required. Therefore, add a _MONGO_TEST_URL_ within the same file.

```bash
MONGO_URL=mongodb+srv://..... # production database
Expand Down Expand Up @@ -69,3 +69,34 @@ For better performance and the ability to manage increased request loads, the se
npm run deploy:cluster # view the (clustered) application as it would deploy on a real server
npm run pm2 --prefix server <PM2 COMMAND> # Run a specific pm2 command on the server
```

## Dockerizing & Production

To containerize (and run) your project, follow these steps:

```bash
docker login
docker build -t <YOUR_USERNAME>/nasa-project .
docker push <YOUR_USERNAME>/nasa-project

# Start the application using docker
docker run -it -p 8000:8000 --env-file ./server/.env <YOUR_USERNAME>/nasa-project
```

Ensure to replace `<YOUR_USERNAME>` with your Docker Hub username.<br />
Production Setup

To containerize (and run) your project, follow these steps:

```bash
docker login
docker build -t <YOUR_USERNAME>/nasa-project .
docker run -it -p 8000:8000 --env-file ./server/.env <YOUR_USERNAME>/nasa-project
```

Ensure to replace _<YOUR_USERNAME>_ with your Docker Hub username.<br />
The `.env` file used to start the container should mirror the variables in your `.env.local` file, with an additional variable: `NODE_ENV=production`.

### Deployment

Once your image is on Docker Hub, you can deploy it on your cloud provider (such as EC2 on AWS). However, detailed steps for this process are beyond the scope of this demo project. For more information, please refer to the official documentation provided by your cloud provider.
5 changes: 4 additions & 1 deletion client/src/hooks/requests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const API_URL = 'http://localhost:8000/api/v1';
let API_URL = 'api/v1';

// If in watch mode, use the local server.
if (process.env.REACT_APP_WATCH_MODE === 'true') API_URL = 'http://localhost:8000/api/v1';

// Load planets and return as JSON.
async function httpGetPlanets() {
Expand Down
Binary file added docs/architecture_diagram_v4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/public
.env
.env.*
3 changes: 2 additions & 1 deletion server/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const dotenv = require('dotenv');

dotenv.config();
dotenv.config({ path: path.join(__dirname, '.env.local') });
50 changes: 15 additions & 35 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"cors": "^2.8.5",
"csv-parse": "^5.5.2",
"express": "^4.18.2",
"mongoose": "^8.2.4",
"morgan": "^1.10.0",
"pm2": "^5.3.1"
},
"devDependencies": {
"dotenv": "^16.4.1",
"jest": "^29.7.0",
"mongoose": "^8.1.1",
"nodemon": "^3.0.1",
"supertest": "^6.3.3"
}
Expand Down
9 changes: 6 additions & 3 deletions server/src/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const path = require('path');
const http = require('http');

const dotenv = require('dotenv');

const app = require('./app');
const { mongoConnect } = require('./services/mongo.service');

const { loadPlanetsData } = require('./models/planets.model');
const { loadLaunchData } = require('./models/launches.model');

dotenv.config();
// If not in production, load environment variables from .env.local file
if (process.env.NODE_ENV !== 'production') {
const dotenv = require('dotenv');
dotenv.config({ path: path.join(__dirname, '..', '.env.local') });
}

const PORT = process.env.PORT || 8000;

Expand Down

0 comments on commit fc11cd9

Please sign in to comment.