-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
72 lines (62 loc) · 1.75 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: MIT
//
// Copyright 2020 Andrew Bursavich. All rights reserved.
// Use of this source code is governed by The MIT License
// which can be found in the LICENSE file.
//go:build go1.14
// +build go1.14
package dynamictls_test
import (
"context"
"crypto/tls"
"net/http"
"bursavich.dev/dynamictls"
"bursavich.dev/dynamictls/tlsprom"
"github.com/prometheus/client_golang/prometheus"
)
func ExampleConfig_Listen() {
observer, err := tlsprom.NewObserver(
tlsprom.WithHTTP(),
tlsprom.WithServer(),
)
check(err)
prometheus.MustRegister(observer)
cfg, err := dynamictls.NewConfig(
dynamictls.WithObserver(observer),
dynamictls.WithCertificate(primaryCertFile, primaryKeyFile),
dynamictls.WithCertificate(secondaryCertFile, secondaryKeyFile),
dynamictls.WithRootCAs(caFile),
dynamictls.WithHTTP(), // NB: adds HTTP/2 and HTTP/1.1 protocols
)
check(err)
defer cfg.Close()
lis, err := cfg.Listen(context.Background(), "tcp", addr)
check(err)
check(http.Serve(lis, http.DefaultServeMux))
}
func ExampleConfig_Dial() {
observer, err := tlsprom.NewObserver(
tlsprom.WithHTTP(),
tlsprom.WithClient(),
)
check(err)
prometheus.MustRegister(observer)
cfg, err := dynamictls.NewConfig(
dynamictls.WithObserver(observer),
dynamictls.WithBase(&tls.Config{
MinVersion: tls.VersionTLS12,
}),
dynamictls.WithCertificate(certFile, keyFile),
dynamictls.WithRootCAs(caFile),
dynamictls.WithHTTP(), // NB: adds HTTP/2 and HTTP/1.1 protocols
)
check(err)
defer cfg.Close()
client := &http.Client{
Transport: &http.Transport{
DialTLSContext: cfg.Dial, // NB: DialTLSContext added in go 1.14
ForceAttemptHTTP2: true, // NB: required if using a custom dialer with HTTP/2
},
}
defer client.CloseIdleConnections()
}