https://github.com/rvflash/fizzbuzz
FizzBuzz REST API over HTTPS (example)
https://github.com/rvflash/fizzbuzz
Last synced: about 1 month ago
JSON representation
FizzBuzz REST API over HTTPS (example)
- Host: GitHub
- URL: https://github.com/rvflash/fizzbuzz
- Owner: rvflash
- License: mit
- Created: 2018-07-22T21:20:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-24T18:04:50.000Z (over 7 years ago)
- Last Synced: 2025-03-23T09:35:12.926Z (8 months ago)
- Language: Go
- Size: 25.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FizzBuzz
[](https://godoc.org/github.com/rvflash/fizzbuzz)
[](https://travis-ci.org/rvflash/fizzbuzz)
[](http://codecov.io/github/rvflash/fizzbuzz?branch=master)
[](https://goreportcard.com/report/github.com/rvflash/fizzbuzz)
FizzBuzz REST API over HTTPS (example).
This project offers 2 endpoints:
* A simple [package](https://godoc.org/github.com/rvflash/fizzbuzz) to play with the original FizzBuzz or make your own one. Three algorithms are available.
* A [REST API](https://github.com/rvflash/fizzbuzz/tree/master/cmd/fizzbuzz) over HTTPS to make your own FizzBuzz and get its responses as JSON.
## Installation
Fizzbuzz uses [dep](https://golang.github.io/dep/) to manage its dependencies.
Waiting for Go Modules (code name: vgo), it's the tool to do this stuff!
```bash
$ go get github.com/rvflash/fizzbuzz
$ cd $GOPATH/src/github.com/rvflash/fizzbuzz
$ dep ensure
```
## Quick start
If you just need a Go package to play with Fizzbuzz, see this [documentation](https://godoc.org/github.com/rvflash/fizzbuzz) on the interface.
This package proposes 3 different methods to apply the algorithm. The benchmark's performances have defined the method to use in the API.
```go
import (
"fmt"
"github.com/rvflash/fizzbuzz"
)
// ...
fb := fizzbuzz.Default
fmt.Println(fb.Bulk(15))
// output: [1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz]
```
Otherwise, to start the REST API, see the [cmd/fizzbuzz](https://github.com/rvflash/fizzbuzz/tree/master/cmd/fizzbuzz) for more information.
Also see, the [documentation](https://github.com/rvflash/fizzbuzz/tree/master/api) of the API handler.
## Dependencies
### Gin
One of the best features of Go is its built-in net/http library.
However, I wanted to use Gin for this project to simplify some parts of the code and used a non-standard package to use dep.
And, it's a really fast alternative to build a micro-service like this one (see [Goa](https://github.com/goadesign/goa) for more complex projects).
## Features
* A package that exposes methods to play with FizzBuzz, with 3 different algorithms (see benchmark bellow).
* A REST API over HTTPS to use it.
* Continuous integration with [Travis](https://travis-ci.org/rvflash/fizzbuzz) that run all lints and tests (benchmark included).
* Fully [tested](https://codecov.io/github/rvflash/fizzbuzz?branch=master) and code that passed various [linters](https://github.com/golangci/golangci-lint) (go vet, etc.)
* A Systemd unit file to easily manage and control the service. The logs can be manipulated with Journalctl.
* The API can recover on its own panics. Systemd will restart it for others cases (server reboot, etc).
* The API exposes a health check as monitoring purpose.
* Vendored dependencies with dep.
* Semantic Versioning. The master branch, as usual with a Go project is not safe. To use in production, see the latest tag: v0.0.2.
### TODO: possible improvements
* Add a cache. As the result is deterministic, we can use a cache to speed up operations and store the API results.
* Add the cache-control header to also cache the data on client-side (max-age).
* Add the CORS headers (Access-Control-Allow-Origin, etc.) to make the API accessible by JavaScript in-browser client-side code.
* Add a system, via NGINX or other, to redirect the HTTP traffic to the HTTPS API.
* Add GZIP encoding to optimize the load time.
## Testing
As usual:
```bash
$ go test -cover -race -v $(go list ./... | grep -v /vendor/)
$ go test -bench=.
```
### Benchmark
> On Linux OS, the default algorithm used in API is even faster, see the [Travis logs](https://travis-ci.org/rvflash/fizzbuzz).
```
goos: darwin
goarch: amd64
pkg: github.com/rvflash/fizzbuzz
BenchmarkMultiples_One-4 20000000 61.6 ns/op
BenchmarkMultiples_Two-4 20000000 59.3 ns/op
BenchmarkMultiples_Three-4 30000000 51.7 ns/op
BenchmarkMultiples_Bulk20-4 2000000 719 ns/op
BenchmarkMultiples_BulkTwo20-4 2000000 749 ns/op
BenchmarkMultiples_BulkThree20-4 2000000 835 ns/op
BenchmarkMultiples_Bulk100-4 500000 3422 ns/op
BenchmarkMultiples_BulkTwo100-4 500000 3538 ns/op
BenchmarkMultiples_BulkThree100-4 300000 3950 ns/op
PASS
ok github.com/rvflash/fizzbuzz 15.906s
```