Created
April 25, 2016 14:45
-
-
Save bouk/825b97c5ef2990bf58ec503ace809f95 to your computer and use it in GitHub Desktop.
Go server with automatic Let's Encrypt registration and graceful restarts
This file contains hidden or 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