重构HttpServer
This commit is contained in:
parent
4f9086650e
commit
510af680c4
|
@ -32,6 +32,7 @@ func InitCmd() (*cobra.Command, error) {
|
||||||
//rootCmd.GenZshCompletion(os.Stdout)
|
//rootCmd.GenZshCompletion(os.Stdout)
|
||||||
|
|
||||||
conf.LoadAppConfig()
|
conf.LoadAppConfig()
|
||||||
|
|
||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
|
|
||||||
return rootCmd, err
|
return rootCmd, err
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"acme-mana/src/common"
|
|
||||||
"acme-mana/src/conf"
|
"acme-mana/src/conf"
|
||||||
"acme-mana/src/http"
|
"acme-mana/src/server"
|
||||||
"acme-mana/src/util"
|
"acme-mana/src/util"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -15,7 +14,7 @@ import (
|
||||||
// 打印 配置信息
|
// 打印 配置信息
|
||||||
func confShow(cmd *cobra.Command, args []string) {
|
func confShow(cmd *cobra.Command, args []string) {
|
||||||
tea.Println()
|
tea.Println()
|
||||||
confJson, err := json.MarshalIndent(common.AppConf, "", " ")
|
confJson, err := json.MarshalIndent(conf.Config(), "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("序列化配置信息失败:", err)
|
fmt.Println("序列化配置信息失败:", err)
|
||||||
}
|
}
|
||||||
|
@ -23,7 +22,7 @@ func confShow(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func editServer(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.Host = util.ReadLine("请输入服务监听地址;", server.Host)
|
||||||
server.Port = util.ReadInt("请输入服务监听端口;", strconv.Itoa(server.Port))
|
server.Port = util.ReadInt("请输入服务监听端口;", strconv.Itoa(server.Port))
|
||||||
conf.WriteConfig()
|
conf.WriteConfig()
|
||||||
|
@ -31,10 +30,10 @@ func editServer(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func serverState(cmd *cobra.Command, args []string) {
|
func serverState(cmd *cobra.Command, args []string) {
|
||||||
http.Start()
|
server.Instance.Status()
|
||||||
}
|
}
|
||||||
func startServer(cmd *cobra.Command, args []string) {
|
func startServer(cmd *cobra.Command, args []string) {
|
||||||
http.Start()
|
server.Instance.Start()
|
||||||
}
|
}
|
||||||
func stopServer(cmd *cobra.Command, args []string) {
|
func stopServer(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("stop server")
|
fmt.Println("stop server")
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import "github.com/spf13/cobra"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
var RootCmd *cobra.Command
|
var RootCmd *cobra.Command
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
//
|
||||||
"fmt"
|
//import (
|
||||||
"github.com/gin-gonic/gin"
|
// "fmt"
|
||||||
)
|
// "github.com/gin-gonic/gin"
|
||||||
|
//)
|
||||||
func domainList(context *gin.Context) {
|
//
|
||||||
fmt.Println("domainList")
|
//func domainList(context *gin.Context) {
|
||||||
}
|
// fmt.Println("domainList")
|
||||||
|
//}
|
||||||
func getCert(context *gin.Context) {
|
//
|
||||||
fmt.Println("getCert")
|
//func getCert(context *gin.Context) {
|
||||||
}
|
// fmt.Println("getCert")
|
||||||
|
//}
|
||||||
func refreshCert(context *gin.Context) {
|
//
|
||||||
fmt.Println("refreshCert")
|
//func refreshCert(context *gin.Context) {
|
||||||
}
|
// fmt.Println("refreshCert")
|
||||||
|
//}
|
||||||
|
|
|
@ -1,45 +1,46 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
//
|
||||||
"acme-mana/src/common"
|
//import (
|
||||||
"github.com/gin-gonic/gin"
|
// "acme-mana/src/common"
|
||||||
"github.com/go-acme/lego/v4/log"
|
// "github.com/gin-gonic/gin"
|
||||||
"strconv"
|
// "github.com/go-acme/lego/v4/log"
|
||||||
)
|
// "strconv"
|
||||||
|
//)
|
||||||
var service *gin.Engine
|
//
|
||||||
var isRunning bool
|
//var service *gin.Engine
|
||||||
|
//var isRunning bool
|
||||||
func Start() {
|
//
|
||||||
if isRunning {
|
//func Start() {
|
||||||
return
|
// if isRunning {
|
||||||
}
|
// return
|
||||||
go start()
|
// }
|
||||||
}
|
// go start()
|
||||||
|
//}
|
||||||
func Stop() {
|
//
|
||||||
|
//func Stop() {
|
||||||
}
|
//
|
||||||
|
//}
|
||||||
func Status() bool {
|
//
|
||||||
return isRunning
|
//func Status() bool {
|
||||||
}
|
// return isRunning
|
||||||
|
//}
|
||||||
func start() {
|
//
|
||||||
conf := common.AppConf
|
//func start() {
|
||||||
server := conf.Server
|
// conf := common.AppConf
|
||||||
service := gin.Default()
|
// server := conf.Server
|
||||||
register(service)
|
// service := gin.Default()
|
||||||
isRunning = true
|
// register(service)
|
||||||
err := service.Run(server.Host + ":" + strconv.Itoa(server.Port))
|
// isRunning = true
|
||||||
if err != nil {
|
// err := service.Run(server.Host + ":" + strconv.Itoa(server.Port))
|
||||||
log.Fatal("http server start error \n", err)
|
// if err != nil {
|
||||||
}
|
// log.Fatal("http server start error \n", err)
|
||||||
isRunning = false
|
// }
|
||||||
}
|
// isRunning = false
|
||||||
|
//}
|
||||||
func register(service *gin.Engine) {
|
//
|
||||||
service.GET("/api/v1/refresh", refreshCert)
|
//func register(service *gin.Engine) {
|
||||||
service.GET("/api/v1/cert", getCert)
|
// service.GET("/api/v1/refresh", refreshCert)
|
||||||
service.GET("/api/v1/domain/list", domainList)
|
// service.GET("/api/v1/cert", getCert)
|
||||||
}
|
// service.GET("/api/v1/domain/list", domainList)
|
||||||
|
//}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package src
|
package src
|
||||||
|
|
||||||
import "acme-mana/src/cmd"
|
import (
|
||||||
|
"acme-mana/src/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
func StartProgram() {
|
func StartProgram() {
|
||||||
_, err := cmd.InitCmd()
|
_, err := cmd.InitCmd()
|
||||||
|
@ -8,4 +10,5 @@ func StartProgram() {
|
||||||
panic(err)
|
panic(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,15 +53,15 @@ func (s *HttpServer) Start() {
|
||||||
func (s *HttpServer) Stop() {
|
func (s *HttpServer) Stop() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := httpServer.server.Shutdown(ctx)
|
err := Instance.server.Shutdown(ctx)
|
||||||
s.status = false
|
s.status = false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Server Shutdown:", err)
|
log.Fatalln("Instance Shutdown:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Status() bool {
|
func (s *HttpServer) Status() bool {
|
||||||
return httpServer.status
|
return s.status
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HttpServer) register() {
|
func (s *HttpServer) register() {
|
||||||
|
|
165
src/server/http-server_test.go
Normal file
165
src/server/http-server_test.go
Normal file
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,3 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
var httpServer *HttpServer
|
var Instance *HttpServer
|
||||||
|
|
Loading…
Reference in New Issue
Block a user