-
-
Save mehmetkose/867a4fe8e4bf9d05c489121808c0fd25 to your computer and use it in GitHub Desktop.
Go server with automatic Let's Encrypt registration and graceful restarts
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" | |
"github.com/facebookgo/grace/gracehttp" | |
"log" | |
"net/http" | |
"rsc.io/letsencrypt" | |
) | |
func main() { | |
var m letsencrypt.Manager | |
if err := m.CacheFile("letsencrypt.cache"); err != nil { | |
log.Fatal(err) | |
} | |
httpServer := &http.Server{ | |
Addr: ":http", | |
Handler: http.HandlerFunc(letsencrypt.RedirectHTTP), | |
} | |
httpsServer := &http.Server{ | |
Addr: ":https", | |
Handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | |
rw.Write([]byte("OK")) | |
}), | |
TLSConfig: &tls.Config{ | |
GetCertificate: m.GetCertificate, | |
}, | |
} | |
if err := gracehttp.Serve(httpServer, httpsServer); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment