Skip to content

Commit

Permalink
add POST to ratings service to demonstrate security policies on HTTP …
Browse files Browse the repository at this point in the history
…Methods (istio#10778)

* add POST to ratings service

* put a space between if and opening parenthesis

* add comments

* remove extra line-break
  • Loading branch information
vadimeisenbergibm authored and wenchenglu committed Feb 11, 2019
1 parent 428179f commit 6fad390
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions samples/bookinfo/src/ratings/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var dispatcher = require('httpdispatcher')

var port = parseInt(process.argv[2])

var userAddedRatings = [] // used to demonstrate POST functionality

/**
* We default to using mongodb, if DB_TYPE is not set to mysql.
*/
Expand All @@ -33,6 +35,34 @@ if (process.env.SERVICE_VERSION === 'v2') {
}
}

dispatcher.onPost(/^\/ratings\/[0-9]*/, function (req, res) {
var productIdStr = req.url.split('/').pop()
var productId = parseInt(productIdStr)
var ratings = {}

if (Number.isNaN(productId)) {
res.writeHead(400, {'Content-type': 'application/json'})
res.end(JSON.stringify({error: 'please provide numeric product ID'}))
return
}

try {
ratings = JSON.parse(req.body)
} catch (error) {
res.writeHead(400, {'Content-type': 'application/json'})
res.end(JSON.stringify({error: 'please provide valid ratings JSON'}))
return
}

if (process.env.SERVICE_VERSION === 'v2') { // the version that is backed by a database
res.writeHead(501, {'Content-type': 'application/json'})
res.end(JSON.stringify({error: 'Post not implemented for database backed ratings'}))
} else { // the version that holds ratings in-memory
res.writeHead(200, {'Content-type': 'application/json'})
res.end(JSON.stringify(putLocalReviews(productId, ratings)))
}
})

dispatcher.onGet(/^\/ratings\/[0-9]*/, function (req, res) {
var productIdStr = req.url.split('/').pop()
var productId = parseInt(productIdStr)
Expand Down Expand Up @@ -118,7 +148,18 @@ dispatcher.onGet('/health', function (req, res) {
res.end(JSON.stringify({status: 'Ratings is healthy'}))
})

function putLocalReviews (productId, ratings) {
userAddedRatings[productId] = {
id: productId,
ratings: ratings
}
return getLocalReviews(productId)
}

function getLocalReviews (productId) {
if (typeof userAddedRatings[productId] !== 'undefined') {
return userAddedRatings[productId]
}
return {
id: productId,
ratings: {
Expand Down

0 comments on commit 6fad390

Please sign in to comment.