-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserver.go
75 lines (68 loc) · 1.95 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2022 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.
// Package server provides an HTTPS server with support for TLS
// and graceful shutdown.
package server
import (
"context"
"crypto/tls"
"net/http"
"time"
"github.com/docker/go-connections/tlsconfig"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
// A Server defines parameters for running an HTTPS/TLS server.
type Server struct {
Addr string // TCP address to listen on
Handler http.Handler
CAFile string // CA certificate file
CertFile string // Server certificate PEM file
KeyFile string // Server key PEM file
ClientCertFile string // Trusted client certificate PEM file for client authentication
Insecure bool // run without TLS
}
// Start initializes a server to respond to HTTPS/TLS network requests.
func (s *Server) Start(ctx context.Context) error {
var tlsConfig *tls.Config
if s.Insecure {
tlsConfig = nil
logrus.Warnln("RUNNING IN INSECURE MODE")
logrus.Warnln("RUNNING IN INSECURE MODE")
logrus.Warnln("RUNNING IN INSECURE MODE")
} else {
tlsOptions := tlsconfig.Options{
CAFile: s.CAFile,
CertFile: s.CertFile,
KeyFile: s.KeyFile,
ExclusiveRootPools: true,
}
tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert
var err error
tlsConfig, err = tlsconfig.Server(tlsOptions)
if err != nil {
return err
}
tlsConfig.MinVersion = tls.VersionTLS13
}
srv := &http.Server{
Addr: s.Addr,
Handler: s.Handler,
TLSConfig: tlsConfig,
ReadHeaderTimeout: 10 * time.Second,
}
var g errgroup.Group
g.Go(func() error {
if s.Insecure {
return srv.ListenAndServe()
}
return srv.ListenAndServeTLS(s.CertFile, s.KeyFile)
})
g.Go(func() error {
<-ctx.Done()
srv.Shutdown(ctx) //nolint: errcheck
return nil
})
return g.Wait()
}