feature: 实现数据库实例管理

This commit is contained in:
kanzihuang
2023-08-27 11:07:29 +08:00
parent 649116a0b8
commit f7b685cfad
17 changed files with 795 additions and 5 deletions

View File

@@ -0,0 +1,213 @@
<template>
<div>
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
<el-form :model="form" ref="dbForm" :rules="rules" label-width="auto">
<el-tabs v-model="tabActiveName">
<el-tab-pane label="基础信息" name="basic">
<el-form-item prop="name" label="别名:" required>
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
</el-form-item>
<el-form-item prop="type" label="类型:" required>
<el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
<el-option key="item.id" label="mysql" value="mysql"> </el-option>
<el-option key="item.id" label="postgres" value="postgres"> </el-option>
</el-select>
</el-form-item>
<el-form-item prop="host" label="host:" required>
<el-col :span="18">
<el-input :disabled="form.id !== undefined" v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
</el-col>
<el-col style="text-align: center" :span="1">:</el-col>
<el-col :span="5">
<el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
</el-col>
</el-form-item>
<el-form-item prop="username" label="用户名:" required>
<el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
</el-form-item>
<el-form-item prop="password" label="密码:">
<el-input
type="password"
show-password
v-model.trim="form.password"
placeholder="请输入密码,修改操作可不填"
autocomplete="new-password"
>
<template v-if="form.id && form.id != 0" #suffix>
<el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
<template #reference>
<el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码 </el-link>
</template>
</el-popover>
</template>
</el-input>
</el-form-item>
<el-form-item prop="remark" label="备注:">
<el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
</el-form-item>
</el-tab-pane>
<el-tab-pane label="其他配置" name="other">
<el-form-item prop="params" label="连接参数:">
<el-input v-model.trim="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2">
<template #suffix>
<el-link
target="_blank"
href="https://github.com/go-sql-driver/mysql#parameters"
:underline="false"
type="primary"
class="mr5"
>参数参考</el-link
>
</template>
</el-input>
</el-form-item>
<el-form-item prop="sshTunnelMachineId" label="SSH隧道:">
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
</el-form-item>
</el-tab-pane>
</el-tabs>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button @click="cancel()"> </el-button>
<el-button type="primary" :loading="btnLoading" @click="btnOk"> </el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script lang="ts" setup>
import { toRefs, reactive, watch, ref } from 'vue';
import { dbApi } from './api';
import { ElMessage } from 'element-plus';
import { notBlank } from '@/common/assert';
import { RsaEncrypt } from '@/common/rsa';
import TagSelect from '../component/TagSelect.vue';
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
const props = defineProps({
visible: {
type: Boolean,
},
db: {
type: [Boolean, Object],
},
title: {
type: String,
},
});
//定义事件
const emit = defineEmits(['update:visible', 'cancel', 'val-change']);
const rules = {
name: [
{
required: true,
message: '请输入别名',
trigger: ['change', 'blur'],
},
],
type: [
{
required: true,
message: '请选择数据库类型',
trigger: ['change', 'blur'],
},
],
host: [
{
required: true,
message: '请输入主机ip和port',
trigger: ['change', 'blur'],
},
],
username: [
{
required: true,
message: '请输入用户名',
trigger: ['change', 'blur'],
},
],
};
const dbForm: any = ref(null);
const state = reactive({
dialogVisible: false,
tabActiveName: 'basic',
form: {
id: null,
type: null,
name: null,
host: '',
port: 3306,
username: null,
password: null,
params: null,
remark: '',
sshTunnelMachineId: null as any,
},
// 原密码
pwd: '',
btnLoading: false,
});
const { dialogVisible, tabActiveName, form, pwd, btnLoading } = toRefs(state);
watch(props, (newValue: any) => {
state.dialogVisible = newValue.visible;
if (!state.dialogVisible) {
return;
}
state.tabActiveName = 'basic';
if (newValue.db) {
state.form = { ...newValue.db };
} else {
state.form = { port: 3306 } as any;
}
});
const getDbPwd = async () => {
state.pwd = await dbApi.getInstancePwd.request({ id: state.form.id });
};
const btnOk = async () => {
if (!state.form.id) {
notBlank(state.form.password, '新增操作,密码不可为空');
}
dbForm.value.validate(async (valid: boolean) => {
if (valid) {
const reqForm = { ...state.form };
reqForm.password = await RsaEncrypt(reqForm.password);
if (!state.form.sshTunnelMachineId) {
reqForm.sshTunnelMachineId = -1;
}
dbApi.saveInstance.request(reqForm).then(() => {
ElMessage.success('保存成功');
emit('val-change', state.form);
state.btnLoading = true;
setTimeout(() => {
state.btnLoading = false;
}, 1000);
cancel();
});
} else {
ElMessage.error('请正确填写信息');
return false;
}
});
};
const cancel = () => {
emit('update:visible', false);
emit('cancel');
};
</script>
<style lang="scss"></style>

View File

@@ -0,0 +1,182 @@
<template>
<div class="db-list">
<page-table
ref="pageTableRef"
:query="queryConfig"
v-model:query-form="query"
:show-selection="true"
v-model:selection-data="state.selectionData"
:data="datas"
:columns="columns"
:total="total"
v-model:page-size="query.pageSize"
v-model:page-num="query.pageNum"
@pageChange="search()"
>
<template #queryRight>
<el-button v-auth="perms.saveInstance" type="primary" icon="plus" @click="editInstance(false)">添加</el-button>
<el-button v-auth="perms.delInstance" :disabled="selectionData.length < 1" @click="deleteInstance()" type="danger" icon="delete">删除</el-button>
</template>
<template #more="{ data }">
<el-button @click="showInfo(data)" link>详情</el-button>
</template>
<template #action="{ data }">
<el-button v-if="actionBtns[perms.saveInstance]" @click="editInstance(data)" type="primary" link>编辑</el-button>
</template>
</page-table>
<el-dialog v-model="infoDialog.visible">
<el-descriptions title="详情" :column="3" border>
<el-descriptions-item :span="2" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
<el-descriptions-item :span="1" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
<el-descriptions-item :span="2" label="主机">{{ infoDialog.data.host }}</el-descriptions-item>
<el-descriptions-item :span="1" label="端口">{{ infoDialog.data.port }}</el-descriptions-item>
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.data.username }}</el-descriptions-item>
<el-descriptions-item :span="1" label="类型">{{ infoDialog.data.type }}</el-descriptions-item>
<el-descriptions-item :span="3" label="连接参数">{{ infoDialog.data.params }}</el-descriptions-item>
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
<el-descriptions-item :span="3" label="SSH隧道">{{ infoDialog.data.sshTunnelMachineId > 0 ? '是' : '否' }} </el-descriptions-item>
<el-descriptions-item :span="2" label="创建时间">{{ dateFormat(infoDialog.data.createTime) }} </el-descriptions-item>
<el-descriptions-item :span="1" label="创建者">{{ infoDialog.data.creator }}</el-descriptions-item>
<el-descriptions-item :span="2" label="更新时间">{{ dateFormat(infoDialog.data.updateTime) }} </el-descriptions-item>
<el-descriptions-item :span="1" label="修改者">{{ infoDialog.data.modifier }}</el-descriptions-item>
</el-descriptions>
</el-dialog>
<instance-edit @val-change="valChange" :title="instanceEditDialog.title" v-model:visible="instanceEditDialog.visible" v-model:db="instanceEditDialog.data"></instance-edit>
</div>
</template>
<script lang="ts" setup>
import { ref, toRefs, reactive, computed, onMounted, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { dbApi } from './api';
import { dateFormat } from '@/common/utils/date';
import PageTable from '@/components/pagetable/PageTable.vue';
import { TableColumn, TableQuery } from '@/components/pagetable';
import { hasPerms } from '@/components/auth/auth';
const InstanceEdit = defineAsyncComponent(() => import('./InstanceEdit.vue'));
const perms = {
saveInstance: 'instance:save',
delInstance: 'instance:del',
};
const queryConfig = [TableQuery.text('name', '名称')];
const columns = ref([
TableColumn.new('name', '名称'),
TableColumn.new('host', 'host:port').setFormatFunc((data: any, _prop: string) => `${data.host}:${data.port}`),
TableColumn.new('type', '类型'),
TableColumn.new('username', '用户名'),
TableColumn.new('remark', '备注'),
TableColumn.new('more', '更多').isSlot().setMinWidth(50).fixedRight(),
]);
// 该用户拥有的的操作列按钮权限
const actionBtns = hasPerms([perms.saveInstance]);
const actionColumn = TableColumn.new('action', '操作').isSlot().setMinWidth(65).fixedRight().alignCenter();
const pageTableRef: any = ref(null);
const state = reactive({
row: {},
dbId: 0,
db: '',
/**
* 选中的数据
*/
selectionData: [],
/**
* 查询条件
*/
query: {
name: null,
pageNum: 1,
pageSize: 10,
},
datas: [],
total: 0,
infoDialog: {
visible: false,
data: null as any,
},
instanceEditDialog: {
visible: false,
data: null as any,
title: '新增数据库实例',
},
});
const {
dbId,
db,
selectionData,
query,
datas,
total,
infoDialog,
instanceEditDialog,
} = toRefs(state);
onMounted(async () => {
if (Object.keys(actionBtns).length > 0) {
columns.value.push(actionColumn);
}
search();
});
const search = async () => {
try {
pageTableRef.value.loading(true);
let res: any = await dbApi.instances.request(state.query);
state.datas = res.list;
state.total = res.total;
} finally {
pageTableRef.value.loading(false);
}
};
const showInfo = (info: any) => {
state.infoDialog.data = info;
state.infoDialog.visible = true;
};
const editInstance = async (data: any) => {
if (!data) {
state.instanceEditDialog.data = null;
state.instanceEditDialog.title = '新增数据库实例';
} else {
state.instanceEditDialog.data = data;
state.instanceEditDialog.title = '修改数据库实例';
}
state.instanceEditDialog.visible = true;
};
const valChange = () => {
search();
};
const deleteInstance = async () => {
try {
await ElMessageBox.confirm(`确定删除【${state.selectionData.map((x: any) => x.name).join(', ')}】实例?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
});
await dbApi.deleteInstance.request({ id: state.selectionData.map((x: any) => x.id).join(',') });
ElMessage.success('删除成功');
search();
} catch (err) {}
};
</script>
<style lang="scss"></style>

View File

@@ -26,4 +26,10 @@ export const dbApi = {
deleteDbSql: Api.newDelete('/dbs/{id}/sql'),
// 获取数据库sql执行记录
getSqlExecs: Api.newGet('/dbs/{dbId}/sql-execs'),
// 获取权限列表
instances: Api.newGet('/instances'),
saveInstance: Api.newPost('/instances'),
getInstancePwd: Api.newGet('/instances/{id}/pwd'),
deleteInstance: Api.newDelete('/instances/{id}'),
};

View 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"`
}

View 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)
}

View 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"`
}

View File

@@ -5,10 +5,15 @@ import (
)
var (
instanceApp Instance = newInstanceApp(persistence.GetInstanceRepo())
dbApp Db = newDbApp(persistence.GetDbRepo(), persistence.GetDbSqlRepo())
dbSqlExecApp DbSqlExec = newDbSqlExecApp(persistence.GetDbSqlExecRepo())
)
func GetInstanceApp() Instance {
return instanceApp
}
func GetDbApp() Db {
return dbApp
}

View File

@@ -0,0 +1,92 @@
package application
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type Instance interface {
// 分页获取
GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Count(condition *entity.InstanceQuery) int64
// 根据条件获取
GetInstanceBy(condition *entity.Instance, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.Instance
Save(instanceEntity *entity.Instance)
// 删除数据库信息
Delete(id uint64)
}
func newInstanceApp(InstanceRepo repository.Instance) Instance {
return &instanceAppImpl{
instanceRepo: InstanceRepo,
}
}
type instanceAppImpl struct {
instanceRepo repository.Instance
}
// 分页获取数据库信息列表
func (d *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return d.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
}
func (d *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
return d.instanceRepo.Count(condition)
}
// 根据条件获取
func (d *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
return d.instanceRepo.GetInstance(condition, cols...)
}
// 根据id获取
func (d *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
return d.instanceRepo.GetById(id, cols...)
}
func (d *instanceAppImpl) Save(instanceEntity *entity.Instance) {
// 默认tcp连接
instanceEntity.Network = instanceEntity.GetNetwork()
// 测试连接
// todo 测试数据库连接
//if instanceEntity.Password != "" {
// TestConnection(instanceEntity)
//}
// 查找是否存在该库
oldInstance := &entity.Instance{Host: instanceEntity.Host, Port: instanceEntity.Port, Username: instanceEntity.Username}
if instanceEntity.SshTunnelMachineId > 0 {
oldInstance.SshTunnelMachineId = instanceEntity.SshTunnelMachineId
}
err := d.GetInstanceBy(oldInstance)
if instanceEntity.Id == 0 {
biz.NotEmpty(instanceEntity.Password, "密码不能为空")
biz.IsTrue(err != nil, "该数据库实例已存在")
instanceEntity.PwdEncrypt()
d.instanceRepo.Insert(instanceEntity)
} else {
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldInstance.Id == instanceEntity.Id, "该数据库实例已存在")
}
instanceEntity.PwdEncrypt()
d.instanceRepo.Update(instanceEntity)
}
}
func (d *instanceAppImpl) Delete(id uint64) {
// todo 删除数据库库实例前必须删除关联数据库
d.instanceRepo.Delete(id)
}

View File

@@ -46,8 +46,3 @@ func (d *Db) PwdDecrypt() {
// 密码替换为解密后的密码
d.Password = utils.PwdAesDecrypt(d.Password)
}
const (
DbTypeMysql = "mysql"
DbTypePostgres = "postgres"
)

View File

@@ -0,0 +1,50 @@
package entity
import (
"fmt"
"mayfly-go/internal/common/utils"
"mayfly-go/pkg/model"
)
type Instance struct {
model.Model
Name string `orm:"column(name)" json:"name"`
Type string `orm:"column(type)" json:"type"` // 类型mysql oracle等
Host string `orm:"column(host)" json:"host"`
Port int `orm:"column(port)" json:"port"`
Network string `orm:"column(network)" json:"network"`
Username string `orm:"column(username)" json:"username"`
Password string `orm:"column(password)" json:"-"`
Params string `orm:"column(params)" json:"params"`
Remark string `orm:"column(remark)" json:"remark"`
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
}
// 获取数据库连接网络, 若没有使用ssh隧道则直接返回。否则返回拼接的网络需要注册至指定dial
func (d *Instance) GetNetwork() string {
network := d.Network
if d.SshTunnelMachineId <= 0 {
if network == "" {
return "tcp"
} else {
return network
}
}
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
}
func (d *Instance) PwdEncrypt() {
// 密码替换为加密后的密码
d.Password = utils.PwdAesEncrypt(d.Password)
}
func (d *Instance) PwdDecrypt() {
// 密码替换为解密后的密码
d.Password = utils.PwdAesDecrypt(d.Password)
}
const (
DbTypeMysql = "mysql"
DbTypePostgres = "postgres"
)

View File

@@ -2,6 +2,21 @@ package entity
import "mayfly-go/pkg/model"
// 数据库实例查询
type InstanceQuery struct {
model.Model
Name string `orm:"column(name)" json:"name" form:"name"`
Type string `orm:"column(type)" json:"type"` // 类型mysql oracle等
Host string `orm:"column(host)" json:"host"`
Port int `orm:"column(port)" json:"port"`
Network string `orm:"column(network)" json:"network"`
Username string `orm:"column(username)" json:"username"`
Password string `orm:"column(password)" json:"-"`
Params string `orm:"column(params)" json:"params"`
Remark string `orm:"column(remark)" json:"remark"`
}
// 数据库查询实体,不与数据库表字段一一对应
type DbQuery struct {
model.Model

View File

@@ -0,0 +1,25 @@
package repository
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/model"
)
type Instance interface {
// 分页获取数据库实例信息列表
GetInstanceList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Count(condition *entity.InstanceQuery) int64
// 根据条件获取账号信息
GetInstance(condition *entity.Instance, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.Instance
Insert(db *entity.Instance)
Update(db *entity.Instance)
Delete(id uint64)
}

View File

@@ -0,0 +1,57 @@
package persistence
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type instanceRepoImpl struct{}
func newInstanceRepo() repository.Instance {
return new(instanceRepoImpl)
}
// 分页获取数据库信息列表
func (d *instanceRepoImpl) GetInstanceList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
qd := gormx.NewQuery(new(entity.Instance)).
Eq("host", condition.Host).
Eq("port", condition.Port).
Eq("username", condition.Username).
Like("name", condition.Name)
return gormx.PageQuery(qd, pageParam, toEntity)
}
func (d *instanceRepoImpl) Count(condition *entity.InstanceQuery) int64 {
where := make(map[string]any)
return gormx.CountByCond(new(entity.Instance), where)
}
// 根据条件获数据库实例信息
func (d *instanceRepoImpl) GetInstance(condition *entity.Instance, cols ...string) error {
return gormx.GetBy(condition, cols...)
}
// 根据id获取数据库实例
func (d *instanceRepoImpl) GetById(id uint64, cols ...string) *entity.Instance {
instance := new(entity.Instance)
if err := gormx.GetById(instance, id, cols...); err != nil {
return nil
}
return instance
}
func (d *instanceRepoImpl) Insert(db *entity.Instance) {
biz.ErrIsNil(gormx.Insert(db), "新增数据库实例失败")
}
func (d *instanceRepoImpl) Update(db *entity.Instance) {
biz.ErrIsNil(gormx.UpdateById(db), "更新数据库实例失败")
}
func (d *instanceRepoImpl) Delete(id uint64) {
gormx.DeleteById(new(entity.Instance), id)
}

View File

@@ -3,11 +3,16 @@ package persistence
import "mayfly-go/internal/db/domain/repository"
var (
instanceRepo repository.Instance = newInstanceRepo()
dbRepo repository.Db = newDbRepo()
dbSqlRepo repository.DbSql = newDbSqlRepo()
dbSqlExecRepo repository.DbSqlExec = newDbSqlExecRepo()
)
func GetInstanceRepo() repository.Instance {
return instanceRepo
}
func GetDbRepo() repository.Db {
return dbRepo
}

View File

@@ -0,0 +1,32 @@
package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
msgapp "mayfly-go/internal/msg/application"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
)
func InitInstanceRouter(router *gin.RouterGroup) {
instances := router.Group("/instances")
d := &api.Instance{
InstanceApp: application.GetInstanceApp(),
MsgApp: msgapp.GetMsgApp(),
}
reqs := [...]*req.Conf{
// 获取数据库列表
req.NewGet("", d.Instances),
req.NewPost("", d.SaveInstance).Log(req.NewLogSave("db-保存数据库实例信息")),
req.NewGet(":dbId/pwd", d.GetInstancePwd),
req.NewDelete(":dbId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
}
req.BatchSetGroup(instances, reqs[:])
}

View File

@@ -3,6 +3,7 @@ package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitInstanceRouter(router)
InitDbRouter(router)
InitDbSqlExecRouter(router)
}

View File

@@ -43,6 +43,9 @@ func T2022() *gormigrate.Migration {
return err
}
if err := tx.AutoMigrate(&entity2.Instance{}); err != nil {
return err
}
if err := tx.AutoMigrate(&entity2.Db{}); err != nil {
return err
}