Day 9: HTTP Server – shorturl
Goal: A URL shortener with a JSON API, routing via ServeMux patterns, middleware for logs and metrics and a clean shutdown – tested with httptest · Duration: approx. 150 min · Prerequisite: Day 8
Briefing
- Since Go 1.22,
http.ServeMuxsupports methods and wildcards:GET /{code},r.PathValue("code"). - Middleware is just a function
http.Handler -> http.Handler. log/slogwrites structured JSON logs,client_golangprovides/metricsfor Prometheus.- Graceful shutdown: on SIGTERM no new connections, running requests may finish.
- Storage behind an interface: a map today, SQLite on Day 10 – without touching the server.
request -> recoverer -> instrument -> logRequests -> ServeMux -> Handler -> Store
(panic) (Prometheus) (slog) pattern JSON Memory
(Day 10: SQLite)
Drill 1: Module and dependency
mkdir -p ~/go-lab/tag09-shorturl && cd ~/go-lab/tag09-shorturl
go mod init example.com/shorturl
go get github.com/prometheus/[email protected]
mkdir -p cmd/shorturl internal/server internal/store/storetestExpected (shortened):
go: creating new go.mod: module example.com/shorturl
go: added github.com/prometheus/client_golang v1.25.0
Save your progress? Sign up for free
Drill 2: The store contract
The interface describes what is stored, not how. Errors are sentinels (Day 3), so that the server can distinguish 404 from 409, no matter which implementation is behind it.
cat > internal/store/store.go <<'EOF'
// Package store defines how short links are stored. Day 10 replaces the
// storage with SQLite – server and tests stay unchanged.
package store
import (
"context"
"errors"
"time"
)
var (
ErrNotFound = errors.New("store: Link nicht gefunden")
ErrExists = errors.New("store: Code existiert bereits")
)
// Link is a short link.
type Link struct {
Code string `json:"code"`
URL string `json:"url"`
Hits int64 `json:"hits"`
Created time.Time `json:"created"`
}
// Store is the contract between the HTTP layer and the storage.
type Store interface {
Create(ctx context.Context, l Link) error // ErrExists
Get(ctx context.Context, code string) (Link, error) // ErrNotFound
Hit(ctx context.Context, code string) (Link, error) // Hits+1; ErrNotFound
Delete(ctx context.Context, code string) error // ErrNotFound
}
EOFThe first implementation is a map with a sync.RWMutex –
HTTP handlers run concurrently (Day 8), and unprotected map accesses
would end the program with
fatal error: concurrent map writes.
cat > internal/store/memory.go <<'EOF'
package store
import (
"context"
"sync"
)
// Memory holds links in a map. After a restart everything is gone (until Day 10).
type Memory struct {
mu sync.RWMutex // many readers (redirects), few writers
links map[string]Link
}
var _ Store = (*Memory)(nil) // the compiler checks: Memory satisfies Store
// NewMemory returns an empty storage.
func NewMemory() *Memory {
return &Memory{links: make(map[string]Link)}
}
func (m *Memory) Create(_ context.Context, l Link) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.links[l.Code]; ok {
return ErrExists
}
m.links[l.Code] = l
return nil
}
func (m *Memory) Get(_ context.Context, code string) (Link, error) {
m.mu.RLock()
defer m.mu.RUnlock()
l, ok := m.links[code]
if !ok {
return Link{}, ErrNotFound
}
return l, nil
}
func (m *Memory) Hit(_ context.Context, code string) (Link, error) {
m.mu.Lock() // writes: increment Hits
defer m.mu.Unlock()
l, ok := m.links[code]
if !ok {
return Link{}, ErrNotFound
}
l.Hits++
m.links[code] = l
return l, nil
}
func (m *Memory) Delete(_ context.Context, code string) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.links[code]; !ok {
return ErrNotFound
}
delete(m.links, code)
return nil
}
EOFWhy is Hit a call of its own instead of Get
plus Update? Reading and incrementing have to be one unit;
otherwise counts get lost with parallel redirects – exactly the race
from Day 8. With SQLite this becomes a single
UPDATE … SET hits = hits + 1.
Save your progress? Sign up for free
Drill 3: Contract tests
Write once, run for every implementation: storetest.Run
gets a factory function. Day 10 calls the same tests with SQLite.
cat > internal/store/storetest/storetest.go <<'EOF'
// Package storetest checks every Store implementation against the same contract.
// Day 10 calls Run with SQLite.
package storetest
import (
"context"
"errors"
"sync"
"testing"
"time"
"example.com/shorturl/internal/store"
)
// Run runs all contract tests; newStore returns an empty store per test.
func Run(t *testing.T, newStore func(t *testing.T) store.Store) {
ctx := context.Background()
link := store.Link{Code: "go", URL: "https://go.dev",
Created: time.Date(2026, 10, 10, 9, 0, 0, 0, time.UTC)}
t.Run("CreateGet", func(t *testing.T) {
s := newStore(t)
if err := s.Create(ctx, link); err != nil {
t.Fatal(err)
}
got, err := s.Get(ctx, "go")
if err != nil || got.URL != link.URL || !got.Created.Equal(link.Created) {
t.Errorf("Get = %+v, %v", got, err)
}
if err := s.Create(ctx, link); !errors.Is(err, store.ErrExists) {
t.Errorf("doppeltes Create: err = %v, want ErrExists", err)
}
})
t.Run("NotFound", func(t *testing.T) {
s := newStore(t)
if _, err := s.Get(ctx, "nix"); !errors.Is(err, store.ErrNotFound) {
t.Errorf("Get: %v", err)
}
if _, err := s.Hit(ctx, "nix"); !errors.Is(err, store.ErrNotFound) {
t.Errorf("Hit: %v", err)
}
if err := s.Delete(ctx, "nix"); !errors.Is(err, store.ErrNotFound) {
t.Errorf("Delete: %v", err)
}
})
t.Run("HitsParallel", func(t *testing.T) {
s := newStore(t)
if err := s.Create(ctx, link); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for range 50 {
wg.Go(func() {
if _, err := s.Hit(ctx, "go"); err != nil {
t.Error(err)
}
})
}
wg.Wait()
if got, _ := s.Get(ctx, "go"); got.Hits != 50 {
t.Errorf("Hits = %d, want 50", got.Hits)
}
})
t.Run("Delete", func(t *testing.T) {
s := newStore(t)
if err := s.Create(ctx, link); err != nil {
t.Fatal(err)
}
if err := s.Delete(ctx, "go"); err != nil {
t.Fatal(err)
}
if _, err := s.Get(ctx, "go"); !errors.Is(err, store.ErrNotFound) {
t.Errorf("nach Delete: %v", err)
}
})
}
EOFcat > internal/store/memory_test.go <<'EOF'
package store_test
import (
"testing"
"example.com/shorturl/internal/store"
"example.com/shorturl/internal/store/storetest"
)
func TestMemory(t *testing.T) {
storetest.Run(t, func(*testing.T) store.Store { return store.NewMemory() })
}
EOFgo test -race -count=1 -v ./internal/store/ 2>&1 | grep -E '^(\s*---|ok)'Expected:
--- PASS: TestMemory (0.00s)
--- PASS: TestMemory/CreateGet (0.00s)
--- PASS: TestMemory/NotFound (0.00s)
--- PASS: TestMemory/HitsParallel (0.00s)
--- PASS: TestMemory/Delete (0.00s)
ok example.com/shorturl/internal/store 1.165s
Why package store_test? The test sees the package only
from the outside, like any other caller – and may import
storetest without creating an import cycle.
Save your progress? Sign up for free
Drill 4: Routes and handlers
The patterns say it all: method, path, wildcard. When they overlap,
the more specific pattern wins – GET /healthz beats
GET /{code}. For a wrong method, the mux itself answers
with 405 and an Allow header.
cat > internal/server/server.go <<'EOF'
// Package server provides the HTTP API of shorturl.
package server
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"regexp"
"time"
"example.com/shorturl/internal/store"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var validCode = regexp.MustCompile(`^[A-Za-z0-9_-]{3,32}$`)
type server struct {
store store.Store
log *slog.Logger
baseURL string
}
// New builds the handler with all routes and middleware.
func New(st store.Store, log *slog.Logger, baseURL string,
reg *prometheus.Registry) http.Handler {
s := &server{store: st, log: log, baseURL: baseURL}
mux := http.NewServeMux()
mux.HandleFunc("POST /api/links", s.create)
mux.HandleFunc("GET /api/links/{code}", s.get)
mux.HandleFunc("DELETE /api/links/{code}", s.delete)
mux.HandleFunc("GET /{code}", s.redirect)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "ok")
})
mux.Handle("GET /metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
return recoverer(log, instrument(reg, logRequests(log, mux)))
}
type createReq struct {
URL string `json:"url"`
Code string `json:"code,omitempty"` // optional, otherwise random
}
type createResp struct {
store.Link
Short string `json:"short"`
}
func (s *server) create(w http.ResponseWriter, r *http.Request) {
var req createReq
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8<<10))
dec.DisallowUnknownFields()
if err := dec.Decode(&req); err != nil {
httpError(w, http.StatusBadRequest, "ungültiges JSON: "+err.Error())
return
}
if u, err := url.ParseRequestURI(req.URL); err != nil || u.Host == "" ||
(u.Scheme != "http" && u.Scheme != "https") {
httpError(w, http.StatusBadRequest, "url muss http(s)://host... sein")
return
}
if req.Code != "" && (!validCode.MatchString(req.Code) || reserved(req.Code)) {
httpError(w, http.StatusBadRequest, "code: 3-32 Zeichen A-Z a-z 0-9 _ -")
return
}
link := store.Link{Code: req.Code, URL: req.URL, Created: time.Now().UTC()}
var err error
for range 5 { // a random code rarely collides – then roll again
if req.Code == "" {
link.Code = randomCode()
}
err = s.store.Create(r.Context(), link)
if req.Code != "" || !errors.Is(err, store.ErrExists) {
break
}
}
switch {
case errors.Is(err, store.ErrExists):
httpError(w, http.StatusConflict, "code existiert bereits")
case err != nil:
s.internal(w, r, err)
default:
writeJSON(w, http.StatusCreated, createResp{link, s.baseURL + "/" + link.Code})
}
}
func (s *server) get(w http.ResponseWriter, r *http.Request) {
link, err := s.store.Get(r.Context(), r.PathValue("code"))
if s.handleErr(w, r, err) {
return
}
writeJSON(w, http.StatusOK, link)
}
func (s *server) delete(w http.ResponseWriter, r *http.Request) {
err := s.store.Delete(r.Context(), r.PathValue("code"))
if s.handleErr(w, r, err) {
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *server) redirect(w http.ResponseWriter, r *http.Request) {
link, err := s.store.Hit(r.Context(), r.PathValue("code"))
if s.handleErr(w, r, err) {
return
}
http.Redirect(w, r, link.URL, http.StatusFound)
}
// handleErr writes 404 or 500 and reports whether there was an error.
func (s *server) handleErr(w http.ResponseWriter, r *http.Request, err error) bool {
switch {
case err == nil:
return false
case errors.Is(err, store.ErrNotFound):
httpError(w, http.StatusNotFound, "unbekannter code")
default:
s.internal(w, r, err)
}
return true
}
// internal logs details; the client sees only a generic message.
func (s *server) internal(w http.ResponseWriter, r *http.Request, err error) {
s.log.ErrorContext(r.Context(), "interner Fehler", "path", r.URL.Path, "err", err)
httpError(w, http.StatusInternalServerError, "interner Fehler")
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func httpError(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, map[string]string{"error": msg})
}
func reserved(code string) bool {
return code == "api" || code == "healthz" || code == "metrics"
}
const alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
// randomCode: 7 characters without look-alikes (l/1, O/0) from crypto/rand.
func randomCode() string {
b := make([]byte, 7)
rand.Read(b) // never returns an error (Go 1.24+)
for i := range b {
b[i] = alphabet[int(b[i])%len(alphabet)]
}
return string(b)
}
EOFWhy so much validation? The server accepts data from the internet:
MaxBytesReader limits the body to 8 KiB,
DisallowUnknownFields makes typos in the client visible,
and allowing only http(s) prevents javascript:
links acting as an open redirect. Internal errors end up in the log,
never in the response.
Save your progress? Sign up for free
Drill 5: Middleware – logs, metrics, panics
statusRecorder embeds the ResponseWriter
and overrides only WriteHeader – that way the middleware
learns the status the handler set.
cat > internal/server/middleware.go <<'EOF'
package server
import (
"log/slog"
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// statusRecorder remembers the status code the handler writes.
type statusRecorder struct {
http.ResponseWriter
status int
}
func (r *statusRecorder) WriteHeader(code int) {
r.status = code
r.ResponseWriter.WriteHeader(code)
}
// logRequests writes one structured log line per request.
func logRequests(log *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(rec, r)
log.InfoContext(r.Context(), "request", "method", r.Method,
"path", r.URL.Path, "status", rec.status,
"ms", time.Since(start).Milliseconds())
})
}
// instrument counts requests per route (pattern, not path!) and status.
func instrument(reg *prometheus.Registry, next http.Handler) http.Handler {
requests := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "shorturl_http_requests_total",
Help: "HTTP-Anfragen nach Route und Status.",
}, []string{"route", "code"})
duration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "shorturl_http_request_duration_seconds",
Help: "Bearbeitungsdauer nach Route.",
Buckets: prometheus.DefBuckets,
}, []string{"route"})
reg.MustRegister(requests, duration)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(rec, r)
route := r.Pattern // set by the ServeMux, e.g. "GET /{code}"
if route == "" {
route = "unmatched"
}
requests.WithLabelValues(route, strconv.Itoa(rec.status)).Inc()
duration.WithLabelValues(route).Observe(time.Since(start).Seconds())
})
}
// recoverer catches panics: 500 instead of a dropped connection.
func recoverer(log *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if v := recover(); v != nil {
log.ErrorContext(r.Context(), "panic", "path", r.URL.Path, "panic", v)
httpError(w, http.StatusInternalServerError, "interner Fehler")
}
}()
next.ServeHTTP(w, r)
})
}
EOFWhy r.Pattern as the label and not
r.URL.Path? Otherwise every short code would be its own
time series – after a million links, Prometheus would have a million
series (cardinality explosion). The pattern GET /{code} is
exactly one.
Save your progress? Sign up for free
Drill 6: main – timeouts and graceful shutdown
An http.Server without timeouts can be crippled with
slowly trickling headers (Slowloris). ListenAndServe runs
in a goroutine; main waits via select for an
error or a signal.
cat > cmd/shorturl/main.go <<'EOF'
// shorturl is a URL shortener with a JSON API, metrics and graceful shutdown.
package main
import (
"cmp"
"context"
"errors"
"flag"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"example.com/shorturl/internal/server"
"example.com/shorturl/internal/store"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
)
func main() {
addr := flag.String("addr", cmp.Or(os.Getenv("SHORTURL_ADDR"), "127.0.0.1:8080"),
"Listen-Adresse")
baseURL := flag.String("base-url", "", "öffentliche URL (Default http://ADDR)")
flag.Parse()
if *baseURL == "" {
*baseURL = "http://" + *addr
}
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
if err := serve(*addr, *baseURL, log); err != nil {
log.Error("Abbruch", "err", err)
os.Exit(1)
}
}
func serve(addr, baseURL string, log *slog.Logger) error {
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
srv := &http.Server{
Addr: addr,
Handler: server.New(store.NewMemory(), log, baseURL, reg),
ReadHeaderTimeout: 5 * time.Second, // protection against Slowloris
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
// Kubernetes sends SIGTERM when stopping a pod, SIGINT is Ctrl+C.
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt, syscall.SIGTERM)
defer stop()
errc := make(chan error, 1)
go func() {
log.Info("shorturl startet", "addr", addr, "base_url", baseURL)
errc <- srv.ListenAndServe()
}()
select {
case err := <-errc: // e.g. port in use
return err
case <-ctx.Done():
}
log.Info("fahre herunter, warte auf laufende Anfragen")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
return err
}
if err := <-errc; !errors.Is(err, http.ErrServerClosed) {
return err
}
log.Info("sauber beendet")
return nil
}
EOFWhy 127.0.0.1 as the default? In the lab, the server
should not be reachable on your Wi-Fi. In a container you set
SHORTURL_ADDR=:8080.
Start the server in the background and play it through:
go mod tidy && go build -o shorturl ./cmd/shorturl
./shorturl > server.log &
sleep 1
curl -s localhost:8080/api/links \
-d '{"url":"https://go.dev/doc/effective_go","code":"effgo"}' | jq -c '{code, short}'
curl -s localhost:8080/api/links -d '{"url":"https://openbao.org"}' | jq -c '{code}'
curl -si localhost:8080/effgo | grep -E '^(HTTP|Location)'
curl -s localhost:8080/api/links/effgo | jq -c '{code, hits}'
curl -s localhost:8080/api/links -d '{"url":"javascript:alert(1)"}'
curl -si -X PUT localhost:8080/api/links/effgo | grep -E '^(HTTP|Allow)'
curl -s -X DELETE -w '%{http_code}\n' localhost:8080/api/links/effgo
curl -s localhost:8080/effgo
curl -s localhost:8080/metrics | grep '^shorturl_http_requests_total'
kill -TERM %1; sleep 1
jq -c 'del(.time, .level)' server.log | sed -n '1p;5p;$p'Expected (code and times vary):
{"code":"effgo","short":"http://127.0.0.1:8080/effgo"}
{"code":"RhRn9iG"}
HTTP/1.1 302 Found
Location: https://go.dev/doc/effective_go
{"code":"effgo","hits":1}
{"error":"url muss http(s)://host... sein"}
HTTP/1.1 405 Method Not Allowed
Allow: DELETE, GET, HEAD
204
{"error":"unbekannter code"}
shorturl_http_requests_total{code="200",route="GET /api/links/{code}"} 1
shorturl_http_requests_total{code="201",route="POST /api/links"} 2
shorturl_http_requests_total{code="204",route="DELETE /api/links/{code}"} 1
shorturl_http_requests_total{code="302",route="GET /{code}"} 1
shorturl_http_requests_total{code="400",route="POST /api/links"} 1
shorturl_http_requests_total{code="404",route="GET /{code}"} 1
shorturl_http_requests_total{code="405",route="unmatched"} 1
{"msg":"shorturl startet","addr":"127.0.0.1:8080","base_url":"http://127.0.0.1:8080"}
{"msg":"request","method":"GET","path":"/api/links/effgo","status":200,"ms":0}
{"msg":"sauber beendet"}
Why unmatched for the 405? No pattern matched
completely, so the mux doesn’t set r.Pattern.
GET automatically allows HEAD too. And
kill -TERM is exactly what Kubernetes sends when stopping a
pod – the server reports “sauber beendet” (“shut down cleanly”) instead
of breaking off.
Save your progress? Sign up for free
Drill 7: Handler tests with httptest
The complete handler stack runs against an
httptest.Server – with its own Prometheus registry per
test, so that metrics don’t get registered twice. The client
deliberately doesn’t follow redirects.
cat > internal/server/server_test.go <<'EOF'
package server
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"example.com/shorturl/internal/store"
"github.com/prometheus/client_golang/prometheus"
)
func newTestServer(t *testing.T) *httptest.Server {
t.Helper()
log := slog.New(slog.NewTextHandler(io.Discard, nil))
h := New(store.NewMemory(), log, "http://short.test", prometheus.NewRegistry())
srv := httptest.NewServer(h)
t.Cleanup(srv.Close)
return srv
}
// noRedirect: the test wants to see the 302, not follow the link.
var noRedirect = &http.Client{
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
func do(t *testing.T, method, url, body string) *http.Response {
t.Helper()
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
resp, err := noRedirect.Do(req)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { resp.Body.Close() })
return resp
}
const godev = `{"url":"https://go.dev","code":"godev"}`
func TestLebenszyklus(t *testing.T) {
srv := newTestServer(t)
resp := do(t, "POST", srv.URL+"/api/links", godev)
var created createResp
if err := json.NewDecoder(resp.Body).Decode(&created); err != nil ||
resp.StatusCode != 201 || created.Short != "http://short.test/godev" {
t.Fatalf("create: %d %+v %v", resp.StatusCode, created, err)
}
resp = do(t, "GET", srv.URL+"/godev", "")
if resp.StatusCode != 302 || resp.Header.Get("Location") != "https://go.dev" {
t.Errorf("redirect: %d %q", resp.StatusCode, resp.Header.Get("Location"))
}
var link store.Link
resp = do(t, "GET", srv.URL+"/api/links/godev", "")
if err := json.NewDecoder(resp.Body).Decode(&link); err != nil || link.Hits != 1 {
t.Errorf("stats: %+v %v", link, err)
}
if resp = do(t, "DELETE", srv.URL+"/api/links/godev", ""); resp.StatusCode != 204 {
t.Errorf("delete: %d", resp.StatusCode)
}
if resp = do(t, "GET", srv.URL+"/godev", ""); resp.StatusCode != 404 {
t.Errorf("nach delete: %d", resp.StatusCode)
}
}
func TestFehlerfaelle(t *testing.T) {
srv := newTestServer(t)
do(t, "POST", srv.URL+"/api/links", godev)
tests := []struct {
name, method, path, body string
want int
}{
{"kein JSON", "POST", "/api/links", `url=x`, 400},
{"unbekanntes Feld", "POST", "/api/links", `{"url":"https://a.b","x":1}`, 400},
{"ftp-URL", "POST", "/api/links", `{"url":"ftp://a.b"}`, 400},
{"reserviert", "POST", "/api/links", `{"url":"https://a.b","code":"api"}`, 400},
{"Code belegt", "POST", "/api/links", godev, 409},
{"falsche Methode", "PUT", "/api/links/godev", ``, 405},
{"unbekannt", "GET", "/api/links/nix", ``, 404},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp := do(t, tt.method, srv.URL+tt.path, tt.body)
if resp.StatusCode != tt.want {
t.Errorf("Status %d, want %d", resp.StatusCode, tt.want)
}
})
}
}
func TestZufallscodeUndMetriken(t *testing.T) {
srv := newTestServer(t)
resp := do(t, "POST", srv.URL+"/api/links", `{"url":"https://kubernetes.io"}`)
var created createResp
if err := json.NewDecoder(resp.Body).Decode(&created); err != nil ||
!validCode.MatchString(created.Code) {
t.Fatalf("Code %q, %v", created.Code, err)
}
do(t, "GET", srv.URL+"/"+created.Code, "")
body, _ := io.ReadAll(do(t, "GET", srv.URL+"/metrics", "").Body)
want := `shorturl_http_requests_total{code="302",route="GET /{code}"} 1`
if !strings.Contains(string(body), want) {
t.Errorf("Metrik fehlt: %s", want)
}
}
EOFgo test -race -count=1 -v ./internal/server 2>&1 | grep -E '^(\s*---|ok)'Expected:
--- PASS: TestLebenszyklus (0.00s)
--- PASS: TestFehlerfaelle (0.00s)
--- PASS: TestFehlerfaelle/kein_JSON (0.00s)
--- PASS: TestFehlerfaelle/unbekanntes_Feld (0.00s)
--- PASS: TestFehlerfaelle/ftp-URL (0.00s)
--- PASS: TestFehlerfaelle/reserviert (0.00s)
--- PASS: TestFehlerfaelle/Code_belegt (0.00s)
--- PASS: TestFehlerfaelle/falsche_Methode (0.00s)
--- PASS: TestFehlerfaelle/unbekannt (0.00s)
--- PASS: TestZufallscodeUndMetriken (0.00s)
ok example.com/shorturl/internal/server 1.196s
Tip: For individual handlers without a network there is
httptest.NewRecorder(). A realhttptest.Serveradditionally tests the mux, middleware and HTTP semantics (405, redirects) – usually the better choice for an API.
Save your progress? Sign up for free
Drill 8: The tool is done
cp ../tag08-urlcheck/.golangci.yml .
gofmt -l . && go vet ./... && ~/go-lab/bin/lint run ./...
go test -race -count=1 ./...Expected:
0 issues.
? example.com/shorturl/cmd/shorturl [no test files]
ok example.com/shorturl/internal/server 1.210s
ok example.com/shorturl/internal/store 1.302s
? example.com/shorturl/internal/store/storetest [no test files]
One more time with an eye on Prometheus – besides your metrics, the registry provides Go runtime and process values:
./shorturl > server.log &
sleep 1
curl -s localhost:8080/metrics | grep -E '^(go_goroutines|process_resident)'
kill -TERM %1Expected (values vary):
go_goroutines 9
process_resident_memory_bytes 8.716288e+06
From now on, the rule for shorturl is: the API stays,
the storage changes. On Day 10 you implement store.Store
with SQLite and call storetest.Run with it.
Save your progress? Sign up for free
Checkpoint
cd ~/go-lab/tag09-shorturl
ok() { echo "✔ $1"; }; nok() { echo "✘ $1"; }
go vet ./... && go test -race -count=1 ./... >/dev/null && ok "Tests mit -race" \
|| nok "Tests"
./shorturl > server.log & sleep 1
curl -s localhost:8080/api/links -d '{"url":"https://go.dev","code":"check"}' \
| grep -q '"short"' && ok "Link angelegt" || nok "POST"
[ "$(curl -s -o /dev/null -w '%{http_code}' localhost:8080/check)" = 302 ] \
&& ok "Redirect 302" || nok "Redirect"
curl -s localhost:8080/metrics | grep -q 'route="GET /{code}"' \
&& ok "Metrik mit Routenmuster" || nok "Metriken"
kill -TERM %1; sleep 1
grep -q '"sauber beendet"' server.log && ok "Graceful Shutdown" || nok "Shutdown"Expected:
✔ Tests mit -race
✔ Link angelegt
✔ Redirect 302
✔ Metrik mit Routenmuster
✔ Graceful Shutdown
Challenge
Build a middleware requestID: it takes over an incoming
X-Request-ID header (from the ingress) or generates a new
ID, sets it in the response, and logRequests writes it to
the log as request_id.
Hint: rand.Text() from
crypto/rand (since Go 1.24) returns a random 26-character
ID; the middleware has to run before
logRequests.
Solution
cat >> internal/server/middleware.go <<'EOF'
// requestID takes over X-Request-ID from the client/ingress or generates a new one.
func requestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.Header.Get("X-Request-ID")
if id == "" {
id = rand.Text() // 26 characters of Base32 from crypto/rand
r.Header.Set("X-Request-ID", id)
}
w.Header().Set("X-Request-ID", id)
next.ServeHTTP(w, r)
})
}
EOFThen three small changes: in middleware.go, import
"crypto/rand" and add the attribute in
logRequests; in server.go, extend the
chain:
"path", r.URL.Path, "status", rec.status,
"request_id", r.Header.Get("X-Request-ID"),
return recoverer(log, instrument(reg, requestID(logRequests(log, mux))))
Append a test:
cat >> internal/server/server_test.go <<'EOF'
func TestRequestID(t *testing.T) {
srv := newTestServer(t)
id := do(t, "GET", srv.URL+"/healthz", "").Header.Get("X-Request-ID")
if len(id) != 26 {
t.Errorf("neue ID = %q", id)
}
req, _ := http.NewRequest("GET", srv.URL+"/healthz", nil)
req.Header.Set("X-Request-ID", "lab-123")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if got := resp.Header.Get("X-Request-ID"); got != "lab-123" {
t.Errorf("übernommene ID = %q", got)
}
}
EOF
go test -race -count=1 -run RequestID -v ./internal/server | grep -E '^(---|ok)'
go build -o shorturl ./cmd/shorturl && ./shorturl > server.log & sleep 1
curl -si localhost:8080/healthz -H 'X-Request-ID: lab-123' | grep -i request-id
kill -TERM %1; sleep 1
jq -c '{msg, path, request_id}' server.log | sed -n 2pExpected:
--- PASS: TestRequestID (0.00s)
ok example.com/shorturl/internal/server 1.197s
X-Request-Id: lab-123
{"msg":"request","path":"/healthz","request_id":"lab-123"}
Go writes headers in canonical form (X-Request-Id) –
HTTP headers are case-insensitive anyway.
Save your progress? Sign up for free
Enterprise Note
- An in-memory store loses all data on every rollout and doesn’t work with several replicas – hence Day 10 (SQLite), or PostgreSQL in production.
- For Kubernetes: separate liveness (
/healthz) and readiness; on shutdown, set readiness to “not ready” immediately, plus apreStopsleep so the load balancer stops sending new requests. - Don’t expose
/metricspublicly: a separate port or a NetworkPolicy, scraped via a ServiceMonitor. - URL shorteners are phishing magnets: rate limit per client,
authentication for
POST/DELETE, checks against Safe Browsing lists. - Logs without personal data (no full query strings, no IPs without a legal basis) – GDPR applies to access logs too.