This repository contains a sample solution for the problem described here.
It was written in Python 2.7, using tornado 4.2.1 as a server serving the REST requests.
The environment can be setup in 2 ways:
- Using
docker-compose
- Using
virtualenv
Both setups are described below.
To run the app using docker
and docker-compose
you need them installed. Installation instructions for docker
can be found here, and for docker-compose
here.
When both are installed, the docker image needs to be built from the defined Dockerfile
with the following:
cd solution
docker build .
The environment is now ready to run the app.
The simplest way to install virtualenv
is with pip
, python's package manager.
Using brew:
brew install pip
pip install virtualenv
curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python # install easy_install
sudo easy_install pip
Depending on your distribution, you can get pip
using one of the following:
sudo apt-get install python-pip
sudo yum install python-pip
When pip
is installed, virtual environment for this project needs to be created. Perform the following:
pip install virtualenv
cd $main_project_folder/solution/
virtualenv venv # create virtual env.
source venv/bin/activate # activate virtual env., puts you inside
pip install -r requirements # installs all requirements for the project
To start the server running in the docker container, make sure you have a built docker image and run the following:
cd solution
docker-compose up
This creates a container with the rest.py
running inside. The server is up and running and by default listening on DOCKER_DEFAULT_IP:8888
.
Verifying docker ip:
boot2docker ip # if you are using boot2docker
docker-machine ip MACHINE_NAME # if you are using docker-machine
To start the server, perform the following:
source venv/bin/activate # activate virtual env (unless you have already done so)
cd solution/
python rest.py
The server is up and running and by default listening on 127.0.0.1:8888
. The PORT
value van be changed in the file config.py
.
Both setups have been tested on OS X El Captain 10.11.1
and Amazon Linux AMI 2015.03
.
Simply stop the running compose environment (ctrl/cmd + c
).
After finishing the server run session (ctrl/cmd + c
), virtual environment can be deactivated by simply typing
deactivate
All the responses are returned in JSON
format. Sample response is presented below. Response always returns HTML code 200
and the status indicates request handling effect.
status
is an integer number,rate
is a stringified decimal number with 6 precision digits.
{"status": 200, "rate": "4.229459"}
Server handles only one url:
/rate/CURRENCY_FROM/CURRENCY_TO/
e.g.:
/rate/EUR/PLN/
All other requests will results in the following response
{"status": 404, "msg": "Incorrect request url."}
The app does not have much configuration, yet if any values or paths (e.g. for CSV files) need to be changed, it can be done in config.py
.
The application logs all important events into a time-rotating log file. The log file is structured as follows:
/logs/main.log/main.log
And is created automatically upon server start.
IMPORTANT : When executing tests, make sure that the IP address under which the test suite sees the server is correct. This can be configured in config.py
(HOST
constant).
Test suite has been prepared for the app. To run the test suite, make sure the test is running and perform the following:
cd test
python run_tests.py
The test process exists with an appropriate exit code so that it can be directly plugged into an e.g. continuous integration solution.