Skip to content

Commit

Permalink
server: Cleanup serve
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Siarkowicz <[email protected]>
  • Loading branch information
serathius committed Mar 3, 2023
1 parent a303dce commit 51bf64c
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions server/embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,8 @@ func (sctx *serveCtx) serve(
servElection := v3election.NewElectionServer(v3c)
servLock := v3lock.NewLockServer(v3c)

var tlscfg *tls.Config

// Make sure serversC is closed even if we prematurely exit the function.
defer close(sctx.serversC)
if sctx.secure {
tlscfg, err = tlsinfo.ServerConfig()
if err != nil {
return err
}
}
gs := v3rpc.Server(s, nil, nil, gopts...)
defer func() {
sctx.lg.Warn("stopping grpc server due to error", zap.Error(err))
Expand All @@ -136,8 +128,14 @@ func (sctx *serveCtx) serve(
if sctx.serviceRegister != nil {
sctx.serviceRegister(gs)
}
var srv *http.Server
var gwmux *gw.ServeMux
var tlscfg *tls.Config
if sctx.secure {
tlscfg, err = tlsinfo.ServerConfig()
if err != nil {
return err
}
}
if s.Cfg.EnableGRPCGateway {
var creds credentials.TransportCredentials
if sctx.insecure {
Expand All @@ -158,6 +156,16 @@ func (sctx *serveCtx) serve(
return err
}
}
// TODO: add debug flag; enable logging when debug flag is set
srv := &http.Server{
Handler: createAccessController(sctx.lg, s, sctx.createMux(gwmux, handler)),
ErrorLog: logger, // do not log user error
}
if err := configureHttpServer(srv, s.Cfg); err != nil {
sctx.lg.Error("Configure https server failed", zap.Error(err))
return err
}

listener := sctx.l
if sctx.secure {
listener, err = transport.NewTLSListener(listener, tlsinfo)
Expand All @@ -166,24 +174,10 @@ func (sctx *serveCtx) serve(
}
}
m := cmux.New(listener)
// TODO: add debug flag; enable logging when debug flag is set
httpmux := sctx.createMux(gwmux, handler)
srv = &http.Server{
Handler: createAccessController(sctx.lg, s, httpmux),
ErrorLog: logger, // do not log user error
}
grpcl := m.Match(cmux.HTTP2())
go func() { errHandler(gs.Serve(grpcl)) }()

httpl := m.Match(cmux.HTTP1())
go func() { errHandler(srv.Serve(httpl)) }()
if err := configureHttpServer(srv, s.Cfg); err != nil {
sctx.lg.Error("Configure https server failed", zap.Error(err))
return err
}
go func() { errHandler(srv.Serve(m.Match(cmux.HTTP1()))) }()
go func() { errHandler(gs.Serve(m.Match(cmux.HTTP2()))) }()

sctx.serversC <- &servers{secure: sctx.secure, grpc: gs, http: srv}

msg := "serving client traffic securely"
if sctx.insecure {
msg = "serving client traffic insecurely; this is strongly discouraged!"
Expand Down

0 comments on commit 51bf64c

Please sign in to comment.