-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.go
More file actions
42 lines (35 loc) · 743 Bytes
/
server.go
File metadata and controls
42 lines (35 loc) · 743 Bytes
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
package main
import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"net/http"
)
type httpsHandler struct {
}
func (*httpsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "golang https server!!!")
}
func main() {
pool := x509.NewCertPool()
caCertPath := "root.crt"
caCrt, err := ioutil.ReadFile(caCertPath)
if err != nil {
log.Fatal("ReadFile err:", err)
return
}
pool.AppendCertsFromPEM(caCrt)
s := &http.Server{
Addr: ":8080",
Handler: &httpsHandler{},
TLSConfig: &tls.Config{
ClientCAs: pool,
ClientAuth: tls.RequireAndVerifyClientCert,
},
}
if err = s.ListenAndServeTLS("server.crt", "server.key"); err != nil {
log.Fatal("ListenAndServeTLS err:", err)
}
}