Report problems with listening to ports and exit (#2102)

Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
runephilosof-karnovgroup 2023-11-01 11:44:08 +01:00 committed by GitHub
parent e83357833d
commit 4647e64f13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,11 +16,11 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"errors"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os"
"strings" "strings"
"time" "time"
@ -84,12 +84,12 @@ func run(c *cli.Context) error {
_forge, err := setupForge(c) _forge, err := setupForge(c)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("") log.Fatal().Err(err).Msg("can't setup forge")
} }
_store, err := setupStore(c) _store, err := setupStore(c)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("") log.Fatal().Err(err).Msg("cant't setup database store")
} }
defer func() { defer func() {
if err := _store.Close(); err != nil { if err := _store.Close(); err != nil {
@ -111,8 +111,7 @@ func run(c *cli.Context) error {
g.Go(func() error { g.Go(func() error {
lis, err := net.Listen("tcp", c.String("grpc-addr")) lis, err := net.Listen("tcp", c.String("grpc-addr"))
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to listen on grpc-addr") log.Fatal().Err(err).Msg("failed to listen on grpc-addr")
return err
} }
jwtSecret := c.String("grpc-secret") jwtSecret := c.String("grpc-secret")
@ -145,8 +144,7 @@ func run(c *cli.Context) error {
err = grpcServer.Serve(lis) err = grpcServer.Serve(lis)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to serve grpc server") log.Fatal().Err(err).Msg("failed to serve grpc server")
return err
} }
return nil return nil
}) })
@ -157,8 +155,7 @@ func run(c *cli.Context) error {
if proxyWebUI == "" { if proxyWebUI == "" {
webEngine, err := web.New() webEngine, err := web.New()
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to create web engine") log.Fatal().Err(err).Msg("failed to create web engine")
return err
} }
webUIServe = webEngine.ServeHTTP webUIServe = webEngine.ServeHTTP
} else { } else {
@ -194,10 +191,14 @@ func run(c *cli.Context) error {
NextProtos: []string{"h2", "http/1.1"}, NextProtos: []string{"h2", "http/1.1"},
}, },
} }
return serve.ListenAndServeTLS( err = serve.ListenAndServeTLS(
c.String("server-cert"), c.String("server-cert"),
c.String("server-key"), c.String("server-key"),
) )
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("failed to start server with tls")
}
return err
}) })
// http to https redirect // http to https redirect
@ -212,7 +213,11 @@ func run(c *cli.Context) error {
} }
g.Go(func() 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 err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("unable to start server to redirect from http to https")
}
return err
}) })
} else if c.Bool("lets-encrypt") { } else if c.Bool("lets-encrypt") {
// start the server with lets-encrypt // start the server with lets-encrypt
@ -226,18 +231,21 @@ func run(c *cli.Context) error {
g.Go(func() error { g.Go(func() error {
if err := certmagic.HTTPS([]string{address.Host}, handler); err != nil { if err := certmagic.HTTPS([]string{address.Host}, handler); err != nil {
log.Err(err).Msg("certmagic does not work") log.Fatal().Err(err).Msg("certmagic does not work")
os.Exit(1)
} }
return nil return nil
}) })
} else { } else {
// start the server without tls // start the server without tls
g.Go(func() error { g.Go(func() error {
return http.ListenAndServe( err := http.ListenAndServe(
c.String("server-addr"), c.String("server-addr"),
handler, handler,
) )
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("could not start server")
}
return err
}) })
} }
@ -245,7 +253,11 @@ func run(c *cli.Context) error {
g.Go(func() error { g.Go(func() error {
metricsRouter := gin.New() metricsRouter := gin.New()
metricsRouter.GET("/metrics", gin.WrapH(promhttp.Handler())) metricsRouter.GET("/metrics", gin.WrapH(promhttp.Handler()))
return http.ListenAndServe(metricsServerAddr, metricsRouter) err := http.ListenAndServe(metricsServerAddr, metricsRouter)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("could not start metrics server")
}
return err
}) })
} }