Skip to content

Commit

Permalink
Report problems with listening to ports and exit (#2102)
Browse files Browse the repository at this point in the history
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
Co-authored-by: Anbraten <anton@ju60.de>
  • Loading branch information
3 people authored Nov 1, 2023
1 parent e833578 commit 4647e64
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package main

import (
"crypto/tls"
"errors"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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
})
Expand All @@ -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 {
Expand Down Expand Up @@ -194,10 +191,14 @@ 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 err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("failed to start server with tls")
}
return err
})

// http to https redirect
Expand All @@ -212,7 +213,11 @@ 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 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") {
// start the server with lets-encrypt
Expand All @@ -226,26 +231,33 @@ 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
})
} else {
// start the server without tls
g.Go(func() error {
return http.ListenAndServe(
err := http.ListenAndServe(
c.String("server-addr"),
handler,
)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("could not start server")
}
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 err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("could not start metrics server")
}
return err
})
}

Expand Down

0 comments on commit 4647e64

Please sign in to comment.