forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
169 lines (152 loc) · 4.25 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package http
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/sync/errgroup"
)
const (
// DefaultReadHeaderTimeout defines the default timeout for reading headers.
DefaultReadHeaderTimeout = 2 * time.Second
)
// Config defines the config of an http server.
// TODO: expose via options?
type Config struct {
Acme bool
Port int
Cert string
Key string
AcmeHost string
ReadHeaderTimeout time.Duration
}
// Server is a wrapper around http.Server that exposes different async ListenAndServe methods
// that return corresponding ShutdownFunctions.
type Server struct {
config Config
handler http.Handler
}
// ShutdownFunction defines a function that is called to shutdown the server.
type ShutdownFunction func(context.Context) error
func NewServer(config Config, handler http.Handler) *Server {
if config.ReadHeaderTimeout == 0 {
config.ReadHeaderTimeout = DefaultReadHeaderTimeout
}
return &Server{
config: config,
handler: handler,
}
}
// ListenAndServe initializes a server to respond to HTTP network requests.
func (s *Server) ListenAndServe() (*errgroup.Group, ShutdownFunction) {
if s.config.Acme {
return s.listenAndServeAcme()
} else if s.config.Key != "" {
return s.listenAndServeTLS()
}
return s.listenAndServe()
}
func (s *Server) listenAndServe() (*errgroup.Group, ShutdownFunction) {
var g errgroup.Group
s1 := &http.Server{
Addr: fmt.Sprintf(":%d", s.config.Port),
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
Handler: s.handler,
}
g.Go(func() error {
return s1.ListenAndServe()
})
return &g, s1.Shutdown
}
func (s *Server) listenAndServeTLS() (*errgroup.Group, ShutdownFunction) {
var g errgroup.Group
s1 := &http.Server{
Addr: ":http",
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
Handler: http.HandlerFunc(redirect),
}
s2 := &http.Server{
Addr: ":https",
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
Handler: s.handler,
}
g.Go(func() error {
return s1.ListenAndServe()
})
g.Go(func() error {
return s2.ListenAndServeTLS(
s.config.Cert,
s.config.Key,
)
})
return &g, func(ctx context.Context) error {
var sg errgroup.Group
sg.Go(func() error {
return s1.Shutdown(ctx)
})
sg.Go(func() error {
return s2.Shutdown(ctx)
})
return sg.Wait()
}
}
func (s Server) listenAndServeAcme() (*errgroup.Group, ShutdownFunction) {
var g errgroup.Group
m := &autocert.Manager{
Cache: autocert.DirCache(".cache"),
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(s.config.AcmeHost),
}
s1 := &http.Server{
Addr: ":http",
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
Handler: m.HTTPHandler(nil),
}
s2 := &http.Server{
Addr: ":https",
Handler: s.handler,
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
GetCertificate: m.GetCertificate,
NextProtos: []string{"h2", "http/1.1"},
},
}
g.Go(func() error {
return s1.ListenAndServe()
})
g.Go(func() error {
return s2.ListenAndServeTLS("", "")
})
return &g, func(ctx context.Context) error {
var sg errgroup.Group
sg.Go(func() error {
return s1.Shutdown(ctx)
})
sg.Go(func() error {
return s2.Shutdown(ctx)
})
return sg.Wait()
}
}
func redirect(w http.ResponseWriter, req *http.Request) {
// TODO: in case of reverse-proxy the host might be not the external host.
target := "https://" + req.Host + "/" + strings.TrimPrefix(req.URL.Path, "/")
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}