PeteGabriel / yamda_go

yet another movie database api

Home Page:https://yamda-go.herokuapp.com/v1/healthcheck

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test

Yet Another Movie Database API

Overview of API

Method URL Pattern Action Implemented
GET /v1/healthcheck Show application health and version information
GET /v1/movies Show the details of all movies
POST /v1/movies Create a new movie
GET /v1/movies/:id Show the details of a specific movie
PATCH /v1/movies/:id Update the details of a specific movie
DELETE /v1/movies/:id Delete a specific movie
POST /v1/users Register a new user
PUT /v1/users/activated Activate a specific user
PUT /v1/users/password Update the password for a specific user
POST /v1/tokens/authentication Generate a new authentication token
POST /v1/tokens/password-reset Generate a new password-reset token
GET /debug/vars Display application metrics

Error response ❌

This API tries to make use of a standardized mediatype called application/problem+json. You should expect this for all the errors in the range 400-4xx.

HTTP/1.1 401 Unauthorized
Content-Type: application/problem+json; charset=utf-8
Date: Wed, 07 Aug 2019 10:10:06 GMT
{
    "type": "https://example.com/v1/movies/78",
    "title": "Not authorized to view movie details",
    "status": 401,
    "detail": "Due to privacy concerns you are not allowed to view account details of others.",
    "instance": "/error/123456/details"
}

Adding new environment variables 🌴

This application makes use of a file with .env extension. Apart from that, the file settings.go is responsible for reading that file and converting it into a structure that can be used in the codebase.

Using the mapstructure module we can convert the data from the .env file directly into the data types we find more useful (string to bool, for example). Using the tag available this module exposes functionality to convert one arbitrary Go type into another.

Adding a new variable resolves into just one new line of code. 😄

Rate Limiter

We make use of the module golang.org/x/time/rate which implements a token-bucket rate-limiter algorithm.

The main idea is:

  1. We will have a bucket that starts with b tokens in it.
  2. Each time we receive a HTTP request, we will remove one token from the bucket.
  3. Every 1/r seconds, a token is added back to the bucket — up to a maximum of b total tokens.
  4. If we receive a HTTP request and the bucket is empty, then we should return a 429 Too Many Requests response.

A simple curl command can test this limiter

for i in {1..6}; do curl <host>/v1/healthcheck; done

Unit Test Coverage

text_coverage

(Tue 18 April)

Running locally 🏠

You can start the database dependecy as a Docker container by running the following command:

docker-compose -f "docker-compose.yaml" up -d --build

This will configure the databse and necessary tables. Also the adminer container allows you to have access to an UI to query/modify the database easily.

Sending emails 📫

The user receives a welcome email after they successfully register. We make use of the SMTP service MailTrap.

An example of an email sent can be seen below.

About

yet another movie database api

https://yamda-go.herokuapp.com/v1/healthcheck


Languages

Language:Go 99.9%Language:Makefile 0.1%