forked from project-flogo/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
176 lines (134 loc) · 3.02 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
package rest
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
"github.com/project-flogo/core/support/log"
)
const (
httpDefaultAddr = ":http" //todo should this be :8080
httpDefaultTlsAddr = ":https" //todo should this be :8443
httpDefaultReadTimeout = 15 * time.Second
httpDefaultWriteTimeout = 15 * time.Second
)
type Server struct {
running bool
srv *http.Server
tlsEnabled bool
certFile string
keyFile string
}
func NewServer(addr string, handler http.Handler, opts ...func(*Server)) (*Server, error) {
if addr == "" {
addr = httpDefaultAddr
}
srv := &Server{}
srv.srv = &http.Server{
Addr: addr,
Handler: handler,
ReadTimeout:httpDefaultReadTimeout,
WriteTimeout:httpDefaultWriteTimeout,
}
for _, opt := range opts {
opt(srv)
}
if err := srv.validateInit(); err != nil {
return nil, err
}
return srv, nil
}
///////////////////////
// Options
// TLS option enables TLS on the server
func TLS(certFile, keyFile string) func(*Server) {
return func(s *Server) {
s.tlsEnabled = true
s.certFile = certFile
s.keyFile = keyFile
if s.srv.Addr == "" {
s.srv.Addr = httpDefaultTlsAddr
}
}
}
// Timeouts options lets you set the read and write timeouts of the server
func Timeouts(readTimeout, writeTimeout time.Duration) func(*Server) {
return func(s *Server) {
s.srv.ReadTimeout = readTimeout
s.srv.WriteTimeout = writeTimeout
}
}
///////////////////////
// Lifecycle
// Start starts the server
func (s *Server) Start() error {
if s.running {
return nil
}
if err := s.validateStart(); err != nil {
return err
}
fullAddr := s.srv.Addr
if fullAddr[0] == ':' {
fullAddr = "0.0.0.0" + s.srv.Addr
}
s.running = true
if s.tlsEnabled {
go func() {
log.RootLogger().Infof("Rest Trigger listening on https://%s", fullAddr)
if err := s.srv.ListenAndServeTLS(s.certFile, s.keyFile); err != nil {
s.running = false
if err != http.ErrServerClosed {
log.RootLogger().Error(err)
}
}
}()
} else {
go func() {
log.RootLogger().Infof("Rest Trigger listening on http://%s", fullAddr)
if err := s.srv.ListenAndServe(); err != nil {
s.running = false
if err != http.ErrServerClosed {
log.RootLogger().Error(err)
}
}
}()
}
return nil
}
// Stop stops the server
func (s *Server) Stop() error {
if !s.running {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := s.srv.Shutdown(ctx)
return err
}
///////////////////////
// Validation Helpers
func (s *Server) validateStart() error {
//check if port is available
ln, err := net.Listen("tcp", s.srv.Addr)
if err != nil {
return err
}
ln.Close()
return nil
}
func (s *Server) validateInit() error {
if s.tlsEnabled {
// using tls, so validate cert & key
if s.certFile == "" || s.keyFile == "" {
return fmt.Errorf("when TLS is enabled, both cert file and key file must be specified")
}
_, err := tls.LoadX509KeyPair(s.certFile, s.keyFile)
if err != nil {
return err
}
}
return nil
}