Skip to content

Instantly share code, notes, and snippets.

@mehmetkose
Forked from bouk/server.go
Created October 10, 2017 16:58
Show Gist options
  • Save mehmetkose/867a4fe8e4bf9d05c489121808c0fd25 to your computer and use it in GitHub Desktop.
Save mehmetkose/867a4fe8e4bf9d05c489121808c0fd25 to your computer and use it in GitHub Desktop.
Go server with automatic Let's Encrypt registration and graceful restarts
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