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

Report problems with listening to ports and exit #2102

33 changes: 29 additions & 4 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -198,10 +199,16 @@ func run(c *cli.Context) error {
NextProtos: []string{"h2", "http/1.1"},
},
}
return serve.ListenAndServeTLS(
err = serve.ListenAndServeTLS(
c.String("server-cert"),
c.String("server-key"),
)
if !errors.Is(err, http.ErrServerClosed) {
anbraten marked this conversation as resolved.
Show resolved Hide resolved
log.Fatal().Err(err).Msg("")
} else {
log.Info().Msgf("Server listening on '%s' serving https", server.Config.Server.PortTLS)
anbraten marked this conversation as resolved.
Show resolved Hide resolved
}
return err
})

// http to https redirect
Expand All @@ -218,7 +225,13 @@ func run(c *cli.Context) error {
}

g.Go(func() error {
return http.ListenAndServe(server.Config.Server.Port, http.HandlerFunc(redirect))
err := http.ListenAndServe(server.Config.Server.Port, http.HandlerFunc(redirect))
if !errors.Is(err, http.ErrServerClosed) {
log.Warn().Err(err).Msg("Not able to redirect from http to https")
} else {
log.Info().Msgf("Server listening on '%s' to redirect from http to https", server.Config.Server.Port)
}
return err
})
} else if c.Bool("lets-encrypt") {
// start the server with lets-encrypt
Expand All @@ -240,18 +253,30 @@ func run(c *cli.Context) error {
} else {
// start the server without tls
g.Go(func() error {
return http.ListenAndServe(
err := http.ListenAndServe(
c.String("server-addr"),
handler,
)
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("")
} else {
log.Info().Msgf("Server listening on '%s' serving http", c.String("server-addr"))
}
return err
})
}

if metricsServerAddr := c.String("metrics-server-addr"); metricsServerAddr != "" {
g.Go(func() error {
metricsRouter := gin.New()
metricsRouter.GET("/metrics", gin.WrapH(promhttp.Handler()))
return http.ListenAndServe(metricsServerAddr, metricsRouter)
err := http.ListenAndServe(metricsServerAddr, metricsRouter)
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("")
} else {
log.Info().Msgf("Server listening on '%s' serving metrics", metricsServerAddr)
}
return err
})
}

Expand Down