fibonacci-api

Awesome API to calculate Fibonacci series

Stars
1

Fibonacci API

Awesome Simple API to calculate Fibonacci series for a given number.

Stack

  • Java 11
  • Springboot 2
  • Gradle 5.6
  • Docker
  • AWS Fargate

Serverless deployment on AWS Fargate

AWS Fargate Diagram AWS Fargate diagram


This API can be deployed on a serverless cluster using AWS Fargate. Pros:

  • There is no need to manage infraestructure (serverless). AWS ECS will create a process based on CPU and memory preferences.
  • There is no need to manage a container orchestration service (clusterless).
  • Elasticity and scalability (scale in and out) built in.
  • Springboot-based development for serverless (or any container-compatible stack).
  • Integration with AWS native services (IAM, Cloudwatch, security group etc) at the container level.

Cons:

  • It is not possible to run exec direct to a container. It require other ways to debug/trace. Ex: spring actuator API.

How to use

Credentials

In order to use the API, follow the credentials:

username: admin password: s3cr3t

1 - To calculate series for number 2

$ curl http://localhost:8080/api/calculate/2 \
  --header 'accept: application/json' \
  --header 'authorization: Basic YWRtaW46czNjcjN0'

[
	1,
	1
]

2 - To health check

$ curl --request GET \
    --url http://localhost:8080/actuator/health \
    --header 'accept: application/json' \
    --header 'authorization: Basic YWRtaW46czNjcjN0'

{
	"status": "UP"
}

3 - To shutdown application for HA testing purposes

$ curl --request POST \
  --url http://localhost:8080/actuator/shutdown \
  --header 'accept: application/json' \
  --header 'authorization: Basic YWRtaW46czNjcjN0'

{
	"message": "Shutting down, bye..."
}

How to debug via API

You can use the /actuator API to get several information about the service. Just do a GET:

$ curl --request GET \
  --url http://localhost:8080/actuator/ \
  --header 'accept: application/json' \
  --header 'authorization: Basic YWRtaW46czNjcjN0'

{
	"_links": {
		"self": {
			"href": "http://localhost:8080/actuator",
			"templated": false
		},
		"beans": {
			"href": "http://localhost:8080/actuator/beans",
			"templated": false
		},
		"caches-cache": {
			"href": "http://localhost:8080/actuator/caches/{cache}",
			"templated": true
		},
        ...
    }
}

How to build

In order to run, install gradle 5.6 and run gradle build on repo path.

Useful Docker commands

docker images
docker container ls
docker logs <container_name>
docker container rm <container_name
docker image rm <image_name

Local dev box using Ansible

To facilitate dev setup, there is an virtual box provisioned using Ansible. To use it:

1 - Install Vagrant 2.2.5 or higher 2 - Install VirtualBox 6.0 or higher 3 - Initialize vm by running on directory`s repo

git clone https://github.com/rgalba/fibonacci-api
cd fibonacci-api
vagrant up

4 - Access machine by running:

vagrant ssh

The /vagrant folder will be mounted with project root dir content.

5 - Build project

gradle build

6 - Run the app

gradle bootRun