Created
October 13, 2020 04:29
-
-
Save snadrus/1ad2bff976650a39e953c6d0afa070ae to your computer and use it in GitHub Desktop.
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
// StartServer serves Handler over HTTPS(443) with a redirect on 80 | |
// using an ACME certificate it creates & is cached in ./certs/ | |
// and has sensible timeouts to avoid nasty clients. | |
// Note the timeouts don't work for slow transfers, | |
// but oddly are ok for websockets. | |
func StartServer(engine http.Handler, ...hostname string) | |
certManager := autocert.Manager{ | |
Prompt: autocert.AcceptTOS, | |
HostPolicy: autocert.HostWhitelist(hostname...), | |
Cache: autocert.DirCache("certs"), | |
} | |
s := &http.Server{ | |
Addr: ":https", | |
Handler: engine, | |
TLSConfig: certManager.TLSConfig(), | |
WriteTimeout: 10 * time.Second, | |
IdleTimeout: 120 * time.Second, | |
ReadHeaderTimeout: 2 * time.Second, | |
} | |
go http.ListenAndServe(":80", certManager.HTTPHandler(nil)) | |
log.Fatal(s.ListenAndServeTLS("", "")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment