The game Rock-Paper-Scissors implemented as a microservice that stores data in MongoDB and publishes events to RabbitMQ. The service has a simple restful HTTP API that allows creation of games and making moves.
Try it here!
Built using the following:
- Langohr as RabbitMQ client
- Monger as MongoDB client
- Cheshire for JSON marshalling
- Friend for OpenID authentication
- The usual suspects: Ring, Compojure, Hiccup...
Deployed on Heroku using CloudAMQP and MongoHQ. Initally based on leiningen heroku template and my previous explorations using Datomic and EventStore.
Used as part of a lab teaching how to build small services communicating using messaging. Not intended for production! :-)
MongoDB is simply used as a key/value storage and RabbitMQ as a simple publish/subscribe bus.
Note:
- This does not use event sourcing! MongoDB and RabbitMQ are not updated transactionally, instead first the message is sent and then MongoDB is update. This means that updates can be lost.
- Basically no input validation! :-)
- To keep it simple very few features of RabbitMQ is being used. Instead the message payload contains meta data.
If you want to run the service locally you need:
- Leiningen (
brew install leiningen
) - MongoDB (
brew install mongodb
) - RabbitMQ (
brew install rabbitmq
)
Run using: lein run
You need to add three properties pointing out the AMQP and MongoDB URL's:
heroku config:set RABBITMQ_URL="<amqp_url>"
heroku config:set MONGODB_URL="<mongodb_url>"
heroku config:add SESSION_SECRET=$RANDOM_16_CHARS
Both URLs must contain username and password, port etc.
Extends message specified by: https://github.com/johanhaleby/lab-service-registry/blob/master/LAB.md
Example when won:
{
"scores":{
"player1":1,
"player2":0
},
"result": "won",
"winner": "player1",
"loser": "player2"
}
Example when tied:
{
"scores":{
"player1":10,
"player2":0
},
"result": "tied"
}
Sent when a player has made a move.
Example:
{
"player": "player1",
"move": "rock"
}
To make a move using curl:
curl -u root:<password> -d player=<playerToMakeMove> -d move=<rock/paper/scissors> http://simple-rps-service.herokuapp.com/games/{gameId}
Copyright @ 2014 Jan Kronquist
Distributed under the Eclipse Public License, the same as Clojure.