How to Use gRPC with TLS in Go

Web
Configure a Go gRPC server with TLS by loading certificates and setting the server's TLSConfig.

Use crypto/tls to load your certificates and configure the http.Server with TLSConfig before calling ListenAndServeTLS.

package main

import (
	"crypto/tls"
	"log"
	"net/http"
)

func main() {
	cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		log.Fatal(err)
	}

	server := &http.Server{
		Addr: ":443",
		TLSConfig: &tls.Config{
			Certificates: []tls.Certificate{cert},
		},
	}

	log.Fatal(server.ListenAndServeTLS("", ""))
}