package cmd import ( "fmt" "log" "os" "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties" "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/setup" "github.com/gin-gonic/gin" "github.com/spf13/cobra" ) var ( // Verbose defines the parameter verbose. Verbose bool // Version defines the version of the web application. Version string // Debug defines the debug parameter. Debug bool // Port defines the parameter port. Port int // TemplatesDir defines the root directory of template files. TemplatesDir string router *gin.Engine ) var rootCmd = &cobra.Command{ Use: "kalorienmanager", Short: "kalorienmanager", Long: `kalorienmanager`, Run: func(cmd *cobra.Command, args []string) { // Set Gin to production mode if Debug { gin.SetMode(gin.DebugMode) } else { gin.SetMode(gin.ReleaseMode) } log.SetOutput(gin.DefaultWriter) // Set the router as the default one provided by Gin router = gin.Default() // Process the templates at the start so that they don't have to be loaded // from the disk again. This makes serving HTML pages very fast. templatesDir := fmt.Sprintf("%s/**/*", TemplatesDir) log.Printf("load template files from %v", templatesDir) router.LoadHTMLGlob(templatesDir) // Initialize the routes setup.InitializeRoutes(router) // Clean up SetSessionStatus setup.CleanupSessions() // Check if at least one user configured setup.CheckUserList() // Start serving the application server := fmt.Sprintf("127.0.0.1:%d", Port) router.Run(server) }, } // Execute parses the commandline and calls Execute on rootCmd. func Execute(version string) { rootCmd.Version = version properties.SetVersion(version) properties.SetApplication("Kalorienmanager") properties.SetTemplatePrefix("kalorienmanager") if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } func init() { rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "debug modus") rootCmd.PersistentFlags().IntVarP(&Port, "port", "p", 8700, "port number") rootCmd.PersistentFlags().StringVarP(&TemplatesDir, "templates", "t", "templates", "path for template files") }