Skip to content

Instantly share code, notes, and snippets.

@oscartbeaumont
Created January 26, 2019 11:59
Show Gist options
  • Save oscartbeaumont/5c552ec1a62cc74eb6742428eb79a143 to your computer and use it in GitHub Desktop.
Save oscartbeaumont/5c552ec1a62cc74eb6742428eb79a143 to your computer and use it in GitHub Desktop.
A Basic Go Webserver Using Certmagic listening on the ports 8443 and 8080.
package main
import (
"log"
"net/http"
"github.com/mholt/certmagic"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
magic := certmagic.New(certmagic.Config{
CA: certmagic.LetsEncryptStagingCA,
Email: "[email protected]",
Agreed: true,
//AltHTTPPort: 8080,
AltTLSALPNPort: 8443,
})
err := magic.Manage([]string{"example.com"})
if err != nil {
log.Fatal(err)
}
s := http.Server{
Addr: "8443",
Handler: mux,
TLSConfig: magic.TLSConfig(),
}
log.Println("listening...")
go log.Fatal(http.ListenAndServe(":8080", magic.HTTPChallengeHandler(mux)))
log.Fatal(s.ListenAndServeTLS("", ""))
}
@marcstreeter
Copy link

not sure how this works given that certmagic.New has an arity of two

@oscartbeaumont
Copy link
Author

You are correct. I assume certmagic have changed their api since I wrote this sample.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment