🚀 STAR WARS API 🌌
About • Features • Pending (to do) • How it works • Tech Stack • Author • License
Star Wars API - is a a REST API for Star Wars planets.
- Create a planet
- Get the number of film appearances of a planet by cosuming an external API
- SWAPI
- Update a planet
- Delete a planet
- Get all planets
- Get a planet by ID
- Get a planet by name
- Error handling
- I've spent most of the time on learning Go language and how to make a REST API with it.
- I've also spent some time on studying MongoDB driver for Go to setup database storage for the API.
- Case insensitive route params
- I still couldn't find a way to treat route params as case insensitive.
- Go MongoDB driver documentation doesn't help much.
- Project structure
- I don't know what project structure I should use with a Go REST API. I have considered MVC or DDD.
- But now I'm currently studying a Go Packages structure approach as it is said to be the standard strucutre among Go developers.
- Tests
- I'm currently learning how to write tests in Go. I should be pushing some tests very soon.
- Database container
- The API uses a MongoDB Atlas cluster database (MongoDB Atlas cluster connection URL). I should write a Dockerfile to create a container for MongoDB image so others don't need to use MongoDB Atlas cluster.
- Application image
- Finally I'm considering making an image of the whole application and its dependencies with Docker so it's easier for others to run it.
For now this project is comprised of these files:
- god.mod - For dependencies
- god.sum - For checksums
- .env - For environment variables
- docker-compose.yaml - Starts a MongoDB container
- Makefile - For running docker-compose more easily
- main.go (framework/cmd/server folder) - All core API code resides in this file (I plan on refactoring)
- mongo.go (framework/storage) - MongoDB connection configuration code
- init.go (framework/storage) - Creates a MongoDB connection whenever storage package is imported (Yes. I know! It needs a more decent abstraction.)
Before you begin, you will need to have the following tools installed on your machine: [Git] (https://git-scm.com), [Go] (https://golang.org/), [Docker Desktop] (https://www.docker.com/products/docker-desktop) - if Mac or Windows. In addition, it is good to have an editor to work with the code like [VSCode] (https://code.visualstudio.com/)
# Clone this repository
$ git clone git@github.com:viniciusbsneto/star-wars-api.git
# Access the project folder cmd/terminal
$ cd star-wars-api
# start MongoDB container with docker-compose through Makefile
make up
# go to server folder
cd framework/cmd/server
# run the server
$ go run main.go
# The server will start at host and port informed by you in .env file - e.g: go to http://localhost:3333
Do NOT type curly braces
POST /planets - Create a planet
- To create a planet send a POST request to /planets endpoint with JSON body (ID is generated automatically by MongoDB):
{
"name": "Alderaan",
"climate" [
"temperate"
],
"terrain": [
"grasslands",
"mountain"
]
}
PUT /planets/{id} - Update a planet
- To update a planet send a PUT request to /planets endpoint with new JSON body and passing an ID in the route:
{
"name": "NEW Alderaan",
"climate" [
"temperate",
"a new climate",
],
"terrain": [
"grasslands",
"mountain"
],
"films": 2
}
DEL /planets/{id} - Delete a planet
- To delete a planet send a DEL request to /planets passing an ID in the route:
Ex.: DEL http://localhost:3333/planets/5f157effe08b5ad3ffa598e6
GET /planets - Get all planets
- To get all planets send a GET request to /planets endpoint
Ex.: GET http://localhost:3333/planets
GET /planets/{id} - Get a planet by ID
- To get a planet by ID send a GET request to /planets passing an ID in the route:
Ex.: GET http://localhost:3333/planets/5f157effe08b5ad3ffa598e6
GET /search?name={planetName} - Get a planet by Name
- To get a planet by name send a GET request to /search endpoint passing the query to a planet name:
Ex.: GET http://localhost:3333/search?name=Alderaan
The following tools were used in the construction of the project:
See the file go.mod
- Editor: Visual Studio Code
- Markdown: StackEdit, Markdown Emoji
- API Test: Insomnia
- Fork the project.
- Create a new branch with your changes:
git checkout -b my-feature
- Save your changes and create a commit message telling you what you did:
git commit -m" feature: My new feature "
- Submit your changes:
git push origin my-feature
This project is under the license MIT.
Made with love by Vinícius Neto 👋🏽 Get in Touch!