# Codding Challenge
We going list all stack of development. All technology need by this challenge are open source and you can find on [github.com](https://github.com).
[TOC]
## Stack of development
1. Linux (Ubuntu) enviroment or similar
3. Docker-CE
4. Docker Compose
5. Golang 1.12+
6. Redis key/value database
7. Vegeta for test API
8. [httpi-go](https://github.com/nojima/httpie-go) for sugar curl request
9. Swagger for documentation api
10. Mockgen for test redis interfaces
## Configure
Install and check system.
Check docker install.
Run the script on `configs/chkdr.sh`
Check docker-compose.
Run the script on `configs/chkdrc.sh`
## How can I run this project
**Development enviroment**, you need docker-ce instaled in your os and follow the sequence commands.
For documentation about the **endpoints** just access the url `http://localhost:3000/swagger/index.html`, you see all the routes about the micro-services
## Compile the binary
Justing run the `make build`, you going generate bolth binary of `api`
and `workers` on folder `bin/`.
## Makefile
Makefile index for avaliable commands.
1. make build
2. make clean
3. make clean-api
4. make clean-workers
5. make build-api
6. make build-workers
7. make build-web
8. make build-docker-api
9. make build-docker-workers
10. make build-docker-web
11. make run-docker-api
12. make run-docker-workers
13. make run-docker-web
14. make clean-docker-containers
15. make clean-docker-images-build
16. make clean-docker-volumes
17. make create-network
## Documentation
Endpoint for access poll.
* Vote
`http://localhost:8080/` or `http://localhost:8080/#/vote`
* Results
`http://localhost:8080/#/results`
* Results per candidate hour
> Participante 1
> `http://localhost:8080/#/results/hour/participante1/20191023`
> Participante 2
> `http://localhost:8080/#/results/hour/participante2/20191023`
>
> Here you have **two parameters**(candidate name and date), you can change anytime for consult the results.
Api call with **CURL**
* Get results for all candidate
`curl -g -d '{"names":["Participante1", "Participante2"]}"' -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://localhost:3000/vote/result`
* Get result individual by hour
`curl -g -d '{"name":"participante1", "date":"20191023"}"' -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://localhost:3000/vote/result/hour`
* Get a single result by candidate
`curl -X GET http://localhost:3000/vote/participante1`
Use **httpie-go** sugar sintax curl
* Get results for all candidate
`ht -v post http://localhost:3000/vote/result names:='["Participante1", "Participante2"]'`
* Get result individual by hour
`ht -v post http://localhost:3000/vote/result/hour name=participante1 date=20191023`
* Get a single result by candidate
`ht -v http://localhost:3000/vote/participante1`
## Running the stack
To run this project you need a valid **user** with **access to the repository**.
In this case we build a container with **two stages**, for protect the **private ssh key**.
You need pass the correct key for clone the project and compile the binaries.
Use **docker-compose** for start the enviroment.
**Build images (Dev)**
$ `export SSH_PRIVATE_KEY_FILE="$(find ~/.ssh | grep id_ | grep -v pub | head -n1)"; SSH_PRIVATE_KEY=$(cat $SSH_PRIVATE_KEY_FILE) sudo -E docker-compose --file dev.docker-compose.yaml build --no-cache`
**Build images (Prod)**
$ `export SSH_PRIVATE_KEY_FILE="$(find ~/.ssh | grep id_ | grep -v pub | head -n1)"; SSH_PRIVATE_KEY=$(cat $SSH_PRIVATE_KEY_FILE) sudo -E docker-compose --file prod.docker-compose.yaml build --no-cache`
**Run stack (Dev)**
$ `export SSH_PRIVATE_KEY_FILE="$(find ~/.ssh | grep id_ | grep -v pub | head -n1)"; SSH_PRIVATE_KEY=$(cat $SSH_PRIVATE_KEY_FILE) sudo -E docker-compose --file dev.docker-compose.yaml up`
**Run stack (Prod)**
$ `export SSH_PRIVATE_KEY_FILE="$(find ~/.ssh | grep id_ | grep -v pub | head -n1)"; SSH_PRIVATE_KEY=$(cat $SSH_PRIVATE_KEY_FILE) sudo -E docker-compose --file prod.docker-compose.yaml up`
or you can running one by one. Start container individual by command line.
$ `make build-docker-api`
$ `make build-docker-workers`
$ `make build-docker-web`
and than
$ `make create-network`
$ `make run-redis` (new window)
$ `make run-docker-api` (new window)
$ `make run-docker-workers` (new window)
$ `make run-docker-web` (new window)
## Tests
You can run the vegeta attack for metrics the request. The script you can find in `tools/scripts/teststress/vegeta.sh`
The last metrics we have:
```shell
thiago_sarmento on dev via 🐹 v1.12.4
➜ tools/scripts/teststress/vegeta.sh
Requests [total, rate, throughput] 1000, 1001.94, 1001.63
Duration [total, attack, wait] 998.368321ms, 998.065793ms, 302.528µs
Latencies [mean, 50, 95, 99, max] 423.826µs, 312.521µs, 426.417µs, 2.751746ms, 13.254874ms
Bytes In [total, mean] 15000, 15.00
Bytes Out [total, mean] 24000, 24.00
Success [ratio] 100.00%
Status Codes [code:count] 201:1000
Error Set:
```
## Versioning and License
We use SemVer for versioning. You can see the versions available by checking the tags on this repository.
For more details about our license model, please take a look at the LICENSE file.
2019, thiagozs