Lezioni del modulo (5/5)
Mini-progetto: un server HTTP
Il pacchetto net/http della stdlib basta a costruire un server HTTP production-ready: routing, handler, middleware, graceful shutdown. Niente framework obbligatorio. Per le API REST tipiche questa è la base.
Hello world
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ciao %s", r.URL.Path[1:])
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}http.ListenAndServe blocca e ritorna solo in caso di errore. nil come secondo argomento = "usa il DefaultServeMux".
La firma dell'handler
Tutti gli handler hanno la stessa firma:
func(w http.ResponseWriter, r *http.Request)w http.ResponseWriter— output: header, status code, body.r *http.Request— input: metodo, URL, header, body, context.
Pattern tipico:
func ping(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) // opzionale, default 200
fmt.Fprintln(w, `{"ok":true}`)
}Routing con ServeMux
Il *http.ServeMux è il router base. Da Go 1.22 supporta pattern con metodo e path parameter:
mux := http.NewServeMux()
mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
fmt.Fprintln(w, "user", id)
})
mux.HandleFunc("POST /users", createUser)
http.ListenAndServe(":8080", mux)Per router più sofisticati (gruppi, middleware integrati, regex) c'è chi, gorilla/mux, gin, echo. La stdlib copre il 90% dei casi.
Middleware come decorator
Un middleware è una funzione func(http.Handler) http.Handler:
func logMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
})
}
http.ListenAndServe(":8080", logMW(mux))Per concatenare: logMW(authMW(rateLimitMW(mux))). Ogni middleware decora il successivo.
http.Server per controllo fine
http.ListenAndServe(addr, h) è una scorciatoia. Per timeout, TLS, graceful shutdown serve istanziare http.Server:
srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 2 * time.Minute,
}
log.Fatal(srv.ListenAndServe())Graceful shutdown con context
Quando il processo riceve SIGINT/SIGTERM, vuoi terminare le richieste in corso prima di chiudere:
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt, syscall.SIGTERM)
defer stop()
srv := &http.Server{Addr: ":8080", Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
<-ctx.Done() // attendi segnale
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Println("shutdown:", err)
}srv.Shutdown smette di accettare nuove connessioni e attende che quelle attive terminino (fino al deadline passato).
Lettura del body JSON
type CreateUserReq struct {
Name string `json:"name"`
}
func createUser(w http.ResponseWriter, r *http.Request) {
var req CreateUserReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// ... usa req.Name
w.WriteHeader(http.StatusCreated)
}r.Body è un io.ReadCloser: lo chiude automaticamente il server, ma json.NewDecoder non legge oltre il primo oggetto JSON (utile per validare input).
Esercizi
Registra l'handler hello sulla route '/' usando http.HandleFunc.
Soluzione disponibile dopo 3 tentativi
Avvia il server su :8080 con http.ListenAndServe e stampa l'errore se non è nil.
Soluzione disponibile dopo 3 tentativi
Qual è la firma idiomatica di un handler HTTP in Go?
func H(???) { ... }