Skip to content

Commit 3f524eb

Browse files
author
Dan McGhan
committed
Added files for part 3 - handling get requests
1 parent 5a8d57e commit 3f524eb

File tree

10 files changed

+663
-0
lines changed

10 files changed

+663
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
hrPool: {
3+
user: process.env.HR_USER,
4+
password: process.env.HR_PASSWORD,
5+
connectString: process.env.HR_CONNECTIONSTRING,
6+
poolMin: 10,
7+
poolMax: 10,
8+
poolIncrement: 0
9+
}
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
port: process.env.HTTP_PORT || 3000
3+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const employees = require('../db_apis/employees.js');
2+
3+
async function get(req, res, next) {
4+
try {
5+
const context = {};
6+
7+
context.id = Number(req.params.id);
8+
9+
const rows = await employees.find(context);
10+
11+
if (req.params.id) {
12+
if (rows.length === 1) {
13+
res.status(200).json(rows[0]);
14+
} else {
15+
res.status(404).end();
16+
}
17+
} else {
18+
res.status(200).json(rows);
19+
}
20+
} catch (err) {
21+
next(err);
22+
}
23+
}
24+
25+
module.exports.get = get;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const database = require('../services/database.js');
2+
3+
const baseQuery =
4+
`select employee_id "id",
5+
first_name "first_name",
6+
last_name "last_name",
7+
email "email",
8+
phone_number "phone_number",
9+
hire_date "hire_date",
10+
job_id "job_id",
11+
salary "salary",
12+
commission_pct "commission_pct",
13+
manager_id "manager_id",
14+
department_id "department_id"
15+
from employees`;
16+
17+
async function find(context) {
18+
let query = baseQuery;
19+
const binds = {};
20+
21+
if (context.id) {
22+
binds.employee_id = context.id;
23+
24+
query += `\nwhere employee_id = :employee_id`;
25+
}
26+
27+
const result = await database.simpleExecute(query, binds);
28+
29+
return result.rows;
30+
}
31+
32+
module.exports.find = find;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const webServer = require('./services/web-server.js');
2+
const database = require('./services/database.js');
3+
const dbConfig = require('./config/database.js');
4+
const defaultThreadPoolSize = 4;
5+
6+
// Increase thread pool size by poolMax
7+
process.env.UV_THREADPOOL_SIZE = dbConfig.hrPool.poolMax + defaultThreadPoolSize;
8+
9+
async function startup() {
10+
console.log('Starting application');
11+
12+
try {
13+
console.log('Initializing database module');
14+
15+
await database.initialize();
16+
} catch (err) {
17+
console.error(err);
18+
19+
process.exit(1); // Non-zero failure code
20+
}
21+
22+
try {
23+
console.log('Initializing web server module');
24+
25+
await webServer.initialize();
26+
} catch (err) {
27+
console.error(err);
28+
29+
process.exit(1); // Non-zero failure code
30+
}
31+
}
32+
33+
startup();
34+
35+
async function shutdown(e) {
36+
let err = e;
37+
38+
console.log('Shutting down application');
39+
40+
try {
41+
console.log('Closing web server module');
42+
43+
await webServer.close();
44+
} catch (e) {
45+
console.error(e);
46+
47+
err = err || e;
48+
}
49+
50+
try {
51+
console.log('Closing database module');
52+
53+
await database.close();
54+
} catch (e) {
55+
console.error(e);
56+
57+
err = err || e;
58+
}
59+
60+
console.log('Exiting process');
61+
62+
if (err) {
63+
process.exit(1); // Non-zero failure code
64+
} else {
65+
process.exit(0);
66+
}
67+
}
68+
69+
process.on('SIGTERM', () => {
70+
console.log('Received SIGTERM');
71+
72+
shutdown();
73+
});
74+
75+
process.on('SIGINT', () => {
76+
console.log('Received SIGINT');
77+
78+
shutdown();
79+
});
80+
81+
process.on('uncaughtException', err => {
82+
console.log('Uncaught exception');
83+
console.error(err);
84+
85+
shutdown(err);
86+
});

0 commit comments

Comments
 (0)