diff --git a/cmd/app/main.go b/cmd/app/main.go index c7a6e272..d243023a 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -30,7 +30,9 @@ var ( var ( initializeConfigFunc = config.NewConfig initializeAppFunc = app.Init - runAppFunc = app.Run + runAppFunc = func(cfg *config.Config, log logger.Interface) { + app.Run(cfg, log) + } // NewGeneratorFunc allows tests to inject a fake OpenAPI generator. NewGeneratorFunc = func(u usecase.Usecases, l logger.Interface) interface { GenerateSpec() ([]byte, error) @@ -63,9 +65,11 @@ func main() { log.Fatalf("CIRA certificate setup error: %s", err) } + l := logger.New(cfg.Level) + handleEncryptionKey(cfg) - handleDebugMode(cfg) - runAppFunc(cfg) + handleDebugMode(cfg, l) + runAppFunc(cfg, l) } func setupCIRACertificates(cfg *config.Config, secretsClient security.Storager) error { @@ -86,11 +90,11 @@ func setupCIRACertificates(cfg *config.Config, secretsClient security.Storager) return nil } -func handleDebugMode(cfg *config.Config) { +func handleDebugMode(cfg *config.Config, l logger.Interface) { if os.Getenv("GIN_MODE") != "debug" { go launchBrowser(cfg) } else { - handleOpenAPIGeneration(cfg) + handleOpenAPIGeneration(l) } } @@ -105,8 +109,7 @@ func launchBrowser(cfg *config.Config) { } } -func handleOpenAPIGeneration(cfg *config.Config) { - l := logger.New(cfg.Level) +func handleOpenAPIGeneration(l logger.Interface) { usecases := usecase.Usecases{} // Create OpenAPI generator @@ -127,7 +130,7 @@ func handleOpenAPIGeneration(cfg *config.Config) { return } - log.Println("OpenAPI specification generated at doc/openapi.json") + l.Info("OpenAPI specification generated at doc/openapi.json") } func handleSecretsConfig(cfg *config.Config) (security.Storager, error) { diff --git a/cmd/app/main_test.go b/cmd/app/main_test.go index e675deaa..41ec0d14 100644 --- a/cmd/app/main_test.go +++ b/cmd/app/main_test.go @@ -32,14 +32,14 @@ func TestMainFunction(_ *testing.T) { //nolint:paralleltest // cannot have simul // Mock functions initializeConfigFunc = func() (*config.Config, error) { - return &config.Config{HTTP: config.HTTP{Port: "8080"}, App: config.App{EncryptionKey: "test"}}, nil + return &config.Config{HTTP: config.HTTP{Port: "8080"}, App: config.App{EncryptionKey: "test"}, Log: config.Log{Level: "info"}}, nil } initializeAppFunc = func(_ *config.Config) error { return nil } - runAppFunc = func(_ *config.Config) {} + runAppFunc = func(_ *config.Config, _ logger.Interface) {} // Mock certificate functions loadOrGenerateRootCertFunc = func(_ security.Storager, _ bool, _, _, _ string, _ bool) (*x509.Certificate, *rsa.PrivateKey, error) { @@ -126,7 +126,7 @@ func TestHandleOpenAPIGeneration_Success(t *testing.T) { mockGen.On("GenerateSpec").Return(expectedSpec, nil) mockGen.On("SaveSpec", expectedSpec, "doc/openapi.json").Return(nil) - handleOpenAPIGeneration(&config.Config{Log: config.Log{Level: "info"}}) + handleOpenAPIGeneration(logger.New("info")) mockGen.AssertExpectations(t) } @@ -144,7 +144,7 @@ func TestHandleOpenAPIGeneration_GenerateFails(t *testing.T) { mockGen.On("GenerateSpec").Return([]byte(nil), assert.AnError) - handleOpenAPIGeneration(&config.Config{Log: config.Log{Level: "info"}}) + handleOpenAPIGeneration(logger.New("info")) mockGen.AssertExpectations(t) } diff --git a/internal/app/app.go b/internal/app/app.go index e4112375..21d28f4a 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -32,8 +32,7 @@ var CertStore security.Storager var Version = "DEVELOPMENT" // Run creates objects via constructors. -func Run(cfg *config.Config) { - log := logger.New(cfg.Level) +func Run(cfg *config.Config, log logger.Interface) { cfg.Version = Version log.Info("app - Run - version: " + cfg.Version) // route standard and Gin logs through our JSON logger diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 8a52daf2..11358a93 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -9,6 +9,7 @@ import ( "github.com/device-management-toolkit/console/config" "github.com/device-management-toolkit/console/internal/app" "github.com/device-management-toolkit/console/internal/mocks" + "github.com/device-management-toolkit/console/pkg/logger" ) func TestRun(t *testing.T) { @@ -45,7 +46,7 @@ func TestRun(t *testing.T) { cfg: cfg, expectFunc: func(_ *testing.T) { go func() { - app.Run(cfg) + app.Run(cfg, logger.New("info")) }() }, }, diff --git a/pkg/httpserver/server.go b/pkg/httpserver/server.go index dba57935..45b1cb93 100644 --- a/pkg/httpserver/server.go +++ b/pkg/httpserver/server.go @@ -55,10 +55,6 @@ func New(handler http.Handler, opts ...Option) *Server { server: httpServer, notify: make(chan error, 1), shutdownTimeout: _defaultShutdownTimeout, - useTLS: false, - certFile: "", - keyFile: "", - log: appLogger.New("info"), } // Custom options diff --git a/pkg/httpserver/server_tls_test.go b/pkg/httpserver/server_tls_test.go index 4701e806..de767be7 100644 --- a/pkg/httpserver/server_tls_test.go +++ b/pkg/httpserver/server_tls_test.go @@ -16,6 +16,8 @@ import ( "path/filepath" "testing" "time" + + appLogger "github.com/device-management-toolkit/console/pkg/logger" ) // helper to create a basic cert/key pair on disk. @@ -83,7 +85,7 @@ func TestTLS_SelfSigned_GeneratesAndServes(t *testing.T) { //nolint:paralleltest l := newTestListener(t) - s := New(handler, Listener(l), TLS(true, "", "")) + s := New(handler, Listener(l), TLS(true, "", ""), Logger(appLogger.New("info"))) defer func() { _ = s.Shutdown() }() // ensure server is shutdown; ignore error for cleanup