diff --git a/automation/server/cmd/commands/server.go b/automation/server/cmd/commands/server.go index c60ae24b75f30042be51192628f1a26c0c3cdb52..50bb6d33e5e5528185dec2a060e70b61a8413578 100644 --- a/automation/server/cmd/commands/server.go +++ b/automation/server/cmd/commands/server.go @@ -1,10 +1,15 @@ package commands import ( + "fmt" + "strings" + "github.com/spf13/cobra" "openeuler.org/PilotGo/PilotGo-plugin-automation/cmd/config/options" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/router" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service" + "openeuler.org/PilotGo/PilotGo-plugin-automation/pkg/utils" ) func NewServerCommand() *cobra.Command { @@ -30,18 +35,25 @@ func Run() error { return err } + ips, err := utils.GetAllHostIPs() + if err != nil { + return fmt.Errorf("获取本机ip地址失败: %v", err) + } else { + global.App.HttpAddr = ips[0].IP + opt.Config.HttpServer.Addr[strings.LastIndex(opt.Config.HttpServer.Addr, ":"):] + } + manager := service.NewServiceManager( &service.LoggerService{Conf: opt.Config.Logopts}, &service.MySQLService{Conf: opt.Config.Mysql}, &service.RedisService{Conf: opt.Config.Redis}, - &service.EtcdService{Conf: opt.Config.Etcd, ServerConf: opt.Config.HttpServer}, + &service.EtcdService{Conf: opt.Config.Etcd, ServerConf: global.App.HttpAddr}, ) if err := manager.InitAll(); err != nil { return err } defer manager.CloseAll() - if err := router.HttpServerInit(opt.Config.HttpServer.Addr).Run(opt.Config.HttpServer.Addr); err != nil { + if err := router.HttpServerInit().Run(global.App.HttpAddr); err != nil { return err } return nil diff --git a/automation/server/internal/router/router.go b/automation/server/internal/router/router.go index 729f4d920bf34dea598c33dbac65435aa87231c0..6546bd16297cb7dfa9e2b3739374ef0262dad2bc 100644 --- a/automation/server/internal/router/router.go +++ b/automation/server/internal/router/router.go @@ -10,17 +10,9 @@ import ( customscripts "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/custom_scripts" scriptlibrary "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service" - "openeuler.org/PilotGo/PilotGo-plugin-automation/pkg/utils" ) -func HttpServerInit(addr string) *gin.Engine { - ips, err := utils.GetAllHostIPs() - if err != nil { - logger.Error("获取本机ip地址失败: %v", err) - } else { - global.App.HttpAddr = ips[0].IP + addr[strings.LastIndex(addr, ":"):] - } - +func HttpServerInit() *gin.Engine { server := initRouters() frontendStaticRouter(server) clientResister(server) diff --git a/automation/server/internal/service/etcd.go b/automation/server/internal/service/etcd.go index 29773f4176f4120f668f90b490a97fba14bde84f..8aaf153cbf9ea7edc05ce93dac1d7c37df9ef8db 100644 --- a/automation/server/internal/service/etcd.go +++ b/automation/server/internal/service/etcd.go @@ -10,7 +10,7 @@ import ( ) type EtcdService struct { - ServerConf *options.HttpServer + ServerConf string Conf *options.Etcd } @@ -21,7 +21,7 @@ func (e *EtcdService) Name() string { func (e *EtcdService) Init(ctx *AppContext) error { sr, err := registry.NewServiceRegistrar(®istry.Options{ Endpoints: e.Conf.Endpoints, - ServiceAddr: e.ServerConf.Addr, + ServiceAddr: e.ServerConf, ServiceName: e.Conf.ServiceName, Version: e.Conf.Version, MenuName: e.Conf.MenuName,