108 lines
3.2 KiB
Go
Executable File
108 lines
3.2 KiB
Go
Executable File
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"nos-comptes/handler"
|
|
"nos-comptes/internal/account"
|
|
"nos-comptes/internal/expense"
|
|
ginserver "nos-comptes/internal/ginserver"
|
|
"nos-comptes/internal/storage/dao/postgresql"
|
|
"nos-comptes/internal/user"
|
|
"nos-comptes/internal/utils"
|
|
validatorInternal "nos-comptes/internal/utils/validator"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
config = &handler.Config{}
|
|
cfgFile string
|
|
)
|
|
|
|
const (
|
|
parameterConfigurationFile = "config"
|
|
parameterLogLevel = "loglevel"
|
|
parameterLogFormat = "logformat"
|
|
parameterDBConnectionURI = "dbconnectionuri"
|
|
parameterPort = "port"
|
|
)
|
|
|
|
var (
|
|
defaultLogLevel = logrus.WarnLevel.String()
|
|
defaultLogFormat = utils.LogFormatText
|
|
defaultDBConnectionURI = ""
|
|
defaultPort = 8080
|
|
)
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "nos-comptes",
|
|
Short: "nos-comptes",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
utils.InitLogger(config.LogLevel, config.LogFormat)
|
|
logrus.
|
|
WithField(parameterConfigurationFile, cfgFile).
|
|
WithField(parameterLogLevel, config.LogLevel).
|
|
WithField(parameterLogFormat, config.LogFormat).
|
|
WithField(parameterPort, config.Port).
|
|
WithField(parameterDBConnectionURI, config.DBConnectionURI).
|
|
Warn("Configuration")
|
|
injector := &utils.Injector{}
|
|
ginserver.Setup(injector, config)
|
|
postgresql.Setup(injector, config.DBConnectionURI)
|
|
validatorInternal.Setup(injector)
|
|
user.Setup(injector)
|
|
account.Setup(injector)
|
|
expense.Setup(injector)
|
|
ginserver.Start(injector)
|
|
},
|
|
}
|
|
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, parameterConfigurationFile, "", "Config file. All flags given in command line will override the values from this file.")
|
|
|
|
rootCmd.Flags().String(parameterLogLevel, defaultLogLevel, "Use this flag to set the logging level")
|
|
viper.BindPFlag(parameterLogLevel, rootCmd.Flags().Lookup(parameterLogLevel))
|
|
|
|
rootCmd.Flags().String(parameterLogFormat, defaultLogFormat, "Use this flag to set the logging format")
|
|
viper.BindPFlag(parameterLogFormat, rootCmd.Flags().Lookup(parameterLogFormat))
|
|
|
|
rootCmd.Flags().String(parameterDBConnectionURI, defaultDBConnectionURI, "Use this flag to set the db connection URI")
|
|
viper.BindPFlag(parameterDBConnectionURI, rootCmd.Flags().Lookup(parameterDBConnectionURI))
|
|
|
|
rootCmd.Flags().Int(parameterPort, defaultPort, "Use this flag to set the listening port of the api")
|
|
viper.BindPFlag(parameterPort, rootCmd.Flags().Lookup(parameterPort))
|
|
|
|
}
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
func initConfig() {
|
|
if cfgFile != "" {
|
|
// Use config file from the flag.
|
|
viper.SetConfigFile(cfgFile)
|
|
}
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
// If a config file is found, read it in.
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
}
|
|
|
|
config.LogLevel = viper.GetString(parameterLogLevel)
|
|
config.LogFormat = viper.GetString(parameterLogFormat)
|
|
config.DBConnectionURI = viper.GetString(parameterDBConnectionURI)
|
|
config.Port = viper.GetInt(parameterPort)
|
|
}
|