restructure repo, separate sample app from docker configs
This commit is contained in:
1
05-example-web-application/api-node/.dockerignore
Normal file
1
05-example-web-application/api-node/.dockerignore
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
||||
12
05-example-web-application/api-node/Makefile
Normal file
12
05-example-web-application/api-node/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: run-local
|
||||
run-local:
|
||||
PGUSER=postgres \
|
||||
PGHOST=localhost \
|
||||
PGPASSWORD=foobarbaz \
|
||||
PGDATABASE=postgres \
|
||||
PGPORT=5432 \
|
||||
node ./src/index.js
|
||||
|
||||
.PHONY: build-naive
|
||||
build-naive:
|
||||
docker build --file ./Dockerfile.naive .
|
||||
8
05-example-web-application/api-node/README.md
Normal file
8
05-example-web-application/api-node/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
```
|
||||
npm init
|
||||
npm install express pg
|
||||
```
|
||||
|
||||
```
|
||||
node src/index.js
|
||||
```
|
||||
1150
05-example-web-application/api-node/package-lock.json
generated
Normal file
1150
05-example-web-application/api-node/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
05-example-web-application/api-node/package.json
Normal file
20
05-example-web-application/api-node/package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "api-node",
|
||||
"version": "1.0.0",
|
||||
"description": "simple api that connects to postgres",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"morgan": "^1.10.0",
|
||||
"pg": "^8.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.20"
|
||||
}
|
||||
}
|
||||
25
05-example-web-application/api-node/src/db.js
Normal file
25
05-example-web-application/api-node/src/db.js
Normal file
@ -0,0 +1,25 @@
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const pool = new Pool();
|
||||
|
||||
// the pool will emit an error on behalf of any idle clients
|
||||
// it contains if a backend error or network partition happens
|
||||
pool.on('error', (err, client) => {
|
||||
console.error('Unexpected error on idle client', err);
|
||||
process.exit(-1);
|
||||
});
|
||||
|
||||
// async/await - check out a client
|
||||
const getDateTime = async () => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const res = await client.query('SELECT NOW() as now;');
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
console.log(err.stack);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getDateTime };
|
||||
28
05-example-web-application/api-node/src/index.js
Normal file
28
05-example-web-application/api-node/src/index.js
Normal file
@ -0,0 +1,28 @@
|
||||
const { getDateTime } = require('./db');
|
||||
|
||||
const express = require('express');
|
||||
const morgan = require('morgan');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
// setup the logger
|
||||
app.use(morgan('tiny'));
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
const dateTime = await getDateTime();
|
||||
const response = dateTime;
|
||||
response.api = 'node';
|
||||
res.send(response);
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
debug('SIGTERM signal received: closing HTTP server');
|
||||
server.close(() => {
|
||||
debug('HTTP server closed');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user