-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
51 lines (45 loc) · 1.29 KB
/
example_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
package risefront_test
import (
"context"
"errors"
"net"
"net/http"
"os"
"strconv"
"time"
"code.pfad.fr/risefront"
)
func newServer() *http.Server {
// This is an example server, which shows the PID of its process.
// Start processes in parallel to show the forwarding of the
// incoming connection to the latest process.
return &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world " + strconv.Itoa(os.Getpid()) + "\n"))
if r.URL.Path == "/hang" {
time.Sleep(10 * time.Second)
}
w.Write([]byte("bye " + strconv.Itoa(os.Getpid())))
}),
}
}
func Example_http() {
// In production, use signal.NotifyContext to interrupt on Ctrl+C:
// ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := risefront.New(ctx, risefront.Config{
Addresses: []string{":8080"},
Run: func(l []net.Listener) error {
// Example http.Server
s := newServer()
// defer Shutdown to wait for ongoing requests to be served before returning
defer s.Shutdown(context.Background())
return s.Serve(l[0]) // serve on the given listener
},
})
if !errors.Is(err, context.DeadlineExceeded) {
panic(err)
}
// Output:
}