enable native tls [ci skip]

This commit is contained in:
Brad Rydzewski 2019-04-25 18:07:28 -07:00
parent 1c6d751d50
commit 5028dbb8f0

View file

@ -35,6 +35,13 @@ type Server struct {
// ListenAndServe initializes a server to respond to HTTP network requests. // ListenAndServe initializes a server to respond to HTTP network requests.
func (s Server) ListenAndServe(ctx context.Context) error { func (s Server) ListenAndServe(ctx context.Context) error {
if s.Key != "" {
return s.listenAndServeTLS(ctx)
}
return s.listenAndServe(ctx)
}
func (s Server) listenAndServe(ctx context.Context) error {
var g errgroup.Group var g errgroup.Group
s1 := &http.Server{ s1 := &http.Server{
Addr: s.Addr, Addr: s.Addr,
@ -51,3 +58,33 @@ func (s Server) ListenAndServe(ctx context.Context) error {
}) })
return g.Wait() return g.Wait()
} }
func (s Server) listenAndServeTLS(ctx context.Context) error {
var g errgroup.Group
s1 := &http.Server{
Addr: ":http",
Handler: s.Handler,
}
s2 := &http.Server{
Addr: ":https",
Handler: s.Handler,
}
g.Go(func() error {
return s1.ListenAndServe()
})
g.Go(func() error {
return s2.ListenAndServeTLS(
s.Cert,
s.Key,
)
})
g.Go(func() error {
select {
case <-ctx.Done():
s1.Shutdown(ctx)
s2.Shutdown(ctx)
return nil
}
})
return g.Wait()
}