From 1fd8bb1e87d4d5c7497cd3189238b945c49f7cc8 Mon Sep 17 00:00:00 2001 From: Daniel Kumor Date: Sat, 19 Mar 2016 04:45:18 -0500 Subject: [PATCH] Enabled http2 support in examples uncreativemynameis on reddit pointed out that the examples don't have http2 support since they don't pass in tlsconfig to the http server. https://www.reddit.com/r/golang/comments/4axi8q/acmewrapper_add_lets_encrypt_support_to_your_go/d15fwi3 This is the go issue: https://github.com/golang/go/issues/14374 The example and readme were changed to have http2 enabled. --- README.md | 27 ++++++++++++++++++++++----- example/example.go | 19 ++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 915981f..888e73c 100755 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ w, err := acmewrapper.New(acmewrapper.Config{ if err!=nil { - log.Fatal("Let's Encrypt: ", err) + log.Fatal("acmewrapper: ", err) } listener, err := tls.Listen("tcp", ":443", w.TLSConfig()) @@ -46,6 +46,10 @@ This means that *no other changes* are needed to your code. You don't need any s ## Example +You can go into `./example` to find a sample basic http server that will serve a given folder over https with Let's Encrypt. + +Another simple example is given below: + ### Old Code This is sample code before adding Let's Encrypt support: @@ -74,6 +78,8 @@ func main() { ### New Code +Adding let's encrypt support is a matter of setting the tls config: + ```go package main @@ -91,7 +97,8 @@ func HelloServer(w http.ResponseWriter, req *http.Request) { } func main() { - http.HandleFunc("/hello", HelloServer) + mux := http.NewServeMux() + mux.HandleFunc("/hello", HelloServer) w, err := acmewrapper.New(acmewrapper.Config{ Domains: []string{"example.com","www.example.com"}, @@ -108,14 +115,24 @@ func main() { if err!=nil { - log.Fatal("Let's Encrypt: ", err) + log.Fatal("acmewrapper: ", err) } - listener, err := tls.Listen("tcp", ":443", w.TLSConfig()) + tlsconfig := w.TLSConfig() + + listener, err := tls.Listen("tcp", ":443", tlsconfig) if err != nil { log.Fatal("Listener: ", err) } - http.Serve(listener, nil) + + // To enable http2, we need http.Server to have reference to tlsconfig + // https://github.com/golang/go/issues/14374 + server := &http.Server{ + Addr: ":443", + Handler: mux, + TLSConfig: tlsconfig, + } + server.Serve(listener) } ``` diff --git a/example/example.go b/example/example.go index d6c1119..df5f5fc 100755 --- a/example/example.go +++ b/example/example.go @@ -42,6 +42,9 @@ func main() { *acme = "https://acme-staging.api.letsencrypt.org/directory" } + mux := http.NewServeMux() + mux.Handle("/", http.FileServer(http.Dir(flag.Arg(flag.NArg()-1)))) + w, err := acmewrapper.New(acmewrapper.Config{ Address: *address, @@ -64,13 +67,23 @@ func main() { os.Exit(1) } - http.Handle("/", http.FileServer(http.Dir(flag.Arg(flag.NArg()-1)))) + tlsconfig := w.TLSConfig() - listener, err := tls.Listen("tcp", *address, w.TLSConfig()) + listener, err := tls.Listen("tcp", *address, tlsconfig) if err != nil { fmt.Printf("ERROR: %s", err.Error()) os.Exit(1) } + fmt.Printf("\n\nRunning server at %s\n\n", *address) - http.Serve(listener, nil) + + // In order to enable http2, we can't just use http.Serve in go1.6, so we need + // to create a manual http.Server, since it needs the tlsconfig + // https://github.com/golang/go/issues/14374 + server := &http.Server{ + Addr: *address, + Handler: mux, + TLSConfig: tlsconfig, + } + server.Serve(listener) }