Skip to content

Commit 9d48bec

Browse files
committed
feat: let httpserver.New return errors
1 parent f717cf9 commit 9d48bec

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

httpserver/server.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func AlwaysOk(c *gin.Context) {
5353

5454
// New creates a new HTTP server with the given health and ready handlers.
5555
// Pass an initRoutes function to configure routes on this server.
56-
func New(port int, health, ready gin.HandlerFunc, initRoutes func(router *gin.Engine)) *http.Server {
56+
func New(port int, health, ready gin.HandlerFunc, initRoutes func(router *gin.Engine)) (*http.Server, error) {
5757
return NewWithConfig(Config{
5858
Port: port,
5959
Health: health,
@@ -63,8 +63,9 @@ func New(port int, health, ready gin.HandlerFunc, initRoutes func(router *gin.En
6363
}
6464

6565
// NewWithConfig allows a more fine-grained configuration of the HTTP server.
66-
// Use it to e.g. create a server with TLS enabled.
67-
func NewWithConfig(config Config) *http.Server {
66+
// Use it to e.g. create a server with TLS enabled. Returns nil if the server
67+
// could not be created.
68+
func NewWithConfig(config Config) (*http.Server, error) {
6869
router := gin.New()
6970
router.Use(newZeroLogLogger([]string{"/healthz", "/readyz"}), gin.Recovery())
7071

@@ -100,9 +101,15 @@ func NewWithConfig(config Config) *http.Server {
100101
reloadDuration = config.CertCacheDuration
101102
}
102103

104+
log.Debug().Msgf("Using TLS certificate %s and key %s", config.PathTLSCert, config.PathTLSKey)
105+
103106
// Create a certificate handler that is reloading the certificate from disk.
104107
// This is required to support certificate rotation.
105108
cert := newFileBasedCert(config.PathTLSCert, config.PathTLSKey, reloadDuration)
109+
if _, err := cert.GetCertificate(); err != nil {
110+
return nil, err
111+
}
112+
106113
tlsConfig = &tls.Config{
107114
GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
108115
return cert.GetCertificate()
@@ -115,7 +122,7 @@ func NewWithConfig(config Config) *http.Server {
115122
Handler: router,
116123
ErrorLog: golog.New(logging.ErrorLogWriter{}, "", 0),
117124
TLSConfig: tlsConfig,
118-
}
125+
}, nil
119126
}
120127

121128
// Listen starts the given HTTP server and blocks until a stop signal like SIGINT or SIGTERM is received.

0 commit comments

Comments
 (0)