重构HttpServer

This commit is contained in:
ZhuoQinghui 2024-12-27 15:11:36 +08:00
parent 4f9086650e
commit 510af680c4
9 changed files with 242 additions and 74 deletions

View File

@ -32,6 +32,7 @@ func InitCmd() (*cobra.Command, error) {
//rootCmd.GenZshCompletion(os.Stdout)
conf.LoadAppConfig()
err := rootCmd.Execute()
return rootCmd, err

View File

@ -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")

View File

@ -1,7 +1,5 @@
package common
import (
"github.com/spf13/cobra"
)
import "github.com/spf13/cobra"
var RootCmd *cobra.Command

View File

@ -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")
//}

View File

@ -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)
//}

View File

@ -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
}
}

View File

@ -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() {

View 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)
})
}
}

View File

@ -1,3 +1,3 @@
package server
var httpServer *HttpServer
var Instance *HttpServer