Enabling HTTPS in a Go Application
2024-11-21
If you don't want to use a reverse proxy like Caddy or Nginx, you can add HTTPS support directly to your Go application, using the module certmagic
, developed by the people behind the Caddy server project.1
Setup
Install the certmagic
module: go get github.com/caddyserver/certmagic
Write your code:
package main
import (
"flag"
"net/http"
"github.com/caddyserver/certmagic"
)
func main()
To run the HTTP server with TLS/HTTPS enabled:
go run main.go -production
Note: Unless you have it set up by other means, certmagic
does not support HTTPS on localhost
.
Note: Multiple Go applications running on the same port will panic. In this case use a reverse-proxy.