-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
50 lines (40 loc) · 1.17 KB
/
server.go
File metadata and controls
50 lines (40 loc) · 1.17 KB
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
package main
import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
// Write hello world to the response body
io.WriteString(w, "🍉 Hello, world 🍉")
}
func main() {
// Set up a /hello resource handler
http.HandleFunc("/hello", helloHandler)
// Listen to port 8443 and wait
// log.Fatal(http.ListenAndServeTLS(":8443", "devlocal-mtls.cer", "devlocal-mtls.key", nil))
// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("devlocal-ca.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create TLS Config with the CA pool and enable
// client certificate validation
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()
// Create a server instance to listen on port 8443 with the TLS
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
}
// Listen to https connection with server certificate and wait
log.Fatal(server.ListenAndServeTLS("devlocal-mtls.cer", "devlocal-mtls.key"))
}