From f6e53712b751f8434f4b148289a1e4d39481689d Mon Sep 17 00:00:00 2001 From: Adrian Ursu Date: Tue, 3 Nov 2015 10:50:37 +0000 Subject: [PATCH 1/2] This fixes spring-cloud-samples/springbox-cloud#9 by getting a valid access token from the Auth server before making the requests. --- README.adoc | 11 +++- .../scripts/loadGraph.sh | 48 ++++++++++++++- .../scripts/loadLikes.sh | 19 +++--- .../scripts/loadMovies.sh | 13 +++-- .../scripts/loadPeople.sh | 9 +-- springbox-reviews/scripts/loadReviews.sh | 58 ++++++++++++++++--- 6 files changed, 125 insertions(+), 33 deletions(-) diff --git a/README.adoc b/README.adoc index 1942632..76cbbda 100644 --- a/README.adoc +++ b/README.adoc @@ -20,9 +20,16 @@ .. `springbox-recommendations` .. `springbox-api-gateway` -. Load data into the reviews DB by running `springbox-reviews/scripts/loadReviews.sh`. +. For the scripts to work, you nned to install `jq` which is a sed-like tool for working with JSON data. You can find it [here](https://stedolan.github.io/jq/download/). +. Load data into the reviews DB by running springbox-reviews/scripts/loadReviews.sh. -. Load data into the recommendations DB by running `springbox-recommendations/scripts/loadGraph.sh`. + cd springbox-reviews/scripts/ + ./loadReviews.sh localhost 9999 localhost 8081 + +. Load data into the recommendations DB by running springbox-recommendations/scripts/loadGraph.sh. + + cd springbox-recommendations/scripts + ./loadGraph.sh localhost 9999 localhost 8082 . Visit http://localhost:9000 and you should see something like the following: + diff --git a/springbox-recommendations/scripts/loadGraph.sh b/springbox-recommendations/scripts/loadGraph.sh index f3d46ff..26fee77 100755 --- a/springbox-recommendations/scripts/loadGraph.sh +++ b/springbox-recommendations/scripts/loadGraph.sh @@ -1,4 +1,46 @@ #!/bin/bash -./loadPeople.sh -./loadMovies.sh -./loadLikes.sh \ No newline at end of file + +usage() { + echo "Error: missing argument" + echo + echo "Usage: $0 [OAUTH_SERVER_HOSTNAME] [OAUTH_SERVER_PORT] [RECOMMENDATIONS_HOSTNAME] [RECOMMENDATIONS_PORT]" + echo " eg. $0 localhost 9999 localhost 8082" + exit 1 +} + +header() { + echo "================================================================================" + echo $@ | sed -e :a -e 's/^.\{1,77\}$/ & /;ta' + echo "================================================================================" + +} + +OAUTH_SERVER_HOSTNAME="$1" +if [ "$OAUTH_SERVER_HOSTNAME" == "" ] ; then + usage +fi + +OAUTH_SERVER_PORT="$2" + if [ "$OAUTH_SERVER_PORT" == "" ] ; then + usage + fi + +RECOMMENDATIONS_HOSTNAME="$3" + if [ "$RECOMMENDATIONS_HOSTNAME" == "" ] ; then + usage + fi +RECOMMENDATIONS_PORT="$4" + if [ "$RECOMMENDATIONS_PORT" == "" ] ; then + usage + fi + +header "Getting access token from server..." +TOKEN=$(curl -X POST -d "grant_type=password&username=mstine&password=secret" http://${OAUTH_SERVER_HOSTNAME}:${OAUTH_SERVER_PORT}/uaa/oauth/token -H "Authorization: Basic YWNtZTphY21lc2VjcmV0" | jq -r '.access_token') +echo "access_token: ${TOKEN}" + +header "Loading People..." +./loadPeople.sh ${TOKEN} ${RECOMMENDATIONS_HOSTNAME} ${RECOMMENDATIONS_PORT} +header "Loading Movies..." +./loadMovies.sh ${TOKEN} ${RECOMMENDATIONS_HOSTNAME} ${RECOMMENDATIONS_PORT} +header "Loading Likes..." +./loadLikes.sh ${TOKEN} ${RECOMMENDATIONS_HOSTNAME} ${RECOMMENDATIONS_PORT} diff --git a/springbox-recommendations/scripts/loadLikes.sh b/springbox-recommendations/scripts/loadLikes.sh index 367155e..1be010f 100755 --- a/springbox-recommendations/scripts/loadLikes.sh +++ b/springbox-recommendations/scripts/loadLikes.sh @@ -1,10 +1,11 @@ #!/bin/bash -ROUTE=${ROUTE:-localhost:8082} -curl -X POST ${ROUTE}/recommendations/mstine/likes/1 -curl -X POST ${ROUTE}/recommendations/mstine/likes/2 -curl -X POST ${ROUTE}/recommendations/starbuxman/likes/2 -curl -X POST ${ROUTE}/recommendations/starbuxman/likes/4 -curl -X POST ${ROUTE}/recommendations/starbuxman/likes/5 -curl -X POST ${ROUTE}/recommendations/littleidea/likes/2 -curl -X POST ${ROUTE}/recommendations/littleidea/likes/3 -curl -X POST ${ROUTE}/recommendations/littleidea/likes/5 +TOKEN="$1" +ROUTE="$2:$3" +curl -X POST ${ROUTE}/recommendations/mstine/likes/1 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/mstine/likes/2 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/starbuxman/likes/2 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/starbuxman/likes/4 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/starbuxman/likes/5 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/littleidea/likes/2 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/littleidea/likes/3 -H "Authorization: Bearer ${TOKEN}" +curl -X POST ${ROUTE}/recommendations/littleidea/likes/5 -H "Authorization: Bearer ${TOKEN}" diff --git a/springbox-recommendations/scripts/loadMovies.sh b/springbox-recommendations/scripts/loadMovies.sh index 94b11cc..a65e2f1 100755 --- a/springbox-recommendations/scripts/loadMovies.sh +++ b/springbox-recommendations/scripts/loadMovies.sh @@ -1,7 +1,8 @@ #!/bin/bash -ROUTE=${ROUTE:-localhost:8082} -curl ${ROUTE}/movies -X POST -d '{"mlId":"1","title":"Toy Story (1995)"}' -H "Content-Type: application/json" -curl ${ROUTE}/movies -X POST -d '{"mlId":"2","title":"GoldenEye (1995)"}' -H "Content-Type: application/json" -curl ${ROUTE}/movies -X POST -d '{"mlId":"3","title":"Four Rooms (1995)"}' -H "Content-Type: application/json" -curl ${ROUTE}/movies -X POST -d '{"mlId":"4","title":"Get Shorty (1995)"}' -H "Content-Type: application/json" -curl ${ROUTE}/movies -X POST -d '{"mlId":"5","title":"Copycat (1995)"}' -H "Content-Type: application/json" \ No newline at end of file +TOKEN="$1" +ROUTE="$2:$3" +curl ${ROUTE}/movies -X POST -d '{"mlId":"1","title":"Toy Story (1995)"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/movies -X POST -d '{"mlId":"2","title":"GoldenEye (1995)"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/movies -X POST -d '{"mlId":"3","title":"Four Rooms (1995)"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/movies -X POST -d '{"mlId":"4","title":"Get Shorty (1995)"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/movies -X POST -d '{"mlId":"5","title":"Copycat (1995)"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" diff --git a/springbox-recommendations/scripts/loadPeople.sh b/springbox-recommendations/scripts/loadPeople.sh index cf79373..12840ad 100755 --- a/springbox-recommendations/scripts/loadPeople.sh +++ b/springbox-recommendations/scripts/loadPeople.sh @@ -1,5 +1,6 @@ #!/bin/bash -ROUTE=${ROUTE:-localhost:8082} -curl ${ROUTE}/people -X POST -d '{"userName":"mstine","firstName":"Matt","lastName":"Stine"}' -H "Content-Type: application/json" -curl ${ROUTE}/people -X POST -d '{"userName":"starbuxman","firstName":"Josh","lastName":"Long"}' -H "Content-Type: application/json" -curl ${ROUTE}/people -X POST -d '{"userName":"littleidea","firstName":"Andrew","lastName":"Shafer"}' -H "Content-Type: application/json" \ No newline at end of file +TOKEN="$1" +ROUTE="$2:$3" +curl ${ROUTE}/people -X POST -d '{"userName":"mstine","firstName":"Matt","lastName":"Stine"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/people -X POST -d '{"userName":"starbuxman","firstName":"Josh","lastName":"Long"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" +curl ${ROUTE}/people -X POST -d '{"userName":"littleidea","firstName":"Andrew","lastName":"Shafer"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" diff --git a/springbox-reviews/scripts/loadReviews.sh b/springbox-reviews/scripts/loadReviews.sh index 57f1df4..8e072af 100755 --- a/springbox-reviews/scripts/loadReviews.sh +++ b/springbox-reviews/scripts/loadReviews.sh @@ -1,10 +1,50 @@ #!/bin/bash -ROUTE=${ROUTE:-localhost:8081} -curl ${ROUTE}/reviews -X POST -d '{"userName":"mstine","mlId":"1","title":"Toy Story (1995)","review":"Great movie!","rating":"5"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"mstine","mlId":"2","title":"GoldenEye (1995)","review":"Pretty good...","rating":"3"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"starbuxman","mlId":"2","title":"GoldenEye (1995)","review":"BOND BOND BOND!","rating":"5"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"starbuxman","mlId":"4","title":"Get Shorty (1995)","review":"Meh","rating":"3" }}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"starbuxman","mlId":"5","title":"Copycat (1995)","review":"Nicely done!","rating":"4"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"littleidea","mlId":"2","title":"GoldenEye (1995)","review":"Good show!","rating":"4"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"littleidea","mlId":"3","title":"Four Rooms (1995)","review":"Could have been better...","rating":"3"}' -H "Content-Type: application/json" -curl ${ROUTE}/reviews -X POST -d '{"userName":"littleidea","mlId":"5","title":"Copycat (1995)","review":"Nicely done!","rating":"4"}' -H "Content-Type: application/json" \ No newline at end of file + +usage() { + echo "Error: missing argument" + echo + echo "Usage: $0 [OAUTH_SERVER_HOSTNAME] [OAUTH_SERVER_PORT] [REVIEWS_HOSTNAME] [REVIEWS_PORT]" + echo " eg. $0 localhost 9999 localhost 8081" + exit 1 +} + +header() { + echo "================================================================================" + echo $@ | sed -e :a -e 's/^.\{1,77\}$/ & /;ta' + echo "================================================================================" + +} + +OAUTH_SERVER_HOSTNAME="$1" +if [ "$OAUTH_SERVER_HOSTNAME" == "" ] ; then + usage +fi + +OAUTH_SERVER_PORT="$2" + if [ "$OAUTH_SERVER_PORT" == "" ] ; then + usage + fi + +REVIEWS_HOSTNAME="$3" + if [ "$REVIEWS_HOSTNAME" == "" ] ; then + usage + fi +REVIEWS_PORT="$4" + if [ "$REVIEWS_PORT" == "" ] ; then + usage + fi + +header "Getting access token from server..." +TOKEN=$(curl -X POST -d "grant_type=password&username=mstine&password=secret" http://${OAUTH_SERVER_HOSTNAME}:${OAUTH_SERVER_PORT}/uaa/oauth/token -H "Authorization: Basic YWNtZTphY21lc2VjcmV0" | jq -r '.access_token') +echo "access_token: ${TOKEN}" + +ROUTE=${REVIEWS_HOSTNAME}:${REVIEWS_PORT} + +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"mstine","mlId":"1","title":"Toy Story (1995)","review":"Great movie!","rating":"5"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"mstine","mlId":"2","title":"GoldenEye (1995)","review":"Pretty good...","rating":"3"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"starbuxman","mlId":"2","title":"GoldenEye (1995)","review":"BOND BOND BOND!","rating":"5"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"starbuxman","mlId":"4","title":"Get Shorty (1995)","review":"Meh","rating":"3" }}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"starbuxman","mlId":"5","title":"Copycat (1995)","review":"Nicely done!","rating":"4"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"littleidea","mlId":"2","title":"GoldenEye (1995)","review":"Good show!","rating":"4"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"littleidea","mlId":"3","title":"Four Rooms (1995)","review":"Could have been better...","rating":"3"}' -H "Content-Type: application/json" +curl ${ROUTE}/reviews -H "Authorization: Bearer ${TOKEN}" -X POST -d '{"userName":"littleidea","mlId":"5","title":"Copycat (1995)","review":"Nicely done!","rating":"4"}' -H "Content-Type: application/json" From 15371270bf81a4a224f1f2c6187c89140cbe8262 Mon Sep 17 00:00:00 2001 From: Adrian Ursu Date: Tue, 3 Nov 2015 10:52:34 +0000 Subject: [PATCH 2/2] Fixed typo --- README.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index 76cbbda..5be0eb7 100644 --- a/README.adoc +++ b/README.adoc @@ -20,7 +20,7 @@ .. `springbox-recommendations` .. `springbox-api-gateway` -. For the scripts to work, you nned to install `jq` which is a sed-like tool for working with JSON data. You can find it [here](https://stedolan.github.io/jq/download/). +. For the scripts to work, you need to install `jq` which is a sed-like tool for working with JSON data. You can find it [here](https://stedolan.github.io/jq/download/). . Load data into the reviews DB by running springbox-reviews/scripts/loadReviews.sh. cd springbox-reviews/scripts/ @@ -28,7 +28,7 @@ . Load data into the recommendations DB by running springbox-recommendations/scripts/loadGraph.sh. - cd springbox-recommendations/scripts + cd springbox-recommendations/scripts/ ./loadGraph.sh localhost 9999 localhost 8082 . Visit http://localhost:9000 and you should see something like the following: