// Chapter 47 example: an http.Server of your own, with time limits. // Run with: go run exercises/47/04-server-timeouts.go // Then open http://localhost:8080 in a browser. Press Ctrl+C to stop it. package main import ( "fmt" "log/slog" "net/http" "os" "time" ) func handleHome(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello from a server with time limits.") } func main() { mux := http.NewServeMux() mux.HandleFunc("GET /{$}", handleHome) srv := &http.Server{ Addr: "localhost:8080", Handler: mux, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, } slog.Info("server starting", "addr", srv.Addr) err := srv.ListenAndServe() slog.Error("server stopped", "err", err) os.Exit(1) }