mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	feature: 实现数据库实例管理
This commit is contained in:
		
							
								
								
									
										14
									
								
								server/internal/db/api/form/instance.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								server/internal/db/api/form/instance.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
package form
 | 
			
		||||
 | 
			
		||||
type InstanceForm struct {
 | 
			
		||||
	Id                 uint64 `json:"id"`
 | 
			
		||||
	Name               string `binding:"required" json:"name"`
 | 
			
		||||
	Type               string `binding:"required" json:"type"` // 类型,mysql oracle等
 | 
			
		||||
	Host               string `binding:"required" json:"host"`
 | 
			
		||||
	Port               int    `binding:"required" json:"port"`
 | 
			
		||||
	Username           string `binding:"required" json:"username"`
 | 
			
		||||
	Password           string `json:"password"`
 | 
			
		||||
	Params             string `json:"params"`
 | 
			
		||||
	Remark             string `json:"remark"`
 | 
			
		||||
	SshTunnelMachineId int    `json:"sshTunnelMachineId"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										71
									
								
								server/internal/db/api/instance.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								server/internal/db/api/instance.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
			
		||||
package api
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"mayfly-go/internal/db/api/form"
 | 
			
		||||
	"mayfly-go/internal/db/api/vo"
 | 
			
		||||
	"mayfly-go/internal/db/application"
 | 
			
		||||
	"mayfly-go/internal/db/domain/entity"
 | 
			
		||||
	msgapp "mayfly-go/internal/msg/application"
 | 
			
		||||
	"mayfly-go/pkg/biz"
 | 
			
		||||
	"mayfly-go/pkg/ginx"
 | 
			
		||||
	"mayfly-go/pkg/req"
 | 
			
		||||
	"mayfly-go/pkg/utils/cryptox"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Instance struct {
 | 
			
		||||
	InstanceApp application.Instance
 | 
			
		||||
	MsgApp      msgapp.Msg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// @router /api/instances [get]
 | 
			
		||||
func (d *Instance) Instances(rc *req.Ctx) {
 | 
			
		||||
	queryCond, page := ginx.BindQueryAndPage[*entity.InstanceQuery](rc.GinCtx, new(entity.InstanceQuery))
 | 
			
		||||
	rc.ResData = d.InstanceApp.GetPageList(queryCond, page, new([]vo.SelectDataInstanceVO))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *Instance) SaveInstance(rc *req.Ctx) {
 | 
			
		||||
	form := &form.InstanceForm{}
 | 
			
		||||
	instance := ginx.BindJsonAndCopyTo[*entity.Instance](rc.GinCtx, form, new(entity.Instance))
 | 
			
		||||
 | 
			
		||||
	// 密码解密,并使用解密后的赋值
 | 
			
		||||
	originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
 | 
			
		||||
	biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
			
		||||
	instance.Password = originPwd
 | 
			
		||||
 | 
			
		||||
	// 密码脱敏记录日志
 | 
			
		||||
	form.Password = "****"
 | 
			
		||||
	rc.ReqParam = form
 | 
			
		||||
 | 
			
		||||
	instance.SetBaseInfo(rc.LoginAccount)
 | 
			
		||||
	d.InstanceApp.Save(instance)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
			
		||||
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
 | 
			
		||||
	dbId := GetInstanceId(rc.GinCtx)
 | 
			
		||||
	dbEntity := d.InstanceApp.GetById(dbId, "Password")
 | 
			
		||||
	dbEntity.PwdDecrypt()
 | 
			
		||||
	rc.ResData = dbEntity.Password
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *Instance) DeleteInstance(rc *req.Ctx) {
 | 
			
		||||
	idsStr := ginx.PathParam(rc.GinCtx, "dbId")
 | 
			
		||||
	rc.ReqParam = idsStr
 | 
			
		||||
	ids := strings.Split(idsStr, ",")
 | 
			
		||||
 | 
			
		||||
	for _, v := range ids {
 | 
			
		||||
		value, err := strconv.Atoi(v)
 | 
			
		||||
		biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
 | 
			
		||||
		dbId := uint64(value)
 | 
			
		||||
		d.InstanceApp.Delete(dbId)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetInstanceId(g *gin.Context) uint64 {
 | 
			
		||||
	dbId, _ := strconv.Atoi(g.Param("dbId"))
 | 
			
		||||
	biz.IsTrue(dbId > 0, "dbId错误")
 | 
			
		||||
	return uint64(dbId)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								server/internal/db/api/vo/instance.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								server/internal/db/api/vo/instance.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
package vo
 | 
			
		||||
 | 
			
		||||
import "time"
 | 
			
		||||
 | 
			
		||||
type SelectDataInstanceVO struct {
 | 
			
		||||
	//models.BaseModel
 | 
			
		||||
	Id         *int64     `json:"id"`
 | 
			
		||||
	Name       *string    `json:"name"`
 | 
			
		||||
	Host       *string    `json:"host"`
 | 
			
		||||
	Port       *int       `json:"port"`
 | 
			
		||||
	Type       *string    `json:"type"`
 | 
			
		||||
	Params     *string    `json:"params"`
 | 
			
		||||
	Username   *string    `json:"username"`
 | 
			
		||||
	Remark     *string    `json:"remark"`
 | 
			
		||||
	CreateTime *time.Time `json:"createTime"`
 | 
			
		||||
	Creator    *string    `json:"creator"`
 | 
			
		||||
	CreatorId  *int64     `json:"creatorId"`
 | 
			
		||||
 | 
			
		||||
	UpdateTime *time.Time `json:"updateTime"`
 | 
			
		||||
	Modifier   *string    `json:"modifier"`
 | 
			
		||||
	ModifierId *int64     `json:"modifierId"`
 | 
			
		||||
 | 
			
		||||
	SshTunnelMachineId int `json:"sshTunnelMachineId"`
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user