Skip to content

Commit d57bfa2

Browse files
Release 5.16.0 (Google Cloud Platform)
- Deployment on AppEngine google cloud platform - Add Swagger docs to all endpoints and models - Fix authentication middleware - Remove Procfile (Heroku)
1 parent 037e7d8 commit d57bfa2

File tree

16 files changed

+623
-153
lines changed

16 files changed

+623
-153
lines changed

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ Node API is production ready and open source project in Node, Express and MongoD
88

99
### Documentation
1010

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

1313
### Support
1414

15-
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
15+
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
1616

1717
### Servers
1818

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

2423
### Showcase
2524

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

3433
#### Projects
3534

36-
| URL | Collection | Method | Parameters | Response | Action |
37-
| ------------------------------------------------------- | ---------- | ------ | ---------- | --------------------------------------- | ------------------------------------------- |
38-
| https://rest-api-node.herokuapp.com/projects | projects | get | Nothing | JSON with Array | Get a list of projects in the database |
39-
| https://rest-api-node.herokuapp.com/projects/create | projects | post | JSON | JSON | Add JSON into the database |
40-
| https://rest-api-node.herokuapp.com/projects/update/:id | projects | put | id, JSON | Update the document with sent JSON data |
41-
| https://rest-api-node.herokuapp.com/projects/select/:id | projects | get | id | JSON | Return the document, related to the sent id |
42-
| https://rest-api-node.herokuapp.com/projects/delete/:id | projects | delete | id | status 200 | Delete the document, related to the sent id |
35+
| URL | Collection | Method | Parameters | Response | Action |
36+
| ----------------------------------------------------------------- | ---------- | ------ | ---------- | --------------------------------------- | ------------------------------------------- |
37+
| https://rest-api-node-413916.uc.r.appspot.com/projects | projects | get | Nothing | JSON with Array | Get a list of projects in the database |
38+
| https://rest-api-node-413916.uc.r.appspot.com/projects/create | projects | post | JSON | JSON | Add JSON into the database |
39+
| https://rest-api-node-413916.uc.r.appspot.com/projects/update/:id | projects | put | id, JSON | Update the document with sent JSON data |
40+
| https://rest-api-node-413916.uc.r.appspot.com/projects/select/:id | projects | get | id | JSON | Return the document, related to the sent id |
41+
| 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 |
4342

4443
#### Users
4544

46-
| URL | Collection | Method | Parameters | Response | Action |
47-
| ---------------------------------------------------- | ---------- | ------ | ---------- | --------------- | ------------------------------------------- |
48-
| https://rest-api-node.herokuapp.com/users | users | get | Nothing | JSON with Array | Get a list of users in the database |
49-
| https://rest-api-node.herokuapp.com/users/create | users | post | JSON | JSON | Add JSON into the database |
50-
| https://rest-api-node.herokuapp.com/users/update/:id | users | put | id | JSON | Update the document with sent JSON data |
51-
| https://rest-api-node.herokuapp.com/users/select/:id | users | get | id | JSON | Return the document, related to the sent id |
52-
| https://rest-api-node.herokuapp.com/users/delete/:id | users | delete | id | status 200 | Delete the document, related to the sent id |
45+
| URL | Collection | Method | Parameters | Response | Action |
46+
| -------------------------------------------------------------- | ---------- | ------ | ---------- | --------------- | ------------------------------------------- |
47+
| https://rest-api-node-413916.uc.r.appspot.com/users | users | get | Nothing | JSON with Array | Get a list of users in the database |
48+
| https://rest-api-node-413916.uc.r.appspot.com/users/create | users | post | JSON | JSON | Add JSON into the database |
49+
| https://rest-api-node-413916.uc.r.appspot.com/users/update/:id | users | put | id | JSON | Update the document with sent JSON data |
50+
| https://rest-api-node-413916.uc.r.appspot.com/users/select/:id | users | get | id | JSON | Return the document, related to the sent id |
51+
| 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 |
5352

5453
## Collections
5554

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

8079
## API
8180

82-
| Technology | Description | Link |
83-
| ---------------- | ----------------------- | ------------ |
84-
| Heroku | Cloud Platform | [heroku.com] |
85-
| Heroku mLab Dyno | MongoDB database server | [mlab.com] |
86-
| GitHub | Version Controlling | [github.com] |
81+
| Technology | Description | Link |
82+
| ------------- | ----------------------- | ------------------ |
83+
| AppEngine | Google Cloud Platform | [cloud.google.com] |
84+
| MongoDB Atlas | MongoDB database server | [mongodb.com] |
85+
| GitHub | Version Controlling | [github.com] |
8786

8887
## API Directories
8988

90-
- Routes `./src/routes`
89+
- Public Routes `./src/routes/public`
90+
- Private Routes `./src/routes/private`
9191
- Models `./src/app/Models`
9292
- Controllers `./src/app/Controllers`
9393
- Services `./src/app/Services`
@@ -112,7 +112,8 @@ AUTH_SECRET=docssecret
112112
```
113113

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

117118
## API Dependencies
118119

app.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
runtime: nodejs
2+
api_version: "1.0"
3+
env: flexible
4+
threadsafe: true
5+
automatic_scaling:
6+
cool_down_period_sec: 180
7+
min_num_instances: 1
8+
max_num_instances: 2
9+
cpu_utilization:
10+
target_utilization: 0.9
11+
network:
12+
name: default
13+
resources:
14+
cpu: 2
15+
memory_gb: 4.6
16+
disk_size_gb: 10
17+
liveness_check:
18+
initial_delay_sec: "300"
19+
check_interval_sec: "30"
20+
timeout_sec: "4"
21+
failure_threshold: 4
22+
success_threshold: 2
23+
readiness_check:
24+
check_interval_sec: "5"
25+
timeout_sec: "4"
26+
failure_threshold: 2
27+
success_threshold: 2
28+
app_start_timeout_sec: "300"
29+
service_account: [email protected]

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"name": "node_api",
3-
"version": "4.14.0",
3+
"version": "5.16.0",
44
"description": "Node API is production ready and open source project in Node, Express, MongoDB",
55
"scripts": {
6-
"start": "node server.js"
6+
"gcp-build": "",
7+
"start": "node server.js",
8+
"deploy": "gcloud app deploy app.yaml --version=0-1-3"
79
},
810
"author": "Renan Lopes",
911
"license": "MIT",
@@ -19,8 +21,5 @@
1921
"nodemailer": "^6.9.9",
2022
"swagger-jsdoc": "^4.3.2",
2123
"swagger-ui-express": "^4.1.6"
22-
},
23-
"engines": {
24-
"node": "18.x"
2524
}
2625
}

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class App {
5757
};
5858

5959
buildCluster = () => {
60-
let workers = Number(process.env.WORKERS ?? 1);
60+
let workers = Number(process.env.WORKERS || 1);
6161

6262
if (process.env.AUTO_SCALE === "true") {
6363
workers = this.autoScale(workers);

src/app/Controllers/AuthController.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,57 @@ const jwt = require("jsonwebtoken");
44
const model = mongoose.model("User");
55

66
class AuthController {
7-
login = async (req, res) => {
7+
login = async (request, response) => {
88
try {
9-
const user = await model.findOne({ email: req.body.email });
10-
const match = await bcrypt.compareSync(req.body.password, user.password);
9+
const user = await model.findOne({ email: request.body.email });
10+
const match = await bcrypt.compareSync(
11+
request.body.password,
12+
user.password
13+
);
1114

12-
let token;
1315
if (!match) {
14-
res.status(401).send({ error: "error", message: "Password mismatch" });
15-
token = jwt.sign({ user_id: user._id }, process.env.AUTH_SECRET, {
16-
expiresIn: "3h",
17-
});
16+
response
17+
.status(401)
18+
.send({ error: "error", message: "Password mismatch" });
1819
}
1920

20-
res.status(200).json({
21+
const token = jwt.sign({ user_id: user._id }, process.env.AUTH_SECRET, {
22+
expiresIn: "3h",
23+
});
24+
25+
response.status(200).json({
2126
_id: user._id,
2227
nickname: user.nickname,
2328
username: user.username,
2429
photo: user.photo,
2530
email: user.email,
26-
token: token,
31+
token,
2732
});
2833
} catch (error) {
29-
res
34+
response
3035
.status(401)
3136
.json({ error: error, message: "Error, user does not exists" });
3237
}
3338
};
3439

35-
verifyToken = (req, res, next) => {
36-
const token = req.get("Authorization");
37-
if (token) {
40+
verifyToken = (request, response, next) => {
41+
const bearerFormat = request.get("authorization");
42+
43+
if (bearerFormat) {
44+
const token = bearerFormat.split(" ")[1];
45+
3846
try {
39-
let decoded = jwt.verify(token, process.env.AUTH_SECRET);
40-
req.user = decoded;
41-
next();
47+
const decoded = jwt.verify(token, process.env.AUTH_SECRET);
48+
request.user = decoded;
49+
50+
return next();
4251
} catch (error) {
43-
res.status(401).json({ error: error, message: "Invalid Token" });
52+
return response
53+
.status(401)
54+
.json({ error: error, message: "Invalid Token" });
4455
}
4556
} else {
46-
res.status(401).json("Token is required");
57+
response.status(401).json("Token is required");
4758
}
4859
};
4960
}

src/app/Controllers/ProjectController.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,50 @@ const mongoose = require("mongoose");
22
const model = mongoose.model("Project");
33

44
class ProjectController {
5-
async getAllProjects(_, res) {
5+
async getAllProjects(request, response) {
66
try {
77
const projects = await model.find({});
8-
res.status(200).json(projects);
8+
return response.status(200).json(projects);
99
} catch (error) {
10-
res.status(500).json(error);
10+
return response.status(500).json(error);
1111
}
1212
}
1313

14-
async createProject(req, res) {
14+
async createProject(request, response) {
1515
try {
16-
const user = await model.create(req.body);
17-
res.json(user);
16+
const user = await model.create(request.body);
17+
return response.json(user);
1818
} catch (error) {
19-
res.status(500).json(error);
19+
return response.status(500).json(error);
2020
}
2121
}
2222

23-
async updateProject(req, res) {
24-
const { params, body } = req;
23+
async updateProject(request, response) {
24+
const { params, body } = request;
2525
try {
2626
const project = await model.findByIdAndUpdate(params.id, body);
27-
res.status(200).json(project);
27+
return response.status(200).json(project);
2828
} catch (error) {
29-
res.status(404).json(error);
29+
return response.status(404).json(error);
3030
}
3131
}
3232

33-
async searchProjectById(req, res) {
33+
async searchProjectById(request, response) {
3434
try {
35-
const project = await model.findById(req.params.id);
36-
res.status(200).json(project);
35+
const project = await model.findById(request.params.id);
36+
return response.status(200).json(project);
3737
} catch (error) {
38-
res.status(404).json(error);
38+
return response.status(404).json(error);
3939
}
4040
}
4141

42-
async deleteProjectById(req, res) {
42+
async deleteProjectById(request, response) {
4343
try {
44-
await model.remove({ _id: req.params.id });
45-
res.status(200);
44+
await model.deleteOne({ _id: request.params.id });
45+
46+
return response.status(200).json({ message: "Deleted successfully" });
4647
} catch (error) {
47-
res.status(404).json(error);
48+
return response.status(404).json(error);
4849
}
4950
}
5051
}

0 commit comments

Comments
 (0)