Skip to content

Commit 2640126

Browse files
authored
feat: Add a full template to be used by the Openshift Catalog. (#188)
* feat: Add a full template to be used by the Openshift Catalog. This modifies the code slightly so that if the Node application starts first before the DB, the DB can get initialized if it is not. This also adds another template that is specific to the openshift catalog.
1 parent bb2e5aa commit 2640126

File tree

8 files changed

+1527
-1467
lines changed

8 files changed

+1527
-1467
lines changed

.openshift/template.json

+530
Large diffs are not rendered by default.

README.md

+4-20
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,19 @@ You can then start the application like this:
2424
Then go to http://localhost:3000
2525

2626

27-
#### Running on Minishift
27+
#### Running on Openshift
2828

29-
First, make sure you have minishift setup and are logged in using `oc login`.
29+
First, make sure you have an instance of Openshift setup and are logged in using `oc login`.
3030

3131
Then create a new project using the `oc` commands
3232

3333
`oc new-project fun-node-fun`
3434

35-
For this example, you will also need a postgres db running on your Minishift cluster.
35+
For this example, you will also need a postgres db running on your Openshift cluster.
3636

3737
`oc new-app -e POSTGRESQL_USER=luke -ePOSTGRESQL_PASSWORD=secret -ePOSTGRESQL_DATABASE=my_data centos/postgresql-10-centos7 --name=my-database`
3838

3939
Then run `npm run openshift` to deploy your app
4040

41-
Then you can navigate to the newly exposed route, something similar to "http://nodejs-rest-http-crud-boosters.192.168.99.100.nip.io/", this will probably be different based on your Minishift IP address
41+
Then you can navigate to the newly exposed route, something similar to "http://nodejs-rest-http-crud-boosters.192.168.99.100.nip.io/", this will probably be different based on your Openshift IP address
4242

43-
44-
This app has an example of integration test using an [integration test tool for Node.js apps on OpenShift](https://github.com/nodeshift/rhoaster)
45-
46-
Once you started your local OpenShift instance you can check it out by running the following commands:
47-
48-
```
49-
npm run test:integration
50-
```
51-
52-
It will deploy the app to local OpenShift and run the tests located on `test/integration` directory.
53-
54-
```
55-
npm run test:integration:undeploy
56-
```
57-
58-
Performs undeploy of the app inside local OpenShift.

app.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const db = require('./lib/db');
3030
const fruits = require('./lib/routes/fruits');
3131

3232
app.use(bodyParser.json());
33-
app.use((error, req, res, next) => {
34-
if (req.body === '' || (error instanceof SyntaxError && error.type === 'entity.parse.failed')) {
35-
res.status(415);
36-
return res.send('Invalid payload!');
33+
app.use((error, request, response, next) => {
34+
if (request.body === '' || (error instanceof SyntaxError && error.type === 'entity.parse.failed')) {
35+
response.status(415);
36+
return response.send('Invalid payload!');
3737
}
3838

3939
next();

lib/db/index.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
'use strict';
22
const {Pool} = require('pg');
33

4-
const serviceHost = process.env.MY_DATABASE_SERVICE_HOST || 'localhost';
5-
const user = process.env.DB_USERNAME || 'user';
6-
const password = process.env.DB_PASSWORD || 'password';
7-
const connectionString = `postgresql://${user}:${password}@${serviceHost}:5432/my_data`;
4+
const serviceHost = process.env.MY_DATABASE_SERVICE_HOST || process.env.POSTGRESQL_SERVICE_HOST || 'localhost';
5+
const user = process.env.DB_USERNAME || process.env.POSTGRESQL_USER || 'user';
6+
const password = process.env.DB_PASSWORD || process.env.POSTGRESQL_PASSWORD || 'password';
7+
const databaseName = process.env.POSTGRESQL_DATABASE || 'my_data';
8+
const connectionString = `postgresql://${user}:${password}@${serviceHost}:5432/${databaseName}`;
89

910
const pool = new Pool({
1011
connectionString
1112
});
1213

14+
let didInitHappen = false;
15+
1316
// -- Create the products table if not present
1417
const initScript = `CREATE TABLE IF NOT EXISTS products (
1518
id SERIAL PRIMARY KEY,
@@ -23,11 +26,22 @@ INSERT INTO products (name, stock) values ('Apple', 10);
2326
INSERT INTO products (name, stock) values ('Orange', 10);
2427
INSERT INTO products (name, stock) values ('Pear', 10);`;
2528

26-
module.exports = {
27-
query: (text, params) => {
28-
return pool.query(text, params);
29-
},
30-
init: () => {
31-
return pool.query(initScript);
29+
async function query (text, parameters) {
30+
// Check that we have initialized the DB on each Query request
31+
if (!didInitHappen) {
32+
await init();
3233
}
34+
35+
return pool.query(text, parameters);
36+
}
37+
38+
function init () {
39+
return pool.query(initScript).then(() => {
40+
didInitHappen = true;
41+
});
42+
}
43+
44+
module.exports = {
45+
query,
46+
init
3347
};

lib/routes/fruits.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const fruits = require('../api/fruits');
99

1010
router.get('/fruits/:id', (request, response) => {
1111
const {id} = request.params;
12+
/* eslint unicorn/no-fn-reference-in-iterator: "off" */
1213
fruits.find(id).then(result => {
1314
if (result.rowCount === 0) {
1415
response.status(404);
@@ -24,7 +25,8 @@ router.get('/fruits/:id', (request, response) => {
2425
router.get('/fruits', (request, response) => {
2526
fruits.findAll().then(results => {
2627
response.send(results.rows);
27-
}).catch(() => {
28+
}).catch(error => {
29+
console.log(error);
2830
response.sendStatus(400);
2931
});
3032
});

lib/validations/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function validateCreateUpdateRequest (request, response, next) {
1414
return response.send('The name is required!');
1515
}
1616

17+
/* eslint unicorn/prefer-number-properties: "off" */
1718
if (stock === null || isNaN(stock) || stock < 0) {
1819
response.status(422);
1920
return response.send('The stock must be greater or equal to 0!');

0 commit comments

Comments
 (0)