restructure repo, separate sample app from docker configs

This commit is contained in:
sid palas
2023-01-27 09:26:02 -05:00
parent dd5d7bff0b
commit 08510ae883
58 changed files with 645 additions and 12 deletions

View File

@ -0,0 +1 @@
node_modules

View 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 .

View File

@ -0,0 +1,8 @@
```
npm init
npm install express pg
```
```
node src/index.js
```

File diff suppressed because it is too large Load Diff

View 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"
}
}

View 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 };

View 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');
});
});