From ad99da70fb24ef440c9c4f788001a569bdbcbba7 Mon Sep 17 00:00:00 2001 From: ZhuoQinghui <1302344380@qq.com> Date: Mon, 17 Feb 2025 15:32:18 +0800 Subject: [PATCH] embed --- src/cmd/cmd.go | 9 +++++++++ src/cmd/cmd_handle/server.go | 9 ++++++++- src/conf/func.go | 3 ++- src/server/http-server.go | 7 +++++++ src/server/static/index.html | 10 ++++++++++ src/util/bash.go | 4 ++++ 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/server/static/index.html diff --git a/src/cmd/cmd.go b/src/cmd/cmd.go index fa45b2b..9ac7457 100644 --- a/src/cmd/cmd.go +++ b/src/cmd/cmd.go @@ -96,6 +96,14 @@ func initServerCmd() *cobra.Command { cmd_handle.ServerStart(cmd, args) }, } + runCmd := &cobra.Command{ + Use: "run", + Short: "前台运行服务", + Long: "前台运行服务", + Run: func(cmd *cobra.Command, args []string) { + cmd_handle.RunStart(cmd, args) + }, + } stopCmd := &cobra.Command{ Use: "stop", Short: "停止服务", @@ -108,6 +116,7 @@ func initServerCmd() *cobra.Command { serverCmd.AddCommand(editCmd) serverCmd.AddCommand(stateCmd) serverCmd.AddCommand(startCmd) + serverCmd.AddCommand(runCmd) serverCmd.AddCommand(stopCmd) return serverCmd } diff --git a/src/cmd/cmd_handle/server.go b/src/cmd/cmd_handle/server.go index 2d77523..4868aac 100644 --- a/src/cmd/cmd_handle/server.go +++ b/src/cmd/cmd_handle/server.go @@ -13,6 +13,7 @@ import ( "path" "path/filepath" "strconv" + "strings" "syscall" ) @@ -62,12 +63,18 @@ func ServerStart(command *cobra.Command, args []string) { os.Exit(0) } +func RunStart(command *cobra.Command, args []string) { + daemonStart() +} + func ServerEdit(cmd *cobra.Command, args []string) { s := conf.Config().Server host := util.ReadLine("请输入服务监听地址;", s.Host) port := util.ReadInt("请输入服务监听端口;", strconv.Itoa(s.Port)) key := util.ReadLine("请输入服务通信认证秘钥;", s.Key) - conf.EditServer(host, port, key) + web := util.ReadLine("是否启用WEB服务; Y(es)/N(o);", s.Key) + openWeb := strings.ToUpper(web) == "Y" || strings.ToUpper(web) == "YES" + conf.EditServer(host, port, key, openWeb) fmt.Println("服务监听配置已完成") } diff --git a/src/conf/func.go b/src/conf/func.go index 3328eb4..7a6a060 100644 --- a/src/conf/func.go +++ b/src/conf/func.go @@ -6,10 +6,11 @@ func Config() *AppConfig { return appConf } -func EditServer(host string, port int, key string) *AppConfig { +func EditServer(host string, port int, key string, web bool) *AppConfig { appConf.Server.Host = host appConf.Server.Port = port appConf.Server.Key = key + appConf.Server.Web = web WriteConfig() return appConf } diff --git a/src/server/http-server.go b/src/server/http-server.go index 930bd7d..c8e15b5 100644 --- a/src/server/http-server.go +++ b/src/server/http-server.go @@ -4,6 +4,7 @@ import ( "acme-mana/src/conf" "acme-mana/src/server/http_handler" "context" + "embed" "errors" "github.com/gin-gonic/gin" "log" @@ -12,6 +13,9 @@ import ( "time" ) +//go:embed static/* +var staticFiles embed.FS + type HttpServer struct { server *http.Server status bool @@ -69,6 +73,9 @@ func (s *HttpServer) register() { service.Use(gin.Logger()) service.Use(http_handler.GlobalErrorHandler()) + fs := http.FileServer(http.FS(staticFiles)) + http.Handle("/static/", http.StripPrefix("/static/", fs)) + certHandler := http_handler.CertHandlerInstance certGroup := service.Group("/api/v1/cert", http_handler.AuthMiddleware()) certGroup.GET("/", certHandler.Get) diff --git a/src/server/static/index.html b/src/server/static/index.html new file mode 100644 index 0000000..d5c043e --- /dev/null +++ b/src/server/static/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +123123 + + \ No newline at end of file diff --git a/src/util/bash.go b/src/util/bash.go index 70e8ed0..9285b0a 100644 --- a/src/util/bash.go +++ b/src/util/bash.go @@ -3,6 +3,7 @@ package util import ( "fmt" "github.com/manifoldco/promptui" + "os" "strconv" ) @@ -13,6 +14,9 @@ func ReadLine(label string, defaultContent string) string { Default: defaultContent, } line, err := prompt.Run() + if err.Error() == "^D" || err.Error() == "^C" { + os.Exit(0) + } if err != nil { fmt.Println("输入错误:", err) continue