Created
June 6, 2017 06:24
-
-
Save DavadDi/4b04d349acef01e52c902a8add41bc21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/tls" | |
| "net/http" | |
| "time" | |
| "log" | |
| ) | |
| func myHandler(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte("tls")) | |
| } | |
| func main() { | |
| t := log.Logger{} | |
| var err error | |
| tlsConfig := &tls.Config{} | |
| tlsConfig.Certificates = make([]tls.Certificate, 3) | |
| // go http server treats the 0'th key as a default fallback key | |
| tlsConfig.Certificates[0], err = tls.LoadX509KeyPair("test0.pem", "key.pem") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| tlsConfig.Certificates[1], err = tls.LoadX509KeyPair("test1.pem", "key.pem") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| tlsConfig.Certificates[2], err = tls.LoadX509KeyPair("test2.pem", "key.pem") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| tlsConfig.BuildNameToCertificate() | |
| http.HandleFunc("/", myHandler) | |
| server := &http.Server{ | |
| ReadTimeout: 10 * time.Second, | |
| WriteTimeout: 10 * time.Second, | |
| MaxHeaderBytes: 1 << 20, | |
| TLSConfig: tlsConfig, | |
| } | |
| listener, err := tls.Listen("tcp", ":8443", tlsConfig) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| log.Fatal(server.Serve(listener)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment