17
17
package main
18
18
19
19
import (
20
+ "context"
20
21
"crypto/tls"
21
22
"flag"
22
23
"math/rand"
@@ -28,7 +29,6 @@ import (
28
29
"time"
29
30
30
31
"github.com/cesanta/glog"
31
- "github.com/facebookgo/httpdown"
32
32
"golang.org/x/crypto/acme/autocert"
33
33
fsnotify "gopkg.in/fsnotify.v1"
34
34
44
44
45
45
type RestartableServer struct {
46
46
configFile string
47
- hd * httpdown.HTTP
48
47
authServer * server.AuthServer
49
- hs httpdown .Server
48
+ hs * http .Server
50
49
}
51
50
52
51
func stringToUint16 (s string ) uint16 {
@@ -57,7 +56,7 @@ func stringToUint16(s string) uint16 {
57
56
return uint16 (v )
58
57
}
59
58
60
- func ServeOnce (c * server.Config , cf string , hd * httpdown. HTTP ) (* server.AuthServer , httpdown .Server ) {
59
+ func ServeOnce (c * server.Config , cf string ) (* server.AuthServer , * http .Server ) {
61
60
glog .Infof ("Config from %s (%d users, %d ACL static entries)" , cf , len (c .Users ), len (c .ACL ))
62
61
as , err := server .NewAuthServer (c )
63
62
if err != nil {
@@ -129,22 +128,33 @@ func ServeOnce(c *server.Config, cf string, hd *httpdown.HTTP) (*server.AuthServ
129
128
glog .Warning ("Running without TLS" )
130
129
tlsConfig = nil
131
130
}
131
+
132
132
hs := & http.Server {
133
133
Addr : c .Server .ListenAddress ,
134
134
Handler : as ,
135
135
TLSConfig : tlsConfig ,
136
136
}
137
-
138
- s , err := hd .ListenAndServe (hs )
139
- if err != nil {
140
- glog .Exitf ("Failed to set up listener: %s" , err )
141
- }
137
+ go func () {
138
+ if c .Server .CertFile == "" && c .Server .KeyFile == "" {
139
+ if err := hs .ListenAndServe (); err != nil {
140
+ if err == http .ErrServerClosed {
141
+ return
142
+ }
143
+ }
144
+ } else {
145
+ if err := hs .ListenAndServeTLS (c .Server .CertFile , c .Server .KeyFile ); err != nil {
146
+ if err == http .ErrServerClosed {
147
+ return
148
+ }
149
+ }
150
+ }
151
+ }()
142
152
glog .Infof ("Serving on %s" , c .Server .ListenAddress )
143
- return as , s
153
+ return as , hs
144
154
}
145
155
146
156
func (rs * RestartableServer ) Serve (c * server.Config ) {
147
- rs .authServer , rs .hs = ServeOnce (c , rs .configFile , rs . hd )
157
+ rs .authServer , rs .hs = ServeOnce (c , rs .configFile )
148
158
rs .WatchConfig ()
149
159
}
150
160
@@ -185,7 +195,9 @@ func (rs *RestartableServer) WatchConfig() {
185
195
case s := <- stopSignals :
186
196
signal .Stop (stopSignals )
187
197
glog .Infof ("Signal: %s" , s )
188
- rs .hs .Stop ()
198
+ if err := rs .hs .Shutdown (context .Background ()); err != nil {
199
+ glog .Errorf ("HTTP server Shutdown: %v" , err )
200
+ }
189
201
rs .authServer .Stop ()
190
202
glog .Exitf ("Exiting" )
191
203
}
@@ -200,9 +212,9 @@ func (rs *RestartableServer) MaybeRestart() {
200
212
return
201
213
}
202
214
glog .Infof ("Config ok, restarting server" )
203
- rs .hs .Stop ()
215
+ rs .hs .Close ()
204
216
rs .authServer .Stop ()
205
- rs .authServer , rs .hs = ServeOnce (c , rs .configFile , rs . hd )
217
+ rs .authServer , rs .hs = ServeOnce (c , rs .configFile )
206
218
}
207
219
208
220
func main () {
@@ -216,13 +228,12 @@ func main() {
216
228
if cf == "" {
217
229
glog .Exitf ("Config file not specified" )
218
230
}
219
- c , err := server .LoadConfig (cf )
231
+ config , err := server .LoadConfig (cf )
220
232
if err != nil {
221
233
glog .Exitf ("Failed to load config: %s" , err )
222
234
}
223
235
rs := RestartableServer {
224
236
configFile : cf ,
225
- hd : & httpdown.HTTP {},
226
237
}
227
- rs .Serve (c )
238
+ rs .Serve (config )
228
239
}
0 commit comments