liveBook · Manning

REST

As explained in Learn Go with Pocket-Sized Projects

REST is an architectural style that defines resources and interactions with them through uniform representations and HTTP semantics.

REST is a set of conventions for defining an API on an HTTP server. It organizes the interface around resources and ways to interact with them. For a word game, the resource is a game: a client creates one, checks its status, and submits guesses through the service. section 8.2.1 section 8.0

Define the contract before filling in the game logic:

  • Choose a path and method for each operation. POST /games creates a game; the planned GetStatus and Guess operations retrieve status and submit a word. section 8.2.1 section 8.0
  • Define the response as JSON. A game response can include an ID, attempts left, word length, status, previous guesses and feedback, and an optional solution. section 8.2.1
  • Use HTTP status codes to tell clients what happened: 201 for creation and 405 when a handler rejects the wrong method. Set the status before writing the response body. section 8.2.1 section 8.7

Try the creation endpoint

Run the server on port 8080, then send `curl -v -X POST http://localhost:8080/games`. The JSON-response handler sets `Content-Type: application/json` and returns `201 Created`. Its initial implementation encodes an empty GameResponse; it does not yet create a playable game. A handler test uses `httptest.NewRecorder` to check the status, content type, and JSON body. section 8.2.1

Listing 8.10 internal/handlers/router.go: Setting the verb and path
package handlers

import (
    "net/http" 

    "learngo-pockets/httpgordle/internal/api"
    "learngo-pockets/httpgordle/internal/handlers/newgame"
)

// NewRouter returns a router that listens for requests
// to the following endpoints:
//   - Create a new game;
//
// The provided router is ready to serve.
func NewRouter() *http.ServeMux { 
    r := http.NewServeMux()
    r.HandleFunc(http.MethodPost+" "+api.NewGameRoute, #1
        newgame.Handle)  #1


    return r
}

See how a Go `http.ServeMux` associates the POST method and `/games` path with the creation handler. section 8.2.1

Keep the public route and response types separate from handler code so the interface is clear to callers. REST defines the HTTP-facing contract here; it does not supply the game algorithm, session tracking, or deployment. section 8.2.1 section 8.0

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines REST conventions and works through a creation route, JSON response, status codes, curl request, and handler test; defers deployment.
Examples
Uses POST /games, a JSON game response, curl output, and an httptest handler check.
What it assumes
Assumes prior exposure to Go structs with JSON tags and the game algorithm; introduces the HTTP route and handler step by step.
What you can do after
Build it from scratch as a Go HTTP creation endpoint with a route, JSON response, status code, and handler test.
How it connects
Connects HTTP methods and routes with JSON representations, HTTP status codes, handlers, and path parameters.

Also explained in

Compare how 22 books explain REST →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.