Go · Chapter 21 of 26

HTTP servers

By the end of this chapter you can write an HTTP server with Go's standard library: handlers, routes with a method and wildcards, JSON answers with the right status code and Content-Type, JSON bodies read and checked, tests with httptest, and an http.Server with timeouts. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Services
  • 4 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go are free. This one comes with the all labs plan.

Why you need this

Everything you have written so far runs inside one program. The rest of BijliGo talks over HTTP: the drivers' app asks whether charger C1042 is free, the station screens ask for a station's chargers, and the chargers themselves post a heartbeat every 30 seconds, 1,180 of them, about 40 requests a second before a single meter reading. Nandini wants the new status API to be one small Go program that answers all of that, and Tenzin wants it to stay up when a client misbehaves.

Many languages need a framework for this. Go's standard library has a production HTTP server in the net/http package, and since Go 1.22 its router understands methods and wildcards. And every request runs in its own goroutine, so classes 3's goroutines, locks, contexts and limits are exactly what a server needs.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    chargers.go: GET /chargers/{id}

    In AddChargerRoutes(mux http.ServeMux, c bijli.Central) in chargers.go, register a handler on mux for GET /chargers/{id}. It asks c.ChargerStatus for that charger, passing the request's context (r.Context()), and answers:

    a known charger: 200 with Content-Type application/json and a ChargerJSON (declared in the starter) as the body, exactly the four fields charger, station, state and power_kw; for C1042: {"charger":"C1042","station":"ST007","state":"available","power_kw":30}.

    an unknown charger such as C9999 (the error wraps bijli.ErrUnknownCharger): 404. Any other error from the central system (the Check makes C1099 not answer): 503. Any other method, such as POST /chargers/C1042: 405, which the mux answers for you when the pattern has a method.

    The Check registers your routes on a new mux and sends requests with httptest. Set the Content-Type header before writing the status or the body.

  2. Task 2

    heartbeats.go: POST /heartbeats

    In AddHeartbeatRoutes(mux http.ServeMux, c bijli.Central) in heartbeats.go, register a handler on mux for POST /heartbeats. It reads a JSON body like {"charger":"C1042","at":"2026-10-14T19:12:00+05:30"} and records it with c.RecordHeartbeat, passing the request's context and a bijli.Heartbeat with that Charger and At.

    Recorded: 202 with Content-Type application/json and the body {"recorded":"C1042"} (exactly that one field, with the charger's id). A body that is not JSON, or a field of the wrong type (a charger that is a number, an at that is not a time): 400. A charger that is missing or empty, or an at that is missing: 400. None of those may reach the central system. An unknown charger such as C9999: 404. Any other error: 503. Any other method, such as GET /heartbeats: 405.

  3. Task 3

    stations.go: GET /stations/{id}/chargers

    In AddStationRoutes(mux http.ServeMux, c bijli.Central) in stations.go, register a handler on mux for GET /stations/{id}/chargers. For a known station (bijli.StationByID), it asks c for the status of each of the station's chargers, all at the same time, with the request's context, and answers 200 with Content-Type application/json and a JSON array of ChargerJSON in the order of the station's Chargers (ST001: C1001 to C1008, ST002: C1009 to C1014).

    An unknown station such as ST999: 404. If any lookup fails (the Check makes C1099 at ST017 not answer): 503. The Check asks for ST001 and ST002 with a central system that takes 20 ms a lookup, compares every charger, station, state and power_kw, and checks that at least 2 lookups ran at once. The handler must wait for its goroutines before it writes the answer.

  4. Task 4 · interview question

    Interview: server.go, the server with no timeouts

    Tenzin's NewHTTPServer(addr string, h http.Handler) *http.Server makes the http.Server that main.go runs, with only Addr and Handler set. A client that opened a connection and sent half a request kept it open for as long as it liked.

    Fix it so that the server keeps Addr addr and Handler h, and has ReadHeaderTimeout 2 s, ReadTimeout 5 s, WriteTimeout 10 s and IdleTimeout 60 s. The Check calls NewHTTPServer(":8081", h) and looks at each field.

    Joseph will ask: why does every server need timeouts?

What you will learn

  • http.Handler and http.HandlerFunc: what the server calls for each request.
  • http.ServeMux patterns: GET /stations/{id}, r.PathValue("id"), and the 404 and 405 you get for free.
  • Writing JSON: the Content-Type header, the status code, then the body, in that order.
  • Reading JSON: decoding a request body, and http.Error with 400.
  • r.Context(): the request's context, ended when the client goes away.
  • httptest: NewRecorder and NewRequest to test a handler, NewServer for a real one.
  • http.Server timeouts: why a server with none can be held open for ever.

More in Services

Build "HTTP servers" yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go are free. This one comes with the all labs plan.