In the saddle!

Exercise 1 of 4 in chapter 1 · 100 XP · 2 objectives checked by tests

Topics covered: RouteJSON response

The Flying Gophers are a bike courier cooperative in Lyon, France. Margot, who founded it, is fed up with the whiteboard covered in arrows and the text messages that get lost. She wants an API for the couriers' app, and she picked Goravel, a Go framework that looks a lot like Laravel. The project is already created, and it runs right here, in your browser.

First things first: the couriers' app needs to know whether the server is up.

Your mission

  1. Open routes/web.go: it's the equivalent of routes/web.php in Laravel. The Web() function declares the routes; there are already two: the home page and /hello/{nom}.
  2. Add a GET /ping route, using the one for / as a model.
  3. It responds with JSON: {"statut": "in the saddle"}, with a 200 status code.

The preview shows /ping after every change, and the “Requests” tab sends the request for you. When it looks right, run the tests.

Reminder

facades.Route().Get(path, action) is Laravel's Route::get(). The action is a function that receives the request context, ctx http.Context, and returns an http.Response. In Go, a variable's type is written after its name: ctx http.Context reads “ctx, of type http.Context”.

ctx.Response() builds the response: .Success() sets the 200 status code, .Json(…) writes the body as JSON. http.Json is a plain map, the equivalent of the associative array you would pass to response()->json().

The JSON keys of this course stay in French, like the rest of the code's names: statut means “status”.