-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
256 lines (205 loc) · 7.31 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package echo
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"strconv"
"time"
"github.com/americanas-go/ignite/golang.org/x/net.v0/http2/server"
"github.com/americanas-go/log"
"github.com/labstack/echo/v4"
"golang.org/x/net/http2"
)
// Plugin defines an echo server Plugin function to execute.
type Plugin func(context.Context, *Server) error
// Server represents a echo server.
type Server struct {
instance *echo.Echo
options *Options
}
// NewServer returns a new echo server with default options.
func NewServer(ctx context.Context, plugins ...Plugin) *Server {
opt, err := NewOptions()
if err != nil {
panic(err)
}
return NewServerWithOptions(ctx, opt, plugins...)
}
// NewServerWithConfigPath returns a new echo server with options from config path.
func NewServerWithConfigPath(ctx context.Context, path string) (*Server, error) {
options, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewServerWithOptions(ctx, options), nil
}
// NewServerWithOptions returns a new echo server with options.
func NewServerWithOptions(ctx context.Context, opt *Options, plugins ...Plugin) *Server {
instance := echo.New()
instance.HideBanner = opt.HideBanner
instance.DisableHTTP2 = opt.DisableHTTP2
instance.Logger = WrapLogger(log.GetLogger())
srv := &Server{instance: instance, options: opt}
for _, plugin := range plugins {
if err := plugin(ctx, srv); err != nil {
panic(err)
}
}
return srv
}
// Instance returns the wrapped echo server instance.
func (s *Server) Instance() *echo.Echo {
return s.instance
}
// Options returns echo server options.
func (s *Server) Options() *Options {
return s.options
}
// GET registers the handler and middlewares for GET route at path.
func (s *Server) GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.GET(path, h, m...)
}
// POST registers the handler and middlewares for POST route at path.
func (s *Server) POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.POST(path, h, m...)
}
// PUT registers the handler and middlewares for PUT route at path.
func (s *Server) PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.PUT(path, h, m...)
}
// DELETE registers the handler and middlewares for DELETE route at path.
func (s *Server) DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.DELETE(path, h, m...)
}
// OPTIONS registers the handler and middlewares for OPTIONS route at path.
func (s *Server) OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.OPTIONS(path, h, m...)
}
// PATCH registers the handler and middlewares for PATCH route at path.
func (s *Server) PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.PATCH(path, h, m...)
}
// HEAD registers the handler and middlewares for HEAD route at path.
func (s *Server) HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.HEAD(path, h, m...)
}
// CONNECT registers the handler and middlewares for CONNECT route at path.
func (s *Server) CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.CONNECT(path, h, m...)
}
// Group creates a router group with prefix and registers middlewares.
func (s *Server) Group(prefix string, m ...echo.MiddlewareFunc) *echo.Group {
return s.instance.Group(prefix, m...)
}
// Add registers the handler and middlewares for method route at path.
func (s *Server) Add(method string, path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.Add(method, path, h, m...)
}
// Any registers the handler and middlewares for all method routes at path.
func (s *Server) Any(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) []*echo.Route {
return s.instance.Any(path, h, m...)
}
// Use registers middlewares that will run after router.
func (s *Server) Use(middleware ...echo.MiddlewareFunc) {
s.instance.Use(middleware...)
}
// Static registers a static path to server static files at root dir.
func (s *Server) Static(prefix, root string) *echo.Route {
return s.instance.Static(prefix, root)
}
// Match registers the handler and middlewares for method routes at path.
func (s *Server) Match(methods []string, path string, handler echo.HandlerFunc, middleware ...echo.MiddlewareFunc) []*echo.Route {
return s.instance.Match(methods, path, handler, middleware...)
}
// Pre registers middlewares that will run before router.
func (s *Server) Pre(middleware ...echo.MiddlewareFunc) {
s.instance.Pre(middleware...)
}
// File registers a path to a server file.
func (s *Server) File(path, file string, m ...echo.MiddlewareFunc) *echo.Route {
return s.instance.File(path, file, m...)
}
// Serve starts echo server.
func (s *Server) Serve(ctx context.Context) {
logger := log.FromContext(ctx)
address := ":" + strconv.Itoa(s.options.Port)
var err error
if s.options.Protocol == "H2C" {
logger.Infof("starting Echo H2C Server. https://echo.labstack.com/")
var srv *http2.Server
srv, err = server.NewServerWithPath(hc2Root)
err = s.instance.StartH2CServer(address, srv)
} else if s.options.TLS.Enabled {
logger.Infof("starting Echo TLS Server. https://echo.labstack.com/")
if s.options.TLS.Type == "AUTO" {
// err = s.instance.StartAutoTLS(address)
var cert, key []byte
cert, key, err = generateCertificate(s.options.TLS.Auto.Host)
if err == nil {
err = s.instance.StartTLS(address, cert, key)
}
} else {
err = s.instance.StartTLS(address, s.options.TLS.File.Cert, s.options.TLS.File.Key)
}
} else {
logger.Infof("starting Echo Server. https://echo.labstack.com/")
err = s.instance.Start(address)
}
if err != nil {
logger.Error(err)
}
}
// Shutdown stops echo server.
func (s *Server) Shutdown(ctx context.Context) {
logger := log.FromContext(ctx)
err := s.instance.Shutdown(ctx)
if err != nil {
logger.Warn(err)
}
}
// generateCertificate generates a test certificate and private key based on the given host.
func generateCertificate(host string) ([]byte, []byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
cert := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"echo http server"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
SignatureAlgorithm: x509.SHA256WithRSA,
DNSNames: []string{host},
BasicConstraintsValid: true,
IsCA: true,
}
certBytes, err := x509.CreateCertificate(
rand.Reader, cert, cert, &priv.PublicKey, priv,
)
p := pem.EncodeToMemory(
&pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
},
)
b := pem.EncodeToMemory(
&pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
},
)
return b, p, err
}