Skip to content

Commit

Permalink
Release 5.16.0 (Google Cloud Platform)
Browse files Browse the repository at this point in the history
- Deployment on AppEngine google cloud platform
- Add Swagger docs to all endpoints and models
- Fix authentication middleware
- Remove Procfile (Heroku)
  • Loading branch information
renanlopescoder committed Feb 11, 2024
1 parent 037e7d8 commit d57bfa2
Show file tree
Hide file tree
Showing 16 changed files with 623 additions and 153 deletions.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

55 changes: 28 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ Node API is production ready and open source project in Node, Express and MongoD

### Documentation

Swagger Docs https://rest-api-node.herokuapp.com/docs/
Swagger Docs https://rest-api-node-413916.uc.r.appspot.com/docs/

### Support

This application is hosted on Heroku and serve a bunch of AJAX requests to give and manipulate data from database and give a support to test front end applications
This application is hosted on Google Cloud Platfom and serve a bunch of AJAX requests to give and manipulate data from database and give a support to test front end applications

### Servers

| Server | Description | Api Url | Branch (GitHub) |
| ---------- | ----------------- | ---------------------------------------------- | --------------- |
| staging | Open Api | https://rest-api-node.herokuapp.com | master |
| production | Authenticated Api | https://rest-api-node-production.herokuapp.com | production |
| Server | Description | Api Url |
| ---------- | ------------------------------------- | --------------------------------------------- |
| App Engine | Rest API Node - Google Cloud Platform | https://rest-api-node-413916.uc.r.appspot.com |

### Showcase

Expand All @@ -33,23 +32,23 @@ This application is hosted on Heroku and serve a bunch of AJAX requests to give

#### Projects

| URL | Collection | Method | Parameters | Response | Action |
| ------------------------------------------------------- | ---------- | ------ | ---------- | --------------------------------------- | ------------------------------------------- |
| https://rest-api-node.herokuapp.com/projects | projects | get | Nothing | JSON with Array | Get a list of projects in the database |
| https://rest-api-node.herokuapp.com/projects/create | projects | post | JSON | JSON | Add JSON into the database |
| https://rest-api-node.herokuapp.com/projects/update/:id | projects | put | id, JSON | Update the document with sent JSON data |
| https://rest-api-node.herokuapp.com/projects/select/:id | projects | get | id | JSON | Return the document, related to the sent id |
| https://rest-api-node.herokuapp.com/projects/delete/:id | projects | delete | id | status 200 | Delete the document, related to the sent id |
| URL | Collection | Method | Parameters | Response | Action |
| ----------------------------------------------------------------- | ---------- | ------ | ---------- | --------------------------------------- | ------------------------------------------- |
| https://rest-api-node-413916.uc.r.appspot.com/projects | projects | get | Nothing | JSON with Array | Get a list of projects in the database |
| https://rest-api-node-413916.uc.r.appspot.com/projects/create | projects | post | JSON | JSON | Add JSON into the database |
| https://rest-api-node-413916.uc.r.appspot.com/projects/update/:id | projects | put | id, JSON | Update the document with sent JSON data |
| https://rest-api-node-413916.uc.r.appspot.com/projects/select/:id | projects | get | id | JSON | Return the document, related to the sent id |
| https://rest-api-node-413916.uc.r.appspot.com/projects/delete/:id | projects | delete | id | status 200 | Delete the document, related to the sent id |

#### Users

| URL | Collection | Method | Parameters | Response | Action |
| ---------------------------------------------------- | ---------- | ------ | ---------- | --------------- | ------------------------------------------- |
| https://rest-api-node.herokuapp.com/users | users | get | Nothing | JSON with Array | Get a list of users in the database |
| https://rest-api-node.herokuapp.com/users/create | users | post | JSON | JSON | Add JSON into the database |
| https://rest-api-node.herokuapp.com/users/update/:id | users | put | id | JSON | Update the document with sent JSON data |
| https://rest-api-node.herokuapp.com/users/select/:id | users | get | id | JSON | Return the document, related to the sent id |
| https://rest-api-node.herokuapp.com/users/delete/:id | users | delete | id | status 200 | Delete the document, related to the sent id |
| URL | Collection | Method | Parameters | Response | Action |
| -------------------------------------------------------------- | ---------- | ------ | ---------- | --------------- | ------------------------------------------- |
| https://rest-api-node-413916.uc.r.appspot.com/users | users | get | Nothing | JSON with Array | Get a list of users in the database |
| https://rest-api-node-413916.uc.r.appspot.com/users/create | users | post | JSON | JSON | Add JSON into the database |
| https://rest-api-node-413916.uc.r.appspot.com/users/update/:id | users | put | id | JSON | Update the document with sent JSON data |
| https://rest-api-node-413916.uc.r.appspot.com/users/select/:id | users | get | id | JSON | Return the document, related to the sent id |
| https://rest-api-node-413916.uc.r.appspot.com/users/delete/:id | users | delete | id | status 200 | Delete the document, related to the sent id |

## Collections

Expand Down Expand Up @@ -79,15 +78,16 @@ This application is hosted on Heroku and serve a bunch of AJAX requests to give

## API

| Technology | Description | Link |
| ---------------- | ----------------------- | ------------ |
| Heroku | Cloud Platform | [heroku.com] |
| Heroku mLab Dyno | MongoDB database server | [mlab.com] |
| GitHub | Version Controlling | [github.com] |
| Technology | Description | Link |
| ------------- | ----------------------- | ------------------ |
| AppEngine | Google Cloud Platform | [cloud.google.com] |
| MongoDB Atlas | MongoDB database server | [mongodb.com] |
| GitHub | Version Controlling | [github.com] |

## API Directories

- Routes `./src/routes`
- Public Routes `./src/routes/public`
- Private Routes `./src/routes/private`
- Models `./src/app/Models`
- Controllers `./src/app/Controllers`
- Services `./src/app/Services`
Expand All @@ -112,7 +112,8 @@ AUTH_SECRET=docssecret
```

- Run the server <code>npm start</code>
- Access in your browser <a href="http://localhost:3000/projects">http://localhost:3000/projects</a>
- Access in your browser <a href="http://localhost:8080/projects">http://localhost:8080/projects</a>
- For deployment is important to update app.yaml to your service_account and can be found on your GCP

## API Dependencies

Expand Down
29 changes: 29 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
runtime: nodejs
api_version: "1.0"
env: flexible
threadsafe: true
automatic_scaling:
cool_down_period_sec: 180
min_num_instances: 1
max_num_instances: 2
cpu_utilization:
target_utilization: 0.9
network:
name: default
resources:
cpu: 2
memory_gb: 4.6
disk_size_gb: 10
liveness_check:
initial_delay_sec: "300"
check_interval_sec: "30"
timeout_sec: "4"
failure_threshold: 4
success_threshold: 2
readiness_check:
check_interval_sec: "5"
timeout_sec: "4"
failure_threshold: 2
success_threshold: 2
app_start_timeout_sec: "300"
service_account: rest-api-node-413916@appspot.gserviceaccount.com
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "node_api",
"version": "4.14.0",
"version": "5.16.0",
"description": "Node API is production ready and open source project in Node, Express, MongoDB",
"scripts": {
"start": "node server.js"
"gcp-build": "",
"start": "node server.js",
"deploy": "gcloud app deploy app.yaml --version=0-1-3"
},
"author": "Renan Lopes",
"license": "MIT",
Expand All @@ -19,8 +21,5 @@
"nodemailer": "^6.9.9",
"swagger-jsdoc": "^4.3.2",
"swagger-ui-express": "^4.1.6"
},
"engines": {
"node": "18.x"
}
}
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class App {
};

buildCluster = () => {
let workers = Number(process.env.WORKERS ?? 1);
let workers = Number(process.env.WORKERS || 1);

if (process.env.AUTO_SCALE === "true") {
workers = this.autoScale(workers);
Expand Down
49 changes: 30 additions & 19 deletions src/app/Controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,57 @@ const jwt = require("jsonwebtoken");
const model = mongoose.model("User");

class AuthController {
login = async (req, res) => {
login = async (request, response) => {
try {
const user = await model.findOne({ email: req.body.email });
const match = await bcrypt.compareSync(req.body.password, user.password);
const user = await model.findOne({ email: request.body.email });
const match = await bcrypt.compareSync(
request.body.password,
user.password
);

let token;
if (!match) {
res.status(401).send({ error: "error", message: "Password mismatch" });
token = jwt.sign({ user_id: user._id }, process.env.AUTH_SECRET, {
expiresIn: "3h",
});
response
.status(401)
.send({ error: "error", message: "Password mismatch" });
}

res.status(200).json({
const token = jwt.sign({ user_id: user._id }, process.env.AUTH_SECRET, {
expiresIn: "3h",
});

response.status(200).json({
_id: user._id,
nickname: user.nickname,
username: user.username,
photo: user.photo,
email: user.email,
token: token,
token,
});
} catch (error) {
res
response
.status(401)
.json({ error: error, message: "Error, user does not exists" });
}
};

verifyToken = (req, res, next) => {
const token = req.get("Authorization");
if (token) {
verifyToken = (request, response, next) => {
const bearerFormat = request.get("authorization");

if (bearerFormat) {
const token = bearerFormat.split(" ")[1];

try {
let decoded = jwt.verify(token, process.env.AUTH_SECRET);
req.user = decoded;
next();
const decoded = jwt.verify(token, process.env.AUTH_SECRET);
request.user = decoded;

return next();
} catch (error) {
res.status(401).json({ error: error, message: "Invalid Token" });
return response
.status(401)
.json({ error: error, message: "Invalid Token" });
}
} else {
res.status(401).json("Token is required");
response.status(401).json("Token is required");
}
};
}
Expand Down
39 changes: 20 additions & 19 deletions src/app/Controllers/ProjectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,50 @@ const mongoose = require("mongoose");
const model = mongoose.model("Project");

class ProjectController {
async getAllProjects(_, res) {
async getAllProjects(request, response) {
try {
const projects = await model.find({});
res.status(200).json(projects);
return response.status(200).json(projects);
} catch (error) {
res.status(500).json(error);
return response.status(500).json(error);
}
}

async createProject(req, res) {
async createProject(request, response) {
try {
const user = await model.create(req.body);
res.json(user);
const user = await model.create(request.body);
return response.json(user);
} catch (error) {
res.status(500).json(error);
return response.status(500).json(error);
}
}

async updateProject(req, res) {
const { params, body } = req;
async updateProject(request, response) {
const { params, body } = request;
try {
const project = await model.findByIdAndUpdate(params.id, body);
res.status(200).json(project);
return response.status(200).json(project);
} catch (error) {
res.status(404).json(error);
return response.status(404).json(error);
}
}

async searchProjectById(req, res) {
async searchProjectById(request, response) {
try {
const project = await model.findById(req.params.id);
res.status(200).json(project);
const project = await model.findById(request.params.id);
return response.status(200).json(project);
} catch (error) {
res.status(404).json(error);
return response.status(404).json(error);
}
}

async deleteProjectById(req, res) {
async deleteProjectById(request, response) {
try {
await model.remove({ _id: req.params.id });
res.status(200);
await model.deleteOne({ _id: request.params.id });

return response.status(200).json({ message: "Deleted successfully" });
} catch (error) {
res.status(404).json(error);
return response.status(404).json(error);
}
}
}
Expand Down
Loading

0 comments on commit d57bfa2

Please sign in to comment.