Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove httpdown #325

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion auth_server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 // indirect
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
Expand Down
37 changes: 20 additions & 17 deletions auth_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"context"
"crypto/tls"
"flag"
"math/rand"
Expand All @@ -28,7 +29,6 @@ import (
"time"

"github.com/cesanta/glog"
"github.com/facebookgo/httpdown"
"golang.org/x/crypto/acme/autocert"
fsnotify "gopkg.in/fsnotify.v1"

Expand All @@ -44,9 +44,8 @@ var (

type RestartableServer struct {
configFile string
hd *httpdown.HTTP
authServer *server.AuthServer
hs httpdown.Server
hs *http.Server
}

func stringToUint16(s string) uint16 {
Expand All @@ -57,7 +56,7 @@ func stringToUint16(s string) uint16 {
return uint16(v)
}

func ServeOnce(c *server.Config, cf string, hd *httpdown.HTTP) (*server.AuthServer, httpdown.Server) {
func ServeOnce(c *server.Config, cf string) (*server.AuthServer, *http.Server) {
glog.Infof("Config from %s (%d users, %d ACL static entries)", cf, len(c.Users), len(c.ACL))
as, err := server.NewAuthServer(c)
if err != nil {
Expand Down Expand Up @@ -129,22 +128,25 @@ func ServeOnce(c *server.Config, cf string, hd *httpdown.HTTP) (*server.AuthServ
glog.Warning("Running without TLS")
tlsConfig = nil
}

hs := &http.Server{
Addr: c.Server.ListenAddress,
Handler: as,
TLSConfig: tlsConfig,
}

s, err := hd.ListenAndServe(hs)
if err != nil {
glog.Exitf("Failed to set up listener: %s", err)
}
go func() {
if err := hs.ListenAndServeTLS(c.Server.CertFile, c.Server.KeyFile); err != nil {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
if err == http.ErrServerClosed {
return
}
}
}()
glog.Infof("Serving on %s", c.Server.ListenAddress)
return as, s
return as, hs
}

func (rs *RestartableServer) Serve(c *server.Config) {
rs.authServer, rs.hs = ServeOnce(c, rs.configFile, rs.hd)
rs.authServer, rs.hs = ServeOnce(c, rs.configFile)
rs.WatchConfig()
}

Expand Down Expand Up @@ -185,7 +187,9 @@ func (rs *RestartableServer) WatchConfig() {
case s := <-stopSignals:
signal.Stop(stopSignals)
glog.Infof("Signal: %s", s)
rs.hs.Stop()
if err := rs.hs.Shutdown(context.Background()); err != nil {
glog.Errorf("HTTP server Shutdown: %v", err)
}
rs.authServer.Stop()
glog.Exitf("Exiting")
}
Expand All @@ -200,9 +204,9 @@ func (rs *RestartableServer) MaybeRestart() {
return
}
glog.Infof("Config ok, restarting server")
rs.hs.Stop()
rs.hs.Close()
rs.authServer.Stop()
rs.authServer, rs.hs = ServeOnce(c, rs.configFile, rs.hd)
rs.authServer, rs.hs = ServeOnce(c, rs.configFile)
}

func main() {
Expand All @@ -216,13 +220,12 @@ func main() {
if cf == "" {
glog.Exitf("Config file not specified")
}
c, err := server.LoadConfig(cf)
config, err := server.LoadConfig(cf)
if err != nil {
glog.Exitf("Failed to load config: %s", err)
}
rs := RestartableServer{
configFile: cf,
hd: &httpdown.HTTP{},
}
rs.Serve(c)
rs.Serve(config)
}