Skip to content

Commit 06294f5

Browse files
authored
Remove httpdown (#325)
* Remove httpdown * Fix ListenAndServe HTTPS when server cert/key present, and HTTP otherwise
1 parent 03dedd4 commit 06294f5

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

auth_server/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require (
1616
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
1717
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
1818
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 // indirect
19-
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2
2019
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
2120
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect
2221
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect

auth_server/main.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"flag"
2223
"math/rand"
@@ -28,7 +29,6 @@ import (
2829
"time"
2930

3031
"github.com/cesanta/glog"
31-
"github.com/facebookgo/httpdown"
3232
"golang.org/x/crypto/acme/autocert"
3333
fsnotify "gopkg.in/fsnotify.v1"
3434

@@ -44,9 +44,8 @@ var (
4444

4545
type RestartableServer struct {
4646
configFile string
47-
hd *httpdown.HTTP
4847
authServer *server.AuthServer
49-
hs httpdown.Server
48+
hs *http.Server
5049
}
5150

5251
func stringToUint16(s string) uint16 {
@@ -57,7 +56,7 @@ func stringToUint16(s string) uint16 {
5756
return uint16(v)
5857
}
5958

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) {
6160
glog.Infof("Config from %s (%d users, %d ACL static entries)", cf, len(c.Users), len(c.ACL))
6261
as, err := server.NewAuthServer(c)
6362
if err != nil {
@@ -129,22 +128,33 @@ func ServeOnce(c *server.Config, cf string, hd *httpdown.HTTP) (*server.AuthServ
129128
glog.Warning("Running without TLS")
130129
tlsConfig = nil
131130
}
131+
132132
hs := &http.Server{
133133
Addr: c.Server.ListenAddress,
134134
Handler: as,
135135
TLSConfig: tlsConfig,
136136
}
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+
}()
142152
glog.Infof("Serving on %s", c.Server.ListenAddress)
143-
return as, s
153+
return as, hs
144154
}
145155

146156
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)
148158
rs.WatchConfig()
149159
}
150160

@@ -185,7 +195,9 @@ func (rs *RestartableServer) WatchConfig() {
185195
case s := <-stopSignals:
186196
signal.Stop(stopSignals)
187197
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+
}
189201
rs.authServer.Stop()
190202
glog.Exitf("Exiting")
191203
}
@@ -200,9 +212,9 @@ func (rs *RestartableServer) MaybeRestart() {
200212
return
201213
}
202214
glog.Infof("Config ok, restarting server")
203-
rs.hs.Stop()
215+
rs.hs.Close()
204216
rs.authServer.Stop()
205-
rs.authServer, rs.hs = ServeOnce(c, rs.configFile, rs.hd)
217+
rs.authServer, rs.hs = ServeOnce(c, rs.configFile)
206218
}
207219

208220
func main() {
@@ -216,13 +228,12 @@ func main() {
216228
if cf == "" {
217229
glog.Exitf("Config file not specified")
218230
}
219-
c, err := server.LoadConfig(cf)
231+
config, err := server.LoadConfig(cf)
220232
if err != nil {
221233
glog.Exitf("Failed to load config: %s", err)
222234
}
223235
rs := RestartableServer{
224236
configFile: cf,
225-
hd: &httpdown.HTTP{},
226237
}
227-
rs.Serve(c)
238+
rs.Serve(config)
228239
}

0 commit comments

Comments
 (0)