Skip to content

Commit

Permalink
Support TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Jan 28, 2020
1 parent f810300 commit c719016
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![This project is considered experimental](https://img.shields.io/badge/Status-experimental-red.svg)](https://arp242.net/status/experimental)
[![Build Status](https://travis-ci.org/zgoat/zhttp.svg?branch=master)](https://travis-ci.org/zgoat/zhttp)
[![codecov](https://codecov.io/gh/zgoat/zhttp/branch/master/graph/badge.svg)](https://codecov.io/gh/zgoat/zhttp)
[![GoDoc](https://godoc.org/github.com/zgoat/zhttp?status.svg)](https://godoc.org/github.com/zgoat/zhttp)
[![GoDoc](https://godoc.org/github.com/zgoat/zhttp?status.svg)](https://pkg.go.dev/github.com/zgoat/zhttp)

Martin's HTTP package: It's not a "framework", but just a collection of
functions for building HTTP services.
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module zgo.at/zhttp
go 1.12

require (
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-chi/chi v4.0.3+incompatible
github.com/monoculum/formam v0.0.0-20191229172733-952f0766a724
github.com/pkg/errors v0.8.1
github.com/pkg/errors v0.9.1
github.com/teamwork/guru v1.0.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
zgo.at/zlog v1.0.7
zgo.at/ztest v1.0.0
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
zgo.at/utils v1.3.1 // indirect
zgo.at/zlog v1.0.9
zgo.at/ztest v1.0.1
)
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/chi v4.0.3+incompatible h1:gakN3pDJnzZN5jqFV2TEdF66rTfKeITyR8qu6ekICEY=
github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/monoculum/formam v0.0.0-20191229172733-952f0766a724 h1:qlTmDrFZLQIGXnd1JE58dqyLnKyIJjR9WBeDQcT3O8w=
github.com/monoculum/formam v0.0.0-20191229172733-952f0766a724/go.mod h1:JKa2av1XVkGjhxdLS59nDoXa2JpmIHpnURWNbzCtXtc=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/teamwork/guru v1.0.0 h1:iOnJwnmfZnwp6ny7+G7/h8OtLjuB9XxmSh5ikwaljzQ=
github.com/teamwork/guru v1.0.0/go.mod h1:1DW23kFX0aXn1kRSuQAY+Byk74h1kZHvhJL72qBUrfU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
zgo.at/utils v1.2.0 h1:Ohv+XfqyoiSdl9lCbyiDZ5E4YgEXWxxE4Y/holHDiYo=
zgo.at/utils v1.2.0/go.mod h1:7acz1AfMZAOOC1wTi6QbYFie+eA0LRSHE38TwLZsFcA=
zgo.at/utils v1.3.1 h1:e5/HyvCEuASSrpipLY0QhWMjDIYr+zU24wRJzejX+es=
zgo.at/utils v1.3.1/go.mod h1:7acz1AfMZAOOC1wTi6QbYFie+eA0LRSHE38TwLZsFcA=
zgo.at/zlog v1.0.7 h1:Ppjb9VOBheqENQ28TWsOddG2qIVO4nGb0b/TPh/5sXc=
zgo.at/zlog v1.0.7/go.mod h1:0RldP/sQzWwOCPsJogZcxnJneFt56T1zfDtIN/LcYec=
zgo.at/zlog v1.0.9 h1:JRggYfHH9eF5daLW6cm9uitibTlKP85AdlK+q9x5vZ4=
zgo.at/zlog v1.0.9/go.mod h1:0RldP/sQzWwOCPsJogZcxnJneFt56T1zfDtIN/LcYec=
zgo.at/ztest v1.0.0 h1:MKJXDboceOH2bFKaWN8GJ0bUgfJgx4pp/HWeCTOCJbA=
zgo.at/ztest v1.0.0/go.mod h1:iTxcAVkHLq73Qnd+a8rlwc6Ayrk8sbnJPsGeapG/y0Q=
zgo.at/ztest v1.0.1 h1:2DWVZVmWAwOX8pq8sJQoOPtAHZ0Y099XVc9YkOmpnuc=
zgo.at/ztest v1.0.1/go.mod h1:iTxcAVkHLq73Qnd+a8rlwc6Ayrk8sbnJPsGeapG/y0Q=
16 changes: 16 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,19 @@ func MountACME(r chi.Router, certdir string) {
w.Write(data)
})
}

// HandlerRedirect redirects all HTTP requests to HTTPS.
func HandlerRedirectHTTP(port string) http.HandlerFunc {
if port == "" {
port = "443"
}
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Encode()
if len(q) > 0 {
q = "?" + q
}
w.Header().Set("Location", fmt.Sprintf("https://%s:%s%s%s", r.Host, port, r.URL.Path, q))
w.WriteHeader(301)
}

}
37 changes: 33 additions & 4 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package zhttp

import (
"context"
"net"
"net/http"
"os"
"os/signal"
"strings"

"zgo.at/zlog"
)

// Serve a HTTP server with graceful shutdown.
func Serve(server *http.Server, wait func()) {
//
// If tls is given, port 80 will be redirected.
func Serve(server *http.Server, tls string, wait func()) {
consClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
Expand All @@ -30,11 +34,36 @@ func Serve(server *http.Server, wait func()) {
close(consClosed)
}()

// Start HTTP servers.
var host, port string

// Redirect port 80 to TLS.
if tls != "" {
var err error
host, port, err = net.SplitHostPort(server.Addr)
if err != nil {
host = server.Addr
port = "443"
}

go func() {
err = http.ListenAndServe(host+":80", HandlerRedirectHTTP(port))
if err != nil && err != http.ErrServerClosed {
zlog.Errorf("zhttp.Serve: ListenAndServe redirect: %s", err)
}
}()
}

// Start HTTP server.
go func() {
err := server.ListenAndServe()
var err error
if tls == "" {
err = server.ListenAndServe()
} else {
cert_key := strings.SplitN(tls, ":", 2)
err = server.ListenAndServeTLS(cert_key[0], cert_key[1])
}
if err != nil && err != http.ErrServerClosed {
zlog.Errorf("http.ListenAndServe: %s", err)
zlog.Errorf("zhttp.Serve: ListenAndServe: %s", err)
}
}()

Expand Down

0 comments on commit c719016

Please sign in to comment.