From 80641ed2ad0c02aadb61779c577b2d6e0a8f0fa8 Mon Sep 17 00:00:00 2001 From: Dorvan Date: Tue, 23 Jan 2024 15:50:40 +0800 Subject: [PATCH] refactor: use Init to init config router and logger --- cmd/server.go | 12 ++++++------ cmd/version.go | 4 ---- configs/config.go | 3 +-- configs/server.go | 3 ++- internal/pkg/logger/logger.go | 2 +- internal/router/router.go | 2 +- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index b69159c..1fdb807 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -19,14 +19,12 @@ import ( ) var ( - // host string - // port uint + host string + port int ServerStartCmd = &cobra.Command{ Use: "server", Short: `Start the server`, Run: func(cmd *cobra.Command, args []string) { - // Do Stuff Here - // log.Println("Hello World!") start() }, } @@ -35,19 +33,21 @@ var ( func init() { // ServerStartCmd.Flags().StringVarP(&host, "host", "H", "127.0.0.1", "HTTP server host") // server host - // ServerStartCmd.Flags().UintVarP(&port, "port", "p", 8080, "HTTP server port") // server port - rootCmd.AddCommand(ServerStartCmd) // add server start command + // ServerStartCmd.Flags().IntVarP(&port, "port", "p", 8080, "Http server port") // server port } func start() { // init router + router.Init() r := router.Router // init logger + logger.Init() logger := logger.LogrusLogger // load env config + configs.Init() EnvConfig := configs.EnvConfig // connect database diff --git a/cmd/version.go b/cmd/version.go index bfc43cb..eadfaa5 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -15,7 +15,3 @@ var VersionCmd = &cobra.Command{ fmt.Printf(`Go-Gin-Boilerplate version: %v`, "v0.0.1") }, } - -func init() { - rootCmd.AddCommand(VersionCmd) // add version command -} diff --git a/configs/config.go b/configs/config.go index aa16d64..f757137 100644 --- a/configs/config.go +++ b/configs/config.go @@ -40,7 +40,6 @@ func LoadConfig() *Config { return config } -func init() { +func Init() { EnvConfig = LoadConfig() - fmt.Printf("👻 EnvConfig: %+v\n", EnvConfig) } diff --git a/configs/server.go b/configs/server.go index e337ea9..024d093 100644 --- a/configs/server.go +++ b/configs/server.go @@ -1,5 +1,6 @@ package configs type Server struct { - Port string + GinMode string + Port string } diff --git a/internal/pkg/logger/logger.go b/internal/pkg/logger/logger.go index 288d767..7787c1f 100644 --- a/internal/pkg/logger/logger.go +++ b/internal/pkg/logger/logger.go @@ -12,7 +12,7 @@ var ( LogrusLogger *logrus.Logger ) -func init() { +func Init() { LogrusLogger = InitLogrusLogger() // ZapLogger = InitZapLogger() diff --git a/internal/router/router.go b/internal/router/router.go index 4597f9f..edfba18 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -12,7 +12,7 @@ import ( var Router *gin.Engine -func init() { +func Init() { Router = gin.Default() // Global middlewares