api/internal/cmd/init.go
Peter Kurfer 9236a38be0 Moved endpoint handling to new module
- introduce new endpoints module
- introduce Endpoint and EndpointManager
- introduce new Logging abstraction API to allow proper mocking
- add error return value to Start and Shutdown of endpoints
- add mocks of some internals to allow easier testing
- add generate target to take care of all code generation
2020-04-14 00:15:33 +02:00

62 lines
1.4 KiB
Go

package cmd
import (
"github.com/baez90/inetmock/internal/endpoints"
"github.com/baez90/inetmock/internal/plugins"
"github.com/baez90/inetmock/pkg/logging"
"github.com/baez90/inetmock/pkg/path"
"github.com/spf13/viper"
"go.uber.org/zap"
"os"
"time"
)
var (
appIsInitialized = false
)
func initApp() (err error) {
if appIsInitialized {
return
}
appIsInitialized = true
logging.ConfigureLogging(
logging.ParseLevel(logLevel),
developmentLogs,
map[string]interface{}{"cwd": path.WorkingDirectory()},
)
logger, _ = logging.CreateLogger()
registry := plugins.Registry()
endpointManager = endpoints.NewEndpointManager(logger)
if err = rootCmd.ParseFlags(os.Args); err != nil {
return
}
if err = appConfig.ReadConfig(configFilePath); err != nil {
logger.Error(
"unrecoverable error occurred during reading the config file",
zap.Error(err),
)
return
}
viperInst := viper.GetViper()
pluginDir := viperInst.GetString("plugins-directory")
pluginLoadStartTime := time.Now()
if err = registry.LoadPlugins(pluginDir); err != nil {
logger.Error("Failed to load plugins",
zap.String("pluginsDirectory", pluginDir),
zap.Error(err),
)
}
pluginLoadDuration := time.Since(pluginLoadStartTime)
logger.Info(
"loading plugins completed",
zap.Duration("pluginLoadDuration", pluginLoadDuration),
)
pluginsCmd.AddCommand(registry.PluginCommands()...)
return
}