A simple Flask-powered microservice that calculates the median of integers.
-
/put
(POST/PUT): Store an integer for one minute. Expects a JSON object in the following structure:{ "int": <integer> }
-
/median
(GET): Initiate a median calculation request for all stored integers received over the last minute. Returns a JSON object in the following format:{ "job_id": <job_id>, "url": <url_for_result> }
-
/median-results/<job_id>
(GET): See results of a median calculation request.-
HTTP 200: Calculation complete. Returns a JSON object in the following format:
{ "message": "Calculation complete!", "median": <median> }
-
HTTP 202: Calculation not-yet complete. Returns a JSON object in the following format:
{ "message": "Still processing..." }
-
- Redis 3.0.x
- Python 2.7.x
Flask==0.10.1
numpy==1.11.0
redis==2.10.5
rq==0.5.6
-
Clone this repo to your local machine:
$ git clone [email protected]:respondcreate/median-microservice.git
-
Create a new virtual environment and install dependencies:
$ cd median-microservice $ virtualenv ENV $ source ENV/bin/activate $ cd web/ $ pip install -r requirements.txt
-
Start-up the microservice:
$ python app.py
-
Open a new shell, activate the virtualenv and start the redis queue consumer (worker.py):
$ cd median-microservice $ source ENV/bin/activate $ cd web/ $ python worker.py
Now visit http://127.0.0.1:5000/put/50 to put your first integer (50) into the microservice!
Run tests with this command (be sure your virtualenv is activated before running):
$ python web/app_tests.py
The following commands assume you have both docker-machine
and docker-compose
installed.
-
Create a machine with docker-machine:
Locally, with virtualbox:
$ docker-machine create -d virtualbox median-microservice
On DigitalOcean (assumes you have a valid DigitalOcean token set to the DO_TOKEN environment variable):
$ docker-machine create -d digitalocean --digitalocean-access-token=$DO_TOKEN median-microservice
On Amazon AWS (assumes you have
~/.aws/credentials
set appropriately):$ docker-machine create --driver amazonec2 --amazonec2-zone=b docker-demo
-
Make the median-microservice docker machine your 'current' one:
$ eval "$(docker-machine env median-microservice)"
-
First, build:
$ docker-compose build
-
Spin up the server:
With development settings:
$ docker-compose up -d
With production settings:
$ docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d