- add certificate and privkey to the project folder
- call via https, NOT http!
- "GetCertificate() called!" is the output when the certificate get (re)loaded after not being cached anymore or when another browser is used
-
-
Save d4z3x/b9f617a06f8118b0db688018998b257d to your computer and use it in GitHub Desktop.
Golang: Automatic TLS Certificate Reload
This file contains 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" | |
"fmt" | |
"io" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", handler) | |
s := &http.Server{ | |
Addr: ":8080", | |
TLSConfig: &tls.Config{ | |
GetCertificate: getCertificate, | |
}, | |
} | |
if err := s.ListenAndServeTLS("", ""); err != nil { | |
fmt.Println(err) | |
} | |
} | |
func getCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error) { | |
fmt.Println("GetCertificate() called!") | |
fmt.Printf("ServerName: %s\n", info.ServerName) | |
caFiles, err := tls.LoadX509KeyPair("cert.pem", "key.pem") | |
if err != nil { | |
return nil, err | |
} | |
return &caFiles, nil | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("handler hit") | |
io.WriteString(w, "Hey") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment