From 4d98e1f107302132e5bd125cb2877a768b42b061 Mon Sep 17 00:00:00 2001 From: Rune Philosof Date: Thu, 3 Aug 2023 10:14:47 +0200 Subject: [PATCH 1/3] Report problems with listening to ports and exit Example output: ``` {"level":"warn","error":"listen tcp :8000: bind: address already in use","time":"2023-08-03T10:15:40+02:00","caller":"/home/code/woodpecker/cmd/server/server.go:249","message":"Not able to redirect from http to https"} {"level":"fatal","error":"listen tcp 127.0.0.1:8443: bind: address already in use","time":"2023-08-03T10:15:40+02:00","caller":"/home/code/woodpecker/cmd/server/server.go:226"} Process 52495 has exited with status 1 ``` --- cmd/server/server.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 1822a3433f..aa9238d7ec 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -17,6 +17,7 @@ package main import ( "context" "crypto/tls" + "errors" "net" "net/http" "net/http/httputil" @@ -216,10 +217,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) { + log.Fatal().Err(err).Msg("") + } else { + log.Info().Msgf("Server listening on '%s' serving https", server.Config.Server.PortTLS) + } + return err }) // http to https redirect @@ -236,7 +243,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 @@ -258,10 +271,16 @@ 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 }) } @@ -269,7 +288,13 @@ func run(c *cli.Context) error { 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 }) } From 28ff7a314d1361e1c1bb25c4f14a3dff646eb9f5 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Wed, 1 Nov 2023 10:53:14 +0100 Subject: [PATCH 2/3] improve fatal errors --- cmd/server/server.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 8610486d6c..92374dcab8 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -22,7 +22,6 @@ import ( "net/http" "net/http/httputil" "net/url" - "os" "strings" "time" @@ -205,8 +204,6 @@ func run(c *cli.Context) error { ) if !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msg("") - } else { - log.Info().Msgf("Server listening on '%s' serving https", server.Config.Server.PortTLS) } return err }) @@ -227,9 +224,7 @@ func run(c *cli.Context) error { g.Go(func() error { 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) + log.Fatal().Err(err).Msg("Not able to start server to redirect from http to https") } return err }) @@ -245,8 +240,7 @@ func run(c *cli.Context) error { g.Go(func() error { if err := certmagic.HTTPS([]string{address.Host}, handler); err != nil { - log.Err(err).Msg("certmagic does not work") - os.Exit(1) + log.Fatal().Err(err).Msg("certmagic does not work") } return nil }) @@ -258,9 +252,7 @@ func run(c *cli.Context) error { 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")) + log.Fatal().Err(err).Msg("Can't start server") } return err }) @@ -272,9 +264,7 @@ func run(c *cli.Context) error { metricsRouter.GET("/metrics", gin.WrapH(promhttp.Handler())) 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) + log.Fatal().Err(err).Msg("Can't start metrics server") } return err }) From a570a1dd808391b5746ae600174759b39edc4022 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Wed, 1 Nov 2023 11:15:00 +0100 Subject: [PATCH 3/3] improve error messages and use fatal were possible --- cmd/server/server.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index ad037b84d4..8761cceb59 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -84,12 +84,12 @@ func run(c *cli.Context) error { _forge, err := setupForge(c) if err != nil { - log.Fatal().Err(err).Msg("") + log.Fatal().Err(err).Msg("can't setup forge") } _store, err := setupStore(c) if err != nil { - log.Fatal().Err(err).Msg("") + log.Fatal().Err(err).Msg("cant't setup database store") } defer func() { if err := _store.Close(); err != nil { @@ -111,8 +111,7 @@ func run(c *cli.Context) error { g.Go(func() error { lis, err := net.Listen("tcp", c.String("grpc-addr")) if err != nil { - log.Error().Err(err).Msg("failed to listen on grpc-addr") - return err + log.Fatal().Err(err).Msg("failed to listen on grpc-addr") } jwtSecret := c.String("grpc-secret") @@ -145,8 +144,7 @@ func run(c *cli.Context) error { err = grpcServer.Serve(lis) if err != nil { - log.Error().Err(err).Msg("failed to serve grpc server") - return err + log.Fatal().Err(err).Msg("failed to serve grpc server") } return nil }) @@ -157,8 +155,7 @@ func run(c *cli.Context) error { if proxyWebUI == "" { webEngine, err := web.New() if err != nil { - log.Error().Err(err).Msg("failed to create web engine") - return err + log.Fatal().Err(err).Msg("failed to create web engine") } webUIServe = webEngine.ServeHTTP } else { @@ -198,8 +195,8 @@ func run(c *cli.Context) error { c.String("server-cert"), c.String("server-key"), ) - if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Err(err).Msg("") + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("failed to start server with tls") } return err }) @@ -217,8 +214,8 @@ func run(c *cli.Context) error { g.Go(func() error { err := http.ListenAndServe(server.Config.Server.Port, http.HandlerFunc(redirect)) - if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Err(err).Msg("Not able to start server to redirect from http to https") + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("unable to start server to redirect from http to https") } return err }) @@ -245,8 +242,8 @@ func run(c *cli.Context) error { c.String("server-addr"), handler, ) - if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Err(err).Msg("Can't start server") + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("could not start server") } return err }) @@ -257,8 +254,8 @@ func run(c *cli.Context) error { metricsRouter := gin.New() metricsRouter.GET("/metrics", gin.WrapH(promhttp.Handler())) err := http.ListenAndServe(metricsServerAddr, metricsRouter) - if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Err(err).Msg("Can't start metrics server") + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("could not start metrics server") } return err })