gcloud components install beta
gcloud components update
touch .npmrc package.json index.js Dockerfile
edit .npmrc
@sap:registry=https://npm.sap.com
{
"name": "knative-accessing-hana",
"version": "1.0.0",
"description": "Post-conference curiosity",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Lucia",
"dependencies": {
"@sap/hana-client": "^2.4.126",
"express": "^4.16.4"
}
}
'use strict';
const dbClient = require('@sap/hana-client');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
const hanaConnection = () => {
return new Promise((resolve, reject) => {
const host = process.env.HANA_HOST || '133.456.789.452:39015';
const user = process.env.HANA_USR || 'SQLDEV';
const password = process.env.HANA_PWD || 'nOTp455w0Rd';
const hdbParams = {
serverNode: host,
uid: user,
pwd: password
};
var conn = dbClient.createConnection();
conn.connect(hdbParams, (err) => {
if (err) {
res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
return reject();
}
return resolve(conn);
});
});
};
const readFood = (conn, results) => {
return new Promise((resolve, reject) => {
let searchParam = req.query.searchFood;
if (typeof searchParam === "undefined" || searchParam === null) {
searchParam = 'Nutella';
}
console.log('Parameter ' + searchParam);
let query = 'SELECT TOP 10 * FROM "FOOD"."COMM_FOODS" where NUTRITION_GRADE_FR IS NULL ' +
'AND ENERGY_100G > 0 ' +
'AND contains(product_name, ? , fuzzy(0.8));'
console.log('Query :' + query);
let stmt = conn.prepare( query );
stmt.exec([searchParam], (err, results) => {
if (err) {
res.type("text/plain").status(500).send(`ERROR executing statement: ${err.toString()}`);
return reject();
}
var result = JSON.stringify(results);
res.type("application/json").status(200).send(result);
return resolve();
});
});
};
hanaConnection()
.then( (conn) => {
readFood(conn)
.catch(err => {
return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
}) } )
.catch(err => {
return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('I am listening on port', port);
});
$ npm install express
$ npm config set @sap:registry https://npm.sap.com
$ npm install @sap/hana-client
# Use the official Node.js 10 image.
# https://hub.docker.com/_/node
FROM node:10
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package.json package*.json ./
COPY .npmrc ./
# Install production dependencies.
RUN npm install --only=production
# Copy local code to the container image.
COPY . .
# Run the web service on container startup.
CMD [ "npm", "start" ]
gcloud builds submit --tag gcr.io/[PROJECT-ID]/run-hana-run
gcloud beta run deploy --image gcr.io/PROJECY_ID/run-hana-run --set-env-vars=HANA_HOST=123.456.789.55:39015,HANA_USR=SQLUSER,HANA_PWD=SomePASSWORD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
26 | |
22 | |
19 | |
13 | |
10 | |
9 | |
9 | |
8 | |
7 | |
7 |