Skip to content

Commit

Permalink
Enabled http2 support in examples
Browse files Browse the repository at this point in the history
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:
golang/go#14374

The example and readme were changed to have http2 enabled.
  • Loading branch information
dkumor committed Mar 19, 2016
1 parent 295f831 commit 1fd8bb1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand Down Expand Up @@ -74,6 +78,8 @@ func main() {

### New Code

Adding let's encrypt support is a matter of setting the tls config:

```go
package main

Expand All @@ -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"},
Expand All @@ -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)
}
```

Expand Down
19 changes: 16 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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)
}

0 comments on commit 1fd8bb1

Please sign in to comment.