Skip to content

Instantly share code, notes, and snippets.

@chasinglogic
Created October 18, 2017 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chasinglogic/c58650da43455026faa26ef1863a05a5 to your computer and use it in GitHub Desktop.
Save chasinglogic/c58650da43455026faa26ef1863a05a5 to your computer and use it in GitHub Desktop.
Build Simple REST API in Go.md

Build a simple REST API in Go

The Problem

You are going to build a simple REST API without a Database backend to serve some info about Movies.

The Code

You're REST API can run on any port (I recommend 8080) and will serve the following data:

var Movies = []struct {
  ID       int
  Director string
  Name     string
  Rating   int
} {
  {
    ID: 1,
    Director: "Steven Spielberg",
    Name: "E.T. the Extra-Terrestrial",
    Rating: 5,
  },
  {
    ID: 2,
    Director: "Howard Hawks",
    Name: "Rio Bravo",
    Rating: 5,
  },
  {
    ID: 3,
    Director: "Tony Leondis",
    Name: "The Emoji Movie",
    Rating: 1,
  },
  {
    ID: 4,
    Director: "Michael Bay",
    Name: "Transformers 3",
    Rating: 3,
  },
}

It should support all CRUD (Create, Read, Update, Delete) operations for this data set. Persisting data between runs is not a requirement.

It should support the following routes:

GET /movies # Return all movies
POST /movies # Create a new Movie
GET /movies/:id # Get a single movie by it's ID
PUT /movies/:id # Update a movie by it's ID
DELETE /movies/:id # Delete a movie by it's ID

You will not require any 3rd party libraries. The example solution was written entirely using the standard library. However you are free to use libraries as you see fit.

The Solution

With your API running the following curl calls should return this

$ curl http://localhost:8080
[{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":5},{"id":2,"director":"Howard Hawks","name":"Rio Bravo","rating":5},{"id":3,"director":"Tony Leondis","name":"The Emoji Movie","rating":1},{"id":4,"director":"Michael Bay","name":"Transformers 3","rating":3}]

$ curl http://localhost:8080/movies/2
{"id":2,"director":"Howard Hawks","name":"Rio Bravo","rating":5}

$ curl -X DELETE http://localhost:8080/movies/2
[{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":5},{"id":3,"director":"Tony Leondis","name":"The Emoji Movie","rating":1},{"id":4,"director":"Michael Bay","name":"Transformers 3","rating":3}]

$ curl -X PUT -d '{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":100}' http://localhost:8080/movies/1
{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":100}
$ curl http://localhost:8080/movies/1
{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":100}

$ curl -X POST -d '{"name": "A Fake Movie", "director": "Mathew Robinson", "rating": 100000}' http://localhost:8080/movies
{"id":5,"director":"Mathew Robinson","name":"A Fake Movie","rating":100000}
$ curl http://localhost:8080/movies/5
{"id":5,"director":"Mathew Robinson","name":"A Fake Movie","rating":100000}
$ curl http://localhost:8080/movies
[{"id":1,"director":"Steven Spielberg","name":"E.T. the Extra-Terrestrial","rating":100},{"id":2,"director":"Howard Hawks","name":"Rio Bravo","rating":5},{"id":3,"director":"Tony Leondis","name":"The Emoji Movie","rating":1},{"id":4,"director":"Michael Bay","name":"Transformers 3","rating":3},{"id":5,"director":"Mathew Robinson","name":"A Fake Movie","rating":100000}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment