Skip to content

Commit

Permalink
feat: Add a full template to be used by the Openshift Catalog. (#188)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
lholmquist authored Sep 18, 2020
1 parent bb2e5aa commit 2640126
Show file tree
Hide file tree
Showing 8 changed files with 1,527 additions and 1,467 deletions.
530 changes: 530 additions & 0 deletions .openshift/template.json

Large diffs are not rendered by default.

24 changes: 4 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,19 @@ You can then start the application like this:
Then go to http://localhost:3000


#### Running on Minishift
#### Running on Openshift

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

Then create a new project using the `oc` commands

`oc new-project fun-node-fun`

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

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

Then run `npm run openshift` to deploy your app

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


This app has an example of integration test using an [integration test tool for Node.js apps on OpenShift](https://github.com/nodeshift/rhoaster)

Once you started your local OpenShift instance you can check it out by running the following commands:

```
npm run test:integration
```

It will deploy the app to local OpenShift and run the tests located on `test/integration` directory.

```
npm run test:integration:undeploy
```

Performs undeploy of the app inside local OpenShift.
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ const db = require('./lib/db');
const fruits = require('./lib/routes/fruits');

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

next();
Expand Down
34 changes: 24 additions & 10 deletions lib/db/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';
const {Pool} = require('pg');

const serviceHost = process.env.MY_DATABASE_SERVICE_HOST || 'localhost';
const user = process.env.DB_USERNAME || 'user';
const password = process.env.DB_PASSWORD || 'password';
const connectionString = `postgresql://${user}:${password}@${serviceHost}:5432/my_data`;
const serviceHost = process.env.MY_DATABASE_SERVICE_HOST || process.env.POSTGRESQL_SERVICE_HOST || 'localhost';
const user = process.env.DB_USERNAME || process.env.POSTGRESQL_USER || 'user';
const password = process.env.DB_PASSWORD || process.env.POSTGRESQL_PASSWORD || 'password';
const databaseName = process.env.POSTGRESQL_DATABASE || 'my_data';
const connectionString = `postgresql://${user}:${password}@${serviceHost}:5432/${databaseName}`;

const pool = new Pool({
connectionString
});

let didInitHappen = false;

// -- Create the products table if not present
const initScript = `CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
Expand All @@ -23,11 +26,22 @@ INSERT INTO products (name, stock) values ('Apple', 10);
INSERT INTO products (name, stock) values ('Orange', 10);
INSERT INTO products (name, stock) values ('Pear', 10);`;

module.exports = {
query: (text, params) => {
return pool.query(text, params);
},
init: () => {
return pool.query(initScript);
async function query (text, parameters) {
// Check that we have initialized the DB on each Query request
if (!didInitHappen) {
await init();
}

return pool.query(text, parameters);
}

function init () {
return pool.query(initScript).then(() => {
didInitHappen = true;
});
}

module.exports = {
query,
init
};
4 changes: 3 additions & 1 deletion lib/routes/fruits.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fruits = require('../api/fruits');

router.get('/fruits/:id', (request, response) => {
const {id} = request.params;
/* eslint unicorn/no-fn-reference-in-iterator: "off" */
fruits.find(id).then(result => {
if (result.rowCount === 0) {
response.status(404);
Expand All @@ -24,7 +25,8 @@ router.get('/fruits/:id', (request, response) => {
router.get('/fruits', (request, response) => {
fruits.findAll().then(results => {
response.send(results.rows);
}).catch(() => {
}).catch(error => {
console.log(error);
response.sendStatus(400);
});
});
Expand Down
1 change: 1 addition & 0 deletions lib/validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function validateCreateUpdateRequest (request, response, next) {
return response.send('The name is required!');
}

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

0 comments on commit 2640126

Please sign in to comment.