123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package middleware
- import (
- "fmt"
- "io/fs"
- "path/filepath"
- "xg_dba/api"
- "xg_dba/internal/middleware/utils"
- "xg_dba/internal/models"
- "github.com/spf13/viper"
- )
- type ServerDbCache struct {
- Cache map[string]models.Cache
- }
- func InitCache(folderPath string) (*ServerDbCache, error) {
- serverDb := ServerDbCache{}
- serverDb.Cache = make(map[string]models.Cache)
- err := filepath.WalkDir(folderPath, func(path string, d fs.DirEntry, err error) error {
- if err != nil {
- return err
- }
- if d.IsDir() {
-
- cacheTemp := models.Cache{}
-
- connects := make(map[string]models.ConnectInfo)
-
- systemMeta := make(map[string]models.SystemMetaInfo)
-
- listenFilePath := filepath.Join(path, "listen.toml")
- if utils.FileExists(listenFilePath) {
-
- parseListen(listenFilePath, connects)
- cacheTemp.Connect = connects
- }
-
- systemMeta, err = utils.GetLatestOsInfoFiles(path)
- if err != nil {
- return fmt.Errorf("获取目录中最新的 os_info.toml 文件时出错: %v", err)
- }
-
-
-
-
-
-
-
-
- if cacheTemp.Connect != nil {
-
- folderName := filepath.Base(path)
- cacheTemp.FileInfo.BaseFileName = folderName
-
- if systemMeta != nil {
- cacheTemp.System = systemMeta
- }
-
- serverDb.Cache[folderName] = cacheTemp
- }
- }
- return nil
- })
- if err != nil {
- return nil, fmt.Errorf("遍历路径 %q 时出错: %v", folderPath, err)
- }
-
-
-
-
- return &serverDb, nil
- }
- func parseListen(listenFilePath string, config map[string]models.ConnectInfo) {
-
- vp := viper.New()
- vp.SetConfigFile(listenFilePath)
- vp.SetConfigType("toml")
- if err := vp.ReadInConfig(); err != nil {
- panic(fmt.Errorf("parseListen fatal error reading config file: %w", err))
- }
-
- serverDBs := vp.GetStringMap("server_db")
- for key := range serverDBs {
- dbConfig := vp.Sub(fmt.Sprintf("server_db.%s", key))
- if dbConfig == nil {
- continue
- }
-
- config[key] = models.ConnectInfo{
- Ssh: models.SshInfo{
- Username: dbConfig.GetString("ssh_username"),
- Password: dbConfig.GetString("ssh_password"),
- Host: dbConfig.GetString("ssh_ip"),
- Port: dbConfig.GetString("ssh_port"),
- },
- Db: models.DbInfo{
- User: dbConfig.GetString("db_user"),
- Password: dbConfig.GetString("db_password"),
- Database: dbConfig.GetString("db_database"),
- Port: dbConfig.GetString("db_port"),
- },
- }
- }
- }
- func (cache *ServerDbCache) RefreshCache() error {
- var err error
- cache, err = InitCache("./config")
- if err != nil {
- return err
- }
- return nil
- }
- func (cache *ServerDbCache) LoadCache(folderPath string) error {
- return nil
- }
- func (cache *ServerDbCache) GetConnectCache() (map[string]map[string]api.ConnectInfoResopnse, error) {
-
-
-
-
-
- allConnects := make(map[string]map[string]api.ConnectInfoResopnse)
- for group, cacheV := range cache.Cache {
- TempMap := make(map[string]api.ConnectInfoResopnse)
- connInfoMap := make(map[string]api.ConnectInfo)
- for key, cache := range cacheV.Connect {
-
- connInfoMap[key] = api.SetConnectInfo(cache.Ssh.Username, cache.Ssh.Password, cache.Ssh.Host, cache.Ssh.Port, cache.Db.User, cache.Db.Password, cache.Db.Database, cache.Db.Port)
- }
-
- connRes := api.ConnectInfoResopnse{
- ConnInfo: connInfoMap,
- BaseFileName: cacheV.FileInfo.BaseFileName,
- }
- TempMap[group] = connRes
-
-
- allConnects[group] = TempMap
- }
- return allConnects, nil
- }
- func (cache *ServerDbCache) GetSingleSystemInfoCache(BaseFileName, ip string) (models.SystemMetaInfo, error) {
- info := cache.Cache[BaseFileName].System[ip]
- fmt.Println("单个系统的信息info:", info)
- return info, nil
- }
|