Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"))
}()
},
},
Expand Down
4 changes: 0 additions & 4 deletions pkg/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pkg/httpserver/server_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
Loading