gulien / fizz-buzz

A simple fizz-buzz REST server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fizz-buzz REST server

Go Reference Tests Lint codecov Go Report Card

A simple fizz-buzz REST server with statistics.

Usage of fizzbuzz:
      --port int      Set the port on which the fizz-buzz server should listen (default 80)
      --timeout int   Set the maximum duration in seconds before timing out execution of fizz-buzz (default 30)

Endpoints

GET /

Basic health check, always returns 200 if the server is running.

GET /api/v1/fizz-buzz

This endpoint returns a JSON list of strings with numbers from 1 to limit, where: all multiples of int1 are replaced by str1, all multiples of int2 are replaced by str2, all multiples of int1 and int2 are replaced by str1str2.

It accepts the following query parameters:

  • int1 - required, non-zero integer
  • int2 - required, non-zero integer
  • limit - required, non-zero positive integer
  • str1 - a string
  • str2 - a string
Example (200 OK)

/api/v1/fizz-buzz?int1=2&int2=3&limit=10&str1=foo&str2=bar

["1","foo","bar","foo","5","foobar","7","foo","bar","foo"]
Example (400 Bad Request)

/api/v1/fizz-buzz?int1=2&int2=0&limit=10&str1=foo&str2=bar

{"message":"zero int1 and/or int2"}
Example (503 Service Unavailable)
{"message":"context deadline exceeded"}

GET /api/v1/stats

This endpoint returns a JSON object with the parameters of the most frequent request and the number of occurrences of this request.

Example (200 OK)

/api/v1/stats

{"count":10,"int1":"2","int2":"3","limit":"10","str1":"foo","str2":"bar"}

📣 Current implementation of this endpoint relies on an in memory data source. In other words, the statistics are not persisted between runs nor are they relevant in a distributed environment.

However, one may provide its own data source implementation by implementing the stats.Statitistics interface. Suitable data sources could be either No-SQL or SQL. For the latter, the implementer will have to make sure the requests' parameters do not lead to SQL injection (e.g., str1 equals DROP TABLE foo;).

Development

Requirements

  • Go >= 1.16
  • golangci-lint >= 1.39
  • A linux-like terminal (ideally)

Makefile commands

  • make fmt - Shortcut for go fmt and go mod tidy
  • make lint - Runs linters
  • make tests - Runs tests
  • make todos - Shows TODOs
  • make godoc - Runs a local webserver for godoc
  • make run - Runs the application (PORT and TIMEOUT are available as variables, i.e, make run PORT=80 TIMEOUT=30)
  • make build - Builds the application (VERSION is available as variable, i.e, make build VERSION=foo)

About

A simple fizz-buzz REST server

License:MIT License


Languages

Language:Go 96.2%Language:Makefile 3.8%