HTTPS – setting up a TLS server
To start a TLS server, you need a certificate and a private key. You can either purchase one from a CA or generate our own certificates with your internal CA. Once you have your certificate, you can use the recipes in this section to start your HTTPS server.
How to do it...
To create a TLS HTTP server, use one of the following:
- Use the
Server.ListenAndServeTLS
method with the certificate and key files:server := http.Server { Addr: ":4443", Handler: handler, } server.ListenAndServeTLS("cert.pem", "key.pem")
- To use the default HTTP server, set a handler function (or
http.Handler
) and callhttp.ListenAndServeTLS
:http.HandleFunc("/",func(w http.ResponseWriter, req *http.Request) { // Handle request }) http.ListenAndServeTLS("cert.pem", "key.pem")
- Or prepare a
http.Transport
with certificates:3.1 Load the TLS certificate:
cert...