Skip to content

Star Wars API is a REST API for Star Wars planets. Developed with Go, Gorilla Mux and MongoDB.

License

Notifications You must be signed in to change notification settings

viniciusbsneto/star-wars-api

Repository files navigation

Star Wars API

🚀 STAR WARS API 🌌

A Star Wars API. Find out more about the planets!

GitHub language count Repository size GitHub last commit License Stargazers made by viniciusbsneto

Status: Core finished 🚧 Refactoring... 🚧

AboutFeaturesPending (to do)How it worksTech StackAuthorLicense

💬 About

Star Wars API - is a a REST API for Star Wars planets.


💡 Features

  • 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

🛠️ Pending (in progress...) ⏳

  1. 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.
  2. 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.
  3. 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.
  4. Tests
    • I'm currently learning how to write tests in Go. I should be pushing some tests very soon.
  5. 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.
  6. 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.

⚙ How it works

For now this project is comprised of these files:

  1. god.mod - For dependencies
  2. god.sum - For checksums
  3. .env - For environment variables
  4. docker-compose.yaml - Starts a MongoDB container
  5. Makefile - For running docker-compose more easily
  6. main.go (framework/cmd/server folder) - All core API code resides in this file (I plan on refactoring)
  7. mongo.go (framework/storage) - MongoDB connection configuration code
  8. init.go (framework/storage) - Creates a MongoDB connection whenever storage package is imported (Yes. I know! It needs a more decent abstraction.)

📌 Pre-requisites

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/)

Rodando o Backend (servidor)

# 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

Run in Insomnia

Endpoints

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

🧰 Tech Stack

The following tools were used in the construction of the project:

REST API

See the file go.mod

Utilitários


🤝 How to contribute

  1. Fork the project.
  2. Create a new branch with your changes: git checkout -b my-feature
  3. Save your changes and create a commit message telling you what you did: git commit -m" feature: My new feature "
  4. Submit your changes: git push origin my-feature

🧑‍💻 Author

Vinícius Neto


Linkedin Badge Gmail Badge


📝 License

This project is under the license MIT.

Made with love by Vinícius Neto 👋🏽 Get in Touch!

About

Star Wars API is a REST API for Star Wars planets. Developed with Go, Gorilla Mux and MongoDB.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published