initial commit
This commit is contained in:
20
dev/tekton/examples/example-bank/bank-knative-service/Dockerfile
Executable file
20
dev/tekton/examples/example-bank/bank-knative-service/Dockerfile
Executable file
@@ -0,0 +1,20 @@
|
||||
# Use the official lightweight Node.js 12 image.
|
||||
# https://hub.docker.com/_/node
|
||||
FROM node:12-slim
|
||||
|
||||
# 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 ./
|
||||
|
||||
# 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" ]
|
||||
28
dev/tekton/examples/example-bank/bank-knative-service/deployment.yaml
Executable file
28
dev/tekton/examples/example-bank/bank-knative-service/deployment.yaml
Executable file
@@ -0,0 +1,28 @@
|
||||
apiVersion: serving.knative.dev/v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: process-transaction
|
||||
# local to cluster only
|
||||
labels:
|
||||
serving.knative.dev/visibility: cluster-local
|
||||
spec:
|
||||
template:
|
||||
metadata:
|
||||
annotations:
|
||||
# Target 10 requests in-flight per pod.
|
||||
autoscaling.knative.dev/target: "10"
|
||||
# Disable scale to zero with a minScale of 1.
|
||||
# autoscaling.knative.dev/minScale: "1"
|
||||
# Limit scaling to 50 pods.
|
||||
# autoscaling.knative.dev/maxScale: "50"
|
||||
spec:
|
||||
containers:
|
||||
- image: anthonyamanse/knative-transaction-process:with-auth
|
||||
envFrom:
|
||||
- secretRef:
|
||||
name: bank-oidc-adminuser
|
||||
- secretRef:
|
||||
name: mobile-simulator-secrets
|
||||
env:
|
||||
- name: TRANSACTION_SERVICE_URL
|
||||
value: "http://transaction-service:9080/bank/v1/transactions"
|
||||
112
dev/tekton/examples/example-bank/bank-knative-service/index.js
Executable file
112
dev/tekton/examples/example-bank/bank-knative-service/index.js
Executable file
@@ -0,0 +1,112 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const axios = require('axios');
|
||||
const qs = require('qs');
|
||||
const jwt_decode = require('jwt-decode')
|
||||
|
||||
let transactionServiceUrl = process.env.TRANSACTION_SERVICE_URL
|
||||
let appIdTokenUrl = process.env.APP_ID_TOKEN_URL
|
||||
let appIdClientId = process.env.APP_ID_CLIENT_ID
|
||||
let appIdClientSecret = process.env.APP_ID_CLIENT_SECRET
|
||||
let appIdAdminUser = process.env.APP_ID_ADMIN_USER
|
||||
let appIdAdminPassword = process.env.APP_ID_ADMIN_PASSWORD
|
||||
|
||||
let appIdResult;
|
||||
|
||||
app.post('/process', (req, res) => {
|
||||
console.log('received request')
|
||||
console.log(req.query)
|
||||
if (!appIdResult) {
|
||||
getAppIdToken(appIdAdminUser, appIdAdminPassword)
|
||||
.then(function (response) {
|
||||
appIdResult = response.data
|
||||
sendToRewardEndpoint(req, res, appIdResult.access_token)
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error)
|
||||
res.status('404').send('Error getting admin token')
|
||||
})
|
||||
} else {
|
||||
console.log('found app id result in global variable')
|
||||
// check if token is expired
|
||||
if (isAccessTokenExpired(appIdResult.access_token)) {
|
||||
console.log('token found is expired. getting new one...')
|
||||
getAppIdToken(appIdAdminUser, appIdAdminPassword)
|
||||
.then(function (response) {
|
||||
appIdResult = response.data
|
||||
sendToRewardEndpoint(req, res, appIdResult.access_token)
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error)
|
||||
res.status('404').send('Error getting admin token')
|
||||
})
|
||||
} else {
|
||||
sendToRewardEndpoint(req, res, appIdResult.access_token)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function sendToRewardEndpoint(req, res, authToken) {
|
||||
if (req.query.transactionId && req.query.category && req.query.amount) {
|
||||
let pointsEarned = computeReward(req.query.category, req.query.amount);
|
||||
axios({
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + authToken
|
||||
},
|
||||
method: 'put',
|
||||
url: transactionServiceUrl + '/reward/' + req.query.transactionId,
|
||||
data: {
|
||||
pointsEarned
|
||||
}
|
||||
})
|
||||
.then(function (response) {
|
||||
if (response.status == '204') {
|
||||
res.status('200').send('OK')
|
||||
} else {
|
||||
console.log({status: error.response.status, data: error.response.data})
|
||||
res.status('404').send({result: 'Failed to post to transaction API', response })
|
||||
}
|
||||
}).catch(function (error) {
|
||||
console.log("Error in PUT /transactions/reward/{transactionId}")
|
||||
console.log({status: error.response.status, data: error.response.data})
|
||||
res.status('404').send({error})
|
||||
})
|
||||
} else {
|
||||
res.status('404').send('transactionId, category, and amount must be present in query parameters.')
|
||||
}
|
||||
}
|
||||
|
||||
function computeReward(category, amount) {
|
||||
return amount;
|
||||
}
|
||||
|
||||
function getAppIdToken(username, password) {
|
||||
let data = {
|
||||
username,
|
||||
password,
|
||||
grant_type: 'password'
|
||||
}
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: appIdTokenUrl + '/token',
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + Buffer.from(appIdClientId + ":" + appIdClientSecret).toString('base64'),
|
||||
'Content-Type' : 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: qs.stringify(data)
|
||||
})
|
||||
}
|
||||
|
||||
function isAccessTokenExpired(access_token) {
|
||||
if (new Date().getTime() - (jwt_decode(access_token).exp * 1000) >= 0) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const port = process.env.PORT || 8080;
|
||||
app.listen(port, () => {
|
||||
console.log('Hello world listening on port', port);
|
||||
});
|
||||
17
dev/tekton/examples/example-bank/bank-knative-service/package.json
Executable file
17
dev/tekton/examples/example-bank/bank-knative-service/package.json
Executable file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "bank-knative-service",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.2",
|
||||
"express": "^4.17.1",
|
||||
"jwt-decode": "^2.2.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user