-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_examples_test.go
66 lines (56 loc) · 1.47 KB
/
http_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ctrl_test
import (
"bytes"
"context"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/go-pkgz/ctrl"
)
// Example_httpServerWithContext demonstrates how to run an HTTP server that shuts down
// gracefully when the parent context is canceled.
func Example_httpServerWithContext() {
// create a context that we can cancel to trigger shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a simple HTTP handler
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})
// create server
server := &http.Server{
Addr: "localhost:0", // random port
Handler: mux,
}
// create a custom logger for the example
var logBuf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelInfo}))
// start server with options
errCh := ctrl.RunHTTPServerWithContext(
ctx,
server,
func() error {
return server.ListenAndServe()
},
ctrl.WithHTTPShutdownTimeout(15*time.Second),
ctrl.WithHTTPLogger(logger),
)
// for example only - trigger shutdown after a brief delay
go func() {
time.Sleep(100 * time.Millisecond)
fmt.Println("Triggering shutdown...")
cancel()
}()
// wait for server to exit and check for errors
err := <-errCh
if err != nil {
fmt.Println("Server error:", err)
} else {
fmt.Println("Server stopped gracefully")
}
// Output:
// Triggering shutdown...
// Server stopped gracefully
}