Skip to content

Commit

Permalink
docs: update example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Apr 12, 2024
1 parent ea0834b commit 3795ddc
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ You can find a simple example in the [example_test.go](example_test.go) file:
package tlscert_test

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
Expand All @@ -47,8 +45,8 @@ func ExampleSelfSigned() {
certsDir := tmp + "/certs"
defer os.RemoveAll(certsDir)

if err := os.MkdirAll(certsDir, 0755); err != nil {
log.Fatal(err)
if err := os.MkdirAll(certsDir, 0o755); err != nil {
log.Fatal(err) // nolint: gocritic
}

// Generate a certificate for localhost and save it to disk.
Expand Down Expand Up @@ -81,7 +79,10 @@ func ExampleSelfSigned() {

server.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("TLS works!\n"))
_, err := w.Write([]byte("TLS works!\n"))
if err != nil {
log.Printf("Failed to write response: %v", err)
}
})

go func() {
Expand All @@ -91,20 +92,9 @@ func ExampleSelfSigned() {

// perform an HTTP request to the server, using the generated certificate

caCertPool := x509.NewCertPool()
caCertPool.AddCert(cert.Cert)

tlsConfig := &tls.Config{
RootCAs: caCertPool,
}

tr := &http.Transport{
TLSClientConfig: tlsConfig,
}

const url = "https://localhost:8443/hello"

client := &http.Client{Transport: tr}
client := &http.Client{Transport: cert.Transport()}
resp, err := client.Get(url)
if err != nil {
log.Fatalf("Failed to get response: %v", err)
Expand All @@ -120,6 +110,5 @@ func ExampleSelfSigned() {

// Output:
// TLS works!

}
```

0 comments on commit 3795ddc

Please sign in to comment.