From 5028dbb8f0b19ac21c2154c837ccb6d964bcc728 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 25 Apr 2019 18:07:28 -0700 Subject: [PATCH] enable native tls [ci skip] --- server/server_oss.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/server/server_oss.go b/server/server_oss.go index 26e26958..38e71c00 100644 --- a/server/server_oss.go +++ b/server/server_oss.go @@ -35,6 +35,13 @@ type Server struct { // ListenAndServe initializes a server to respond to HTTP network requests. 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 s1 := &http.Server{ Addr: s.Addr, @@ -51,3 +58,33 @@ func (s Server) ListenAndServe(ctx context.Context) error { }) 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() +}