diff --git a/src/cmd/cmd.go b/src/cmd/cmd.go index 5beb1ab..73d2410 100644 --- a/src/cmd/cmd.go +++ b/src/cmd/cmd.go @@ -32,6 +32,7 @@ func InitCmd() (*cobra.Command, error) { //rootCmd.GenZshCompletion(os.Stdout) conf.LoadAppConfig() + err := rootCmd.Execute() return rootCmd, err diff --git a/src/cmd/handler.go b/src/cmd/handler.go index 8338f4b..0ef8af1 100644 --- a/src/cmd/handler.go +++ b/src/cmd/handler.go @@ -1,9 +1,8 @@ package cmd import ( - "acme-mana/src/common" "acme-mana/src/conf" - "acme-mana/src/http" + "acme-mana/src/server" "acme-mana/src/util" "encoding/json" "fmt" @@ -15,7 +14,7 @@ import ( // 打印 配置信息 func confShow(cmd *cobra.Command, args []string) { tea.Println() - confJson, err := json.MarshalIndent(common.AppConf, "", " ") + confJson, err := json.MarshalIndent(conf.Config(), "", " ") if err != nil { fmt.Println("序列化配置信息失败:", err) } @@ -23,7 +22,7 @@ func confShow(cmd *cobra.Command, args []string) { } func editServer(cmd *cobra.Command, args []string) { - server := common.AppConf.Server + server := conf.Config().Server server.Host = util.ReadLine("请输入服务监听地址;", server.Host) server.Port = util.ReadInt("请输入服务监听端口;", strconv.Itoa(server.Port)) conf.WriteConfig() @@ -31,10 +30,10 @@ func editServer(cmd *cobra.Command, args []string) { } func serverState(cmd *cobra.Command, args []string) { - http.Start() + server.Instance.Status() } func startServer(cmd *cobra.Command, args []string) { - http.Start() + server.Instance.Start() } func stopServer(cmd *cobra.Command, args []string) { fmt.Println("stop server") diff --git a/src/common/variable.go b/src/common/variable.go index 3ece2e6..d77e9ea 100644 --- a/src/common/variable.go +++ b/src/common/variable.go @@ -1,7 +1,5 @@ package common -import ( - "github.com/spf13/cobra" -) +import "github.com/spf13/cobra" var RootCmd *cobra.Command diff --git a/src/http/handler.go b/src/http/handler.go index 695b73f..b446ae8 100644 --- a/src/http/handler.go +++ b/src/http/handler.go @@ -1,18 +1,19 @@ package http -import ( - "fmt" - "github.com/gin-gonic/gin" -) - -func domainList(context *gin.Context) { - fmt.Println("domainList") -} - -func getCert(context *gin.Context) { - fmt.Println("getCert") -} - -func refreshCert(context *gin.Context) { - fmt.Println("refreshCert") -} +// +//import ( +// "fmt" +// "github.com/gin-gonic/gin" +//) +// +//func domainList(context *gin.Context) { +// fmt.Println("domainList") +//} +// +//func getCert(context *gin.Context) { +// fmt.Println("getCert") +//} +// +//func refreshCert(context *gin.Context) { +// fmt.Println("refreshCert") +//} diff --git a/src/http/server.go b/src/http/server.go index 09b8355..2508ac2 100644 --- a/src/http/server.go +++ b/src/http/server.go @@ -1,45 +1,46 @@ package http -import ( - "acme-mana/src/common" - "github.com/gin-gonic/gin" - "github.com/go-acme/lego/v4/log" - "strconv" -) - -var service *gin.Engine -var isRunning bool - -func Start() { - if isRunning { - return - } - go start() -} - -func Stop() { - -} - -func Status() bool { - return isRunning -} - -func start() { - conf := common.AppConf - server := conf.Server - service := gin.Default() - register(service) - isRunning = true - err := service.Run(server.Host + ":" + strconv.Itoa(server.Port)) - if err != nil { - log.Fatal("http server start error \n", err) - } - isRunning = false -} - -func register(service *gin.Engine) { - service.GET("/api/v1/refresh", refreshCert) - service.GET("/api/v1/cert", getCert) - service.GET("/api/v1/domain/list", domainList) -} +// +//import ( +// "acme-mana/src/common" +// "github.com/gin-gonic/gin" +// "github.com/go-acme/lego/v4/log" +// "strconv" +//) +// +//var service *gin.Engine +//var isRunning bool +// +//func Start() { +// if isRunning { +// return +// } +// go start() +//} +// +//func Stop() { +// +//} +// +//func Status() bool { +// return isRunning +//} +// +//func start() { +// conf := common.AppConf +// server := conf.Server +// service := gin.Default() +// register(service) +// isRunning = true +// err := service.Run(server.Host + ":" + strconv.Itoa(server.Port)) +// if err != nil { +// log.Fatal("http server start error \n", err) +// } +// isRunning = false +//} +// +//func register(service *gin.Engine) { +// service.GET("/api/v1/refresh", refreshCert) +// service.GET("/api/v1/cert", getCert) +// service.GET("/api/v1/domain/list", domainList) +//} diff --git a/src/program.go b/src/program.go index 2682a43..394fe26 100644 --- a/src/program.go +++ b/src/program.go @@ -1,6 +1,8 @@ package src -import "acme-mana/src/cmd" +import ( + "acme-mana/src/cmd" +) func StartProgram() { _, err := cmd.InitCmd() @@ -8,4 +10,5 @@ func StartProgram() { panic(err) return } + } diff --git a/src/server/http-server.go b/src/server/http-server.go index 9fbaf40..62019d9 100644 --- a/src/server/http-server.go +++ b/src/server/http-server.go @@ -53,15 +53,15 @@ func (s *HttpServer) Start() { func (s *HttpServer) Stop() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - err := httpServer.server.Shutdown(ctx) + err := Instance.server.Shutdown(ctx) s.status = false if err != nil { - log.Fatalln("Server Shutdown:", err) + log.Fatalln("Instance Shutdown:", err) } } -func Status() bool { - return httpServer.status +func (s *HttpServer) Status() bool { + return s.status } func (s *HttpServer) register() { diff --git a/src/server/http-server_test.go b/src/server/http-server_test.go new file mode 100644 index 0000000..74e2a37 --- /dev/null +++ b/src/server/http-server_test.go @@ -0,0 +1,165 @@ +package server + +import ( + "fmt" + "github.com/gin-gonic/gin" + "net/http" + "os" + "os/signal" + "syscall" + "testing" +) + +func TestHttpServer_InitServer(t *testing.T) { + type fields struct { + server *http.Server + status bool + engine *gin.Engine + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &HttpServer{ + server: tt.fields.server, + status: tt.fields.status, + engine: tt.fields.engine, + } + s.InitServer() + }) + } +} + +func TestHttpServer_Start(t *testing.T) { + type fields struct { + server *http.Server + status bool + engine *gin.Engine + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + { + name: "0.0.0.0", + fields: fields{ + server: nil, + status: false, + engine: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &HttpServer{ + server: tt.fields.server, + status: tt.fields.status, + engine: tt.fields.engine, + } + s.initServer("0.0.0.0", 35541) + s.Start() + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + fmt.Println("Shutting down server...") + }) + } +} + +func TestHttpServer_Stop(t *testing.T) { + type fields struct { + server *http.Server + status bool + engine *gin.Engine + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &HttpServer{ + server: tt.fields.server, + status: tt.fields.status, + engine: tt.fields.engine, + } + s.Stop() + }) + } +} + +func TestHttpServer_register(t *testing.T) { + type fields struct { + server *http.Server + status bool + engine *gin.Engine + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &HttpServer{ + server: tt.fields.server, + status: tt.fields.status, + engine: tt.fields.engine, + } + s.register() + }) + } +} + +func TestStatus(t *testing.T) { + tests := []struct { + name string + want bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Status(); got != tt.want { + t.Errorf("Status() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestHttpServer_initServer(t *testing.T) { + type fields struct { + server *http.Server + status bool + engine *gin.Engine + } + type args struct { + host string + port int + } + tests := []struct { + name string + fields fields + args args + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &HttpServer{ + server: tt.fields.server, + status: tt.fields.status, + engine: tt.fields.engine, + } + s.initServer(tt.args.host, tt.args.port) + }) + } +} diff --git a/src/server/variable.go b/src/server/variable.go index a26af1c..61c5b6b 100644 --- a/src/server/variable.go +++ b/src/server/variable.go @@ -1,3 +1,3 @@ package server -var httpServer *HttpServer +var Instance *HttpServer