From fedf632f2e96339a57639643ca52ee60e0badd90 Mon Sep 17 00:00:00 2001 From: wubijie Date: Fri, 1 Dec 2023 16:30:26 +0800 Subject: [PATCH] add plugin_config init message --- ...nfig.yml.templete => config.yaml.templete} | 2 - configmanage/server/config/config.go | 22 +++------ configmanage/server/db/db.go | 14 +++--- configmanage/server/global/global.go | 9 ++++ .../server/global/plugin_configmanage.go | 21 ++++++++ configmanage/server/main.go | 48 ++++++++++++++++++- configmanage/server/router/router.go | 23 +++++++++ 7 files changed, 114 insertions(+), 25 deletions(-) rename configmanage/server/{config.yml.templete => config.yaml.templete} (91%) create mode 100644 configmanage/server/global/global.go create mode 100644 configmanage/server/global/plugin_configmanage.go create mode 100644 configmanage/server/router/router.go diff --git a/configmanage/server/config.yml.templete b/configmanage/server/config.yaml.templete similarity index 91% rename from configmanage/server/config.yml.templete rename to configmanage/server/config.yaml.templete index 8cb9f5a7..099958b1 100644 --- a/configmanage/server/config.yml.templete +++ b/configmanage/server/config.yaml.templete @@ -3,8 +3,6 @@ config_plugin: plugin_type: "iframe" # iframe micro-app http_server: addr: "localhost:8099" -pilotgo_server: - addr: "0.0.0.0:8888" log: level: debug driver: file #可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 diff --git a/configmanage/server/config/config.go b/configmanage/server/config/config.go index 7ae85147..6c607aa4 100644 --- a/configmanage/server/config/config.go +++ b/configmanage/server/config/config.go @@ -17,10 +17,6 @@ type HttpServer struct { Addr string `yaml:"addr"` } -type PilotGoServer struct { - Addr string `yaml:"addr"` -} - type MysqlDBInfo struct { HostName string `yaml:"host"` Port int `yaml:"port"` @@ -30,23 +26,17 @@ type MysqlDBInfo struct { } type ServerConfig struct { - ConfigPlugin *ConfigPlugin `yaml:"config_plugin"` - HttpServer *HttpServer `yaml:"http_server"` - PilotGoServer *PilotGoServer `yaml:"pilotgo_server"` - Logopts *logger.LogOpts `yaml:"log"` - Mysql *MysqlDBInfo `yaml:"mysql"` + ConfigPlugin *ConfigPlugin `yaml:"config_plugin"` + HttpServer *HttpServer `yaml:"http_server"` + Logopts *logger.LogOpts `yaml:"log"` + Mysql *MysqlDBInfo `yaml:"mysql"` } -const config_file = "./config.yml" - var global_config ServerConfig -func Init() { +func Init(config_file string) error { err := readConfig(config_file, &global_config) - if err != nil { - fmt.Printf("%v", err.Error()) - os.Exit(-1) - } + return err } func Config() *ServerConfig { diff --git a/configmanage/server/db/db.go b/configmanage/server/db/db.go index 41ad9677..81022515 100644 --- a/configmanage/server/db/db.go +++ b/configmanage/server/db/db.go @@ -61,22 +61,24 @@ func MysqldbInit(conf *config.MysqlDBInfo) error { db.SetMaxIdleConns(10) db.SetMaxOpenConns(100) + + //创建数据库表信息 MySQL().AutoMigrate() return nil } -func ensureDatabase(conf *MysqlManager) error { +func ensureDatabase(m *MysqlManager) error { Url := fmt.Sprintf("%s:%s@(%s:%d)/?charset=utf8mb4&parseTime=true", - conf.userName, - conf.passWord, - conf.ip, - conf.port) + m.userName, + m.passWord, + m.ip, + m.port) db, err := gorm.Open(mysql.Open(Url)) if err != nil { return err } - creatDataBase := "CREATE DATABASE IF NOT EXISTS " + conf.ip + " DEFAULT CHARSET utf8 COLLATE utf8_general_ci" + creatDataBase := "CREATE DATABASE IF NOT EXISTS " + m.dbName + " DEFAULT CHARSET utf8 COLLATE utf8_general_ci" db.Exec(creatDataBase) d, err := db.DB() diff --git a/configmanage/server/global/global.go b/configmanage/server/global/global.go new file mode 100644 index 00000000..696e9f43 --- /dev/null +++ b/configmanage/server/global/global.go @@ -0,0 +1,9 @@ +package global + +import ( + "gitee.com/openeuler/PilotGo/sdk/plugin/client" +) + +var ( + GlobalClient *client.Client +) diff --git a/configmanage/server/global/plugin_configmanage.go b/configmanage/server/global/plugin_configmanage.go new file mode 100644 index 00000000..830944c6 --- /dev/null +++ b/configmanage/server/global/plugin_configmanage.go @@ -0,0 +1,21 @@ +package global + +import ( + "gitee.com/openeuler/PilotGo/sdk/plugin/client" + "openeuler.org/PilotGo/configmanage-plugin/config" +) + +const Version = "1.0.1" + +func Init(plugin *config.ConfigPlugin) *client.PluginInfo { + PluginInfo := client.PluginInfo{ + Name: "configmanage", + Version: Version, + Description: "configmanage-plugin", + Author: "wubijie", + Email: "wubijie@kylinos.cn", + Url: plugin.URL, + PluginType: "iframe", + } + return &PluginInfo +} diff --git a/configmanage/server/main.go b/configmanage/server/main.go index 38dd16da..0159e37f 100644 --- a/configmanage/server/main.go +++ b/configmanage/server/main.go @@ -1,3 +1,49 @@ package main -func main() {} +import ( + "flag" + "fmt" + "os" + + "gitee.com/openeuler/PilotGo/sdk/logger" + "gitee.com/openeuler/PilotGo/sdk/plugin/client" + "openeuler.org/PilotGo/configmanage-plugin/config" + "openeuler.org/PilotGo/configmanage-plugin/db" + "openeuler.org/PilotGo/configmanage-plugin/global" + "openeuler.org/PilotGo/configmanage-plugin/router" +) + +var config_file string + +func main() { + fmt.Println("hello plugin-config") + + flag.StringVar(&config_file, "conf", "./config.yaml", "plugin-config configuration file") + flag.Parse() + err := config.Init(config_file) + if err != nil { + fmt.Println("failed to load configure, exit..", err) + os.Exit(-1) + } + + if err := logger.Init(config.Config().Logopts); err != nil { + fmt.Printf("logger init failed, please check the config file: %s", err) + os.Exit(-1) + } + logger.Info("Thanks to choose PilotGo!") + + // mysql db初始化 + if err := db.MysqldbInit(config.Config().Mysql); err != nil { + logger.Error("mysql db init failed, please check again: %s", err) + os.Exit(-1) + } + + server := router.InitRouter() + global.GlobalClient = client.DefaultClient(global.Init(config.Config().ConfigPlugin)) + + go router.RegisterAPIs(server) + if err := server.Run(config.Config().HttpServer.Addr); err != nil { + logger.Error("failed to run server: %s", err) + os.Exit(-1) + } +} diff --git a/configmanage/server/router/router.go b/configmanage/server/router/router.go new file mode 100644 index 00000000..dbf6b0db --- /dev/null +++ b/configmanage/server/router/router.go @@ -0,0 +1,23 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/configmanage-plugin/global" +) + +// gin.egnine充当server的角色 +func InitRouter() *gin.Engine { + gin.SetMode(gin.ReleaseMode) + router := gin.New() + router.Use(gin.Recovery()) + return router +} + +func RegisterAPIs(router *gin.Engine) { + //输出插件初始化的信息 + global.GlobalClient.RegisterHandlers(router) + pg := router.Group("/plugin/" + global.GlobalClient.PluginInfo.Name) + { + //pg.POST("/install") + } +} -- Gitee