This project is a simple REST API created during the selection process for a position on B2W Digital. It provides information about planets from the Star Wars movies. It interacts with SWAPI (https://swapi.dev/), a public API with data from the franchise.
The project can be run by compiling the source code and/or using Docker.
Name | Default | Description |
---|---|---|
API_PORT | 9080 | Port used by the API |
SWAPI_URL | https://swapi.dev/api | Base url for the SWAPI api. |
MONGODB_URI | mongodb://localhost:27017/ | URI to your MongoDB server |
MONGODB_DATABASE_NAME | starwars | Database name to be used by the API |
MONGODB_TEST_DATABASE_NAME | starwars_testdb | Database name to be used by the integration tests |
To compile the code from this repository, you'll need to install Golang and MongoDB on your machine.
After setting up your MongoDB server, we can compile and run the api. For simplicity, authentication is not used when connecting to the MongoDB server. Make sure you setup your server accordingly
At the root folder of this repository, run the command:
go build -o starwars_api .
The -o
flag sets the name of the output binary.
Now simply run the resulting binary:
./starwars_api
The api will be avaliable at: http://localhost:9080/api/
To modify the starting parameters such as port and database uri, add the environment variables from the table above before the binary in the run command:
MONGODB_URI='mongodb://localhost:27017/' API_PORT='9500' ./starwars_api
Install Docker and Docker Compose.
At the root folder of this repository, run the command:
docker-compose up --build -d
To stop and remove the containers, run docker-compose down
.
To modify environment variables, edit the docker-compose.yaml
file.
The api will be avaliable at: http://localhost:9080/api/
Tests can be run from source and with Docker. In order to run tests on Docker, the containers (api and db) must be running.
Integration tests require an internet connection and the MongoDB instance to be running.
From source
go test desafio-b2w/tests -tags=integration -v
Docker
docker-compose exec starwars_api go test desafio-b2w/tests -tags=integration -v
starwars_api
is the container name created on the previous step.
From source
go test desafio-b2w/tests -tags=unit -v
Docker
docker-compose exec starwars_api go test desafio-b2w/tests -tags=unit -v
The base url for the api is /api
. Both requests and responses use Content-type: application/json
.
Request Body
Property | Type | Info |
---|---|---|
name | string | required, must be unique |
terrain | string | required |
climate | string | required |
Response
Status 201
{
"id": "5ed6d8fdfa09e89371ded229",
"name": "Tatooine",
"climate": "Arid",
"terrain": "Mountain",
"filmAppearances": 5
}
Status 400 (either if the name already exists or a required property is empty).
Ex:
{
"Status": 400,
"Code": "planet.duplicated",
"Message": "A planet with the given name already exists."
}
Response
Status 200
[
{
"id": "5ed6d8fdfa09e89371ded229",
"name": "Tatooine",
"climate": "Arid",
"terrain": "Desert",
"filmAppearances": 5
},
{
"id": "5ed6da0bfa09e89371ded22a",
"name": "Yavin IV",
"climate": "Temperate, tropical",
"terrain": "Jungle, rainforests",
"filmAppearances": 1
}
]
Response
Status 200
{
"id": "5ed6da0bfa09e89371ded22a",
"name": "Yavin IV",
"climate": "Temperate, tropical",
"terrain": "Jungle, rainforests",
"filmAppearances": 1
}
Status 400
{
"Status": 400,
"Code": "invalid.id",
"Message": "The given ID is invalid."
}
Status 404
{
"Status": 404,
"Code": "not_found",
"Message": "The requested resource was not found."
}
Status 200
{
"id": "5ed6d8fdfa09e89371ded229",
"name": "Tatooine",
"climate": "Arid",
"terrain": "Desert",
"filmAppearances": 5
}
Status 404
{
"Status": 404,
"Code": "not_found",
"Message": "The requested resource was not found."
}
Status 200
Empty body
Status 400
{
"Status": 400,
"Code": "invalid.id",
"Message": "The given ID is invalid."
}
Status 404
{
"Status": 404,
"Code": "not_found",
"Message": "The requested resource was not found."
}
This project is under the MIT license.