Compare commits

..

3 Commits

Author SHA1 Message Date
meilin.huang
db554ebdc9 feat: 新增系统操作日志&其他优化 2022-07-14 11:39:12 +08:00
meilin.huang
1c18a01bf6 feat: 新增pgsql数据操作&redis集群操作 2022-07-10 12:14:06 +08:00
meilin.huang
729a3d7028 feat: 新增linux文件夹创建&删除&其他优化 2022-07-04 20:21:24 +08:00
72 changed files with 7524 additions and 1637 deletions

View File

@@ -1,7 +1,7 @@
# 🌈mayfly-go
### 介绍
简单基于DDD(领域驱动设计)分层架构实现的web版 **linux、数据库mysql、redis、mongo统一管理操作平台**
简单基于DDD(领域驱动设计)分层架构实现的web版 **linux、数据库mysql postgres、redis(单机 集群)、mongo统一管理操作平台**
### 开发语言与主要框架

5116
mayfly_go_web/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -13,7 +13,7 @@
"countup.js": "^2.0.7",
"cropperjs": "^1.5.11",
"echarts": "^5.3.3",
"element-plus": "^2.2.8",
"element-plus": "^2.2.9",
"jsoneditor": "^9.9.0",
"lodash": "^4.17.21",
"mitt": "^3.0.0",

View File

@@ -1,4 +1,5 @@
window.globalConfig = {
"BaseApiUrl": "http://localhost:8888",
"BaseWsUrl": "ws://localhost:8888"
// 默认为空以访问根目录为api请求地址。若前后端分离部署可单独配置该后端api请求地址
"BaseApiUrl": "",
"BaseWsUrl": ""
}

View File

@@ -1,6 +1,6 @@
const config = {
baseApiUrl: `${(window as any).globalConfig.BaseApiUrl}/api`,
baseWsUrl: `${(window as any).globalConfig.BaseWsUrl}/api`
baseApiUrl: `${(window as any).globalConfig.BaseApiUrl}/api`,
baseWsUrl: `${(window as any).globalConfig.BaseWsUrl || `${location.protocol == 'https:' ? 'wss:' : 'ws:'}//${location.host}`}/api`
}
export default config

View File

@@ -10,6 +10,7 @@ export const imports = {
"ResourceList": () => import('@/views/system/resource'),
"RoleList": () => import('@/views/system/role'),
"AccountList": () => import('@/views/system/account'),
"SyslogList": () => import('@/views/system/syslog/SyslogList.vue'),
// project
"ProjectList": () => import('@/views/ops/project/ProjectList.vue'),
// db

View File

@@ -44,7 +44,9 @@ export default defineComponent({
state.isShowSearch = true;
initTageView();
nextTick(() => {
layoutMenuAutocompleteRef.value.focus();
setTimeout(() => {
layoutMenuAutocompleteRef.value.focus();
});
});
};
// 搜索弹窗关闭

View File

@@ -1,7 +1,7 @@
<template>
<div>
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="35%">
<el-form :model="form" ref="dbForm" :rules="rules" label-width="85px">
<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="95px">
<el-form-item prop="projectId" label="项目:" required>
<el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
<el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
@@ -19,6 +19,7 @@
<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>
@@ -39,6 +40,9 @@
autocomplete="new-password"
></el-input>
</el-form-item>
<el-form-item prop="params" label="连接参数:">
<el-input v-model="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2"></el-input>
</el-form-item>
<el-form-item prop="database" label="数据库名:" required>
<el-tag
v-for="db in databaseList"
@@ -116,6 +120,7 @@ export default defineComponent({
port: 3306,
username: null,
password: null,
params: null,
database: '',
project: null,
projectId: null,
@@ -173,13 +178,6 @@ export default defineComponent({
trigger: ['change', 'blur'],
},
],
// password: [
// {
// required: true,
// message: '请输入密码',
// trigger: ['change', 'blur'],
// },
// ],
database: [
{
required: true,

View File

@@ -20,15 +20,15 @@
</el-radio>
</template>
</el-table-column>
<el-table-column prop="project" label="项目" min-width="100"></el-table-column>
<el-table-column prop="project" label="项目" min-width="100" show-overflow-tooltip></el-table-column>
<el-table-column prop="env" label="环境" min-width="100"></el-table-column>
<el-table-column prop="name" label="名称" min-width="200"></el-table-column>
<el-table-column min-width="160" label="host:port">
<el-table-column prop="name" label="名称" min-width="160" show-overflow-tooltip></el-table-column>
<el-table-column min-width="170" label="host:port" show-overflow-tooltip>
<template #default="scope">
{{ `${scope.row.host}:${scope.row.port}` }}
</template>
</el-table-column>
<el-table-column prop="type" label="类型" min-width="80"></el-table-column>
<el-table-column prop="type" label="类型" min-width="90"></el-table-column>
<el-table-column prop="database" label="数据库" min-width="160">
<template #default="scope">
<el-tag
@@ -224,6 +224,7 @@
<el-table-column prop="columnName" label="列名" show-overflow-tooltip> </el-table-column>
<el-table-column prop="seqInIndex" label="列序列号" show-overflow-tooltip> </el-table-column>
<el-table-column prop="indexType" label="类型"> </el-table-column>
<el-table-column prop="indexComment" label="备注" min-width="230" show-overflow-tooltip> </el-table-column>
</el-table>
</el-dialog>
@@ -341,7 +342,7 @@ export default defineComponent({
onMounted(async () => {
search();
state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
state.projects = await projectApi.accountProjects.request(null);
});
const choose = (item: any) => {

View File

@@ -276,6 +276,7 @@ export default defineComponent({
dbs: [], // 数据库实例列表
databaseList: [], // 数据库实例拥有的数据库列表1数据库实例 -> 多数据库
db: '', // 当前操作的数据库
dbType: '',
tables: [],
dbId: null, // 当前选中操作的数据库实例
tableName: '',
@@ -622,7 +623,9 @@ export default defineComponent({
*/
const changeDbInstance = (dbId: any) => {
state.db = '';
state.databaseList = (state.dbs.find((e: any) => e.id == dbId) as any).database.split(' ');
const dbInfo = state.dbs.find((e: any) => e.id == dbId) as any;
state.dbType = dbInfo.type;
state.databaseList = dbInfo.database.split(' ');
clearDb();
};
@@ -788,16 +791,21 @@ export default defineComponent({
* 获取默认查询语句
*/
const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '', pageNum: number = 1) => {
return `SELECT * FROM \`${tableName}\` ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''} LIMIT ${
(pageNum - 1) * state.defalutLimit
}, ${state.defalutLimit}`;
const baseSql = `SELECT * FROM ${tableName} ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''}`;
if (state.dbType == 'mysql') {
return `${baseSql} LIMIT ${(pageNum - 1) * state.defalutLimit}, ${state.defalutLimit};`
}
if (state.dbType == 'postgres') {
return `${baseSql} OFFSET ${(pageNum - 1) * state.defalutLimit} LIMIT ${state.defalutLimit};`
}
return baseSql;
};
/**
* 获取默认查询统计语句
*/
const getDefaultCountSql = (tableName: string, where: string = '') => {
return `SELECT COUNT(*) count FROM \`${tableName}\` ${where ? 'WHERE ' + where : ''}`;
return `SELECT COUNT(*) count FROM ${tableName} ${where ? 'WHERE ' + where : ''}`;
};
/**
@@ -819,7 +827,7 @@ export default defineComponent({
const tableName = state.activeName;
const sortType = sort.order == 'descending' ? 'DESC' : 'ASC';
const orderBy = `ORDER BY \`${sort.prop}\` ${sortType}`;
const orderBy = `ORDER BY ${sort.prop} ${sortType}`;
state.dataTabs[state.activeName].orderBy = orderBy;
onRefresh(tableName);

View File

@@ -22,5 +22,5 @@ export const dbApi = {
getSqlNames: Api.create("/dbs/{id}/sql-names", 'get'),
deleteDbSql: Api.create("/dbs/{id}/sql", 'delete'),
// 获取数据库sql执行记录
getSqlExecs: Api.create("/dbs/{id}/sql-execs", 'get'),
getSqlExecs: Api.create("/dbs/{dbId}/sql-execs", 'get'),
}

View File

@@ -135,7 +135,9 @@ export default defineComponent({
state.db = props.db;
state.dialogVisible = true;
nextTick(() => {
remarkInputRef.value?.focus();
setTimeout(() => {
remarkInputRef.value?.focus();
});
});
};

View File

@@ -96,6 +96,12 @@
<el-link type="info" icon="view" :underline="false">查看</el-link>
</el-dropdown-item>
<span v-auth="'machine:file:write'">
<el-dropdown-item @click="showCreateFileDialog(node, data)" v-if="data.type == 'd'">
<el-link type="primary" icon="document" :underline="false" style="margin-left: 2px">新建</el-link>
</el-dropdown-item>
</span>
<span v-auth="'machine:file:upload'">
<el-dropdown-item v-if="data.type == 'd'">
<el-upload
@@ -133,6 +139,35 @@
</div>
</el-dialog>
<el-dialog
:destroy-on-close="true"
title="新建文件"
v-model="createFileDialog.visible"
:before-close="closeCreateFileDialog"
:close-on-click-modal="false"
top="5vh"
width="400px"
>
<div>
<el-form-item prop="name" label="名称:">
<el-input v-model.trim="createFileDialog.name" placeholder="请输入名称" auto-complete="off"></el-input>
</el-form-item>
<el-form-item prop="type" label="类型:">
<el-radio-group v-model="createFileDialog.type">
<el-radio label="d" size="small">文件夹</el-radio>
<el-radio label="-" size="small">文件</el-radio>
</el-radio-group>
</el-form-item>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="closeCreateFileDialog">关闭</el-button>
<el-button v-auth="'machine:file:write'" type="primary" @click="createFile">确定</el-button>
</div>
</template>
</el-dialog>
<el-dialog
:destroy-on-close="true"
:title="fileContent.dialogTitle"
@@ -164,6 +199,7 @@ import { codemirror } from '@/components/codemirror';
import { getSession } from '@/common/utils/storage';
import enums from './enums';
import config from '@/common/config';
import { isTrue } from '@/common/assert';
export default defineComponent({
name: 'FileManage',
@@ -184,26 +220,8 @@ export default defineComponent({
const fileTree: any = ref(null);
const token = getSession('token');
const cmOptions = {
tabSize: 2,
mode: 'text/x-sh',
theme: 'panda-syntax',
line: true,
// 开启校验
lint: true,
gutters: ['CodeMirror-lint-markers'],
indentWithTabs: true,
smartIndent: true,
matchBrackets: true,
autofocus: true,
styleSelectedText: true,
styleActiveLine: true, // 高亮选中行
foldGutter: true, // 块槽
hintOptions: {
// 当匹配只有一项的时候是否自动补全
completeSingle: true,
},
};
const folderType = 'd';
const fileType = '-';
const state = reactive({
dialogVisible: false,
@@ -250,6 +268,12 @@ export default defineComponent({
path: '',
type: '',
},
createFileDialog: {
visible: false,
name: '',
type: folderType,
node: null as any,
},
file: null as any,
});
@@ -272,18 +296,6 @@ export default defineComponent({
getFiles();
};
/**
* tab切换触发事件
* @param {Object} tab
* @param {Object} event
*/
// handleClick(tab, event) {
// // if (tab.name == 'file-manage') {
// // this.fileManage.node.childNodes = [];
// // this.loadNode(this.fileManage.node, this.fileManage.resolve);
// // }
// }
const add = () => {
// 往数组头部添加元素
state.fileTable = [{}].concat(state.fileTable);
@@ -311,7 +323,6 @@ export default defineComponent({
})
.then(() => {
getFiles();
// state.fileTable.splice(idx, 1);
});
});
} else {
@@ -388,7 +399,6 @@ export default defineComponent({
emit('update:visible', false);
emit('update:machineId', null);
emit('cancel');
// state.activeName = 'conf-file'
state.fileTable = [];
state.tree.folder = { id: 0 };
};
@@ -413,7 +423,7 @@ export default defineComponent({
return resolve([
{
name: path,
type: 'd',
type: folderType,
path: path,
},
]);
@@ -435,13 +445,42 @@ export default defineComponent({
});
for (const file of res) {
const type = file.type;
if (type != 'd') {
if (type == fileType) {
file.leaf = true;
}
}
return resolve(res);
};
const showCreateFileDialog = (node: any) => {
isTrue(node.expanded, '请先点击展开该节点后再创建');
state.createFileDialog.node = node;
state.createFileDialog.visible = true;
};
const createFile = async () => {
const node = state.createFileDialog.node;
console.log(node.data);
const name = state.createFileDialog.name;
const type = state.createFileDialog.type;
const path = node.data.path + '/' + name;
await machineApi.createFile.request({
machineId: props.machineId,
id: state.tree.folder.id,
path,
type,
});
fileTree.value.append({ name: name, path: path, type: type, leaf: type === fileType, size: 0 }, node);
closeCreateFileDialog();
};
const closeCreateFileDialog = () => {
state.createFileDialog.visible = false;
state.createFileDialog.node = null;
state.createFileDialog.name = '';
state.createFileDialog.type = folderType;
};
const deleteFile = (node: any, data: any) => {
const file = data.path;
ElMessageBox.confirm(`此操作将删除 [${file}], 是否继续?`, '提示', {
@@ -468,7 +507,6 @@ export default defineComponent({
const downloadFile = (node: any, data: any) => {
const a = document.createElement('a');
// a.setAttribute('target', '_blank')
a.setAttribute(
'href',
`${config.baseApiUrl}/machines/${props.machineId}/files/${state.tree.folder.id}/read?type=1&path=${data.path}&token=${token}`
@@ -516,7 +554,6 @@ export default defineComponent({
const beforeUpload = (file: File) => {
state.file = file;
// ElMessage.success(`'${file.name}' 上传中,请关注结果通知`);
};
const getFilePath = (data: object, visible: boolean) => {
if (visible) {
@@ -572,7 +609,6 @@ export default defineComponent({
fileTree,
enums,
token,
cmOptions,
add,
getFiles,
handlePageChange,
@@ -583,6 +619,9 @@ export default defineComponent({
updateContent,
handleClose,
loadNode,
showCreateFileDialog,
closeCreateFileDialog,
createFile,
deleteFile,
downloadFile,
getUploadFile,

View File

@@ -25,7 +25,7 @@
</el-form-item>
<el-form-item prop="params" label="参数">
<el-input v-model.trim="form.params" placeholder="参数数组json若无可不填"></el-input>
<el-input v-model="form.params" placeholder="参数数组json若无可不填"></el-input>
</el-form-item>
<el-form-item prop="script" label="内容" id="content">

View File

@@ -25,6 +25,7 @@ export const machineApi = {
rmFile: Api.create("/machines/{machineId}/files/{fileId}/remove", 'delete'),
uploadFile: Api.create("/machines/{machineId}/files/{fileId}/upload?token={token}", 'post'),
fileContent: Api.create("/machines/{machineId}/files/{fileId}/read", 'get'),
createFile: Api.create("/machines/{machineId}/files/{id}/create-file", 'post'),
// 修改文件内容
updateFileContent: Api.create("/machines/{machineId}/files/{id}/write", 'post'),
// 添加文件or目录

View File

@@ -250,7 +250,7 @@ export default defineComponent({
onMounted(async () => {
search();
state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
state.projects = await projectApi.accountProjects.request(null);
});
const handlePageChange = (curPage: number) => {
@@ -266,12 +266,6 @@ export default defineComponent({
state.currentData = item;
};
// connect() {
// Req.post('/open/redis/connect', this.form, res => {
// this.redisInfo = res
// })
// }
const showDatabases = async (id: number) => {
state.databaseDialog.data = (await mongoApi.databases.request({ id })).Databases;
state.databaseDialog.title = `数据库列表`;
@@ -371,14 +365,6 @@ export default defineComponent({
} catch (err) {}
};
// const info = (redis: any) => {
// redisApi.redisInfo.request({ id: redis.id }).then((res: any) => {
// state.infoDialog.info = res;
// state.infoDialog.title = `'${redis.host}' info`;
// state.infoDialog.visible = true;
// });
// };
const search = async () => {
const res = await mongoApi.mongoList.request(state.query);
state.list = res.list;

View File

@@ -31,7 +31,7 @@
></el-input>
</el-form-item>
<el-form-item label="count" label-width="60px">
<el-input placeholder="count" style="width: 62px" v-model="scanParam.count"></el-input>
<el-input placeholder="count" style="width: 62px" v-model.number="scanParam.count"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="searchKey()" type="success" icon="search" plain></el-button>
@@ -92,7 +92,7 @@ import { toRefs, reactive, defineComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import ProjectEnvSelect from '../component/ProjectEnvSelect.vue';
import DataEdit from './DataEdit.vue';
import { isTrue, notNull } from '@/common/assert';
import { isTrue, notBlank, notNull } from '@/common/assert';
export default defineComponent({
name: 'DataOperation',
@@ -103,18 +103,15 @@ export default defineComponent({
setup() {
const state = reactive({
loading: false,
cluster: 0,
redisList: [],
query: {
envId: 0,
},
scanParam: {
id: null,
cluster: 0,
match: null,
count: 10,
cursor: 0,
prevCursor: null,
cursor: {},
},
valueDialog: {
visible: false,
@@ -151,31 +148,33 @@ export default defineComponent({
}
};
const changeRedis = () => {
resetScanParam();
const changeRedis = (id: number) => {
resetScanParam(id);
state.keys = [];
state.dbsize = 0;
searchKey();
};
const scan = () => {
const scan = async () => {
isTrue(state.scanParam.id != null, '请先选择redis');
notBlank(state.scanParam.count, 'count不能为空');
isTrue(state.scanParam.count < 20001, 'count不能超过20000');
state.loading = true;
state.scanParam.cluster = state.cluster == 0 ? 0 : 1;
redisApi.scan.request(state.scanParam).then((res) => {
try {
const res = await redisApi.scan.request(state.scanParam);
state.keys = res.keys;
state.dbsize = res.dbSize;
state.scanParam.cursor = res.cursor;
} finally {
state.loading = false;
});
}
};
const searchKey = () => {
state.scanParam.cursor = 0;
scan();
const searchKey = async () => {
state.scanParam.cursor = {};
await scan();
};
const clearRedis = () => {
@@ -193,10 +192,17 @@ export default defineComponent({
}
};
const resetScanParam = () => {
state.scanParam.match = null;
state.scanParam.cursor = 0;
const resetScanParam = (id: number = 0) => {
state.scanParam.count = 10;
if (id != 0) {
const redis: any = state.redisList.find((x: any) => x.id == id);
// 集群模式count设小点因为后端会从所有master节点scan一遍然后合并结果
if (redis && redis.mode == 'cluster') {
state.scanParam.count = 5;
}
}
state.scanParam.match = null;
state.scanParam.cursor = {};
};
const getValue = async (row: any) => {
@@ -204,11 +210,9 @@ export default defineComponent({
const key = row.key;
let res: any;
const id = state.cluster == 0 ? state.scanParam.id : state.cluster;
const reqParam = {
cluster: state.cluster,
key: row.key,
id,
id: state.scanParam.id,
};
switch (type) {
case 'string':
@@ -260,29 +264,27 @@ export default defineComponent({
};
const del = (key: string) => {
ElMessageBox.confirm(`此操作将删除对应的key , 是否继续?`, '提示', {
ElMessageBox.confirm(`确定删除[ ${key} ] 该key?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
let id = state.cluster == 0 ? state.scanParam.id : state.cluster;
redisApi.delKey
.request({
cluster: state.cluster,
key,
id,
id: state.scanParam.id,
})
.then(() => {
ElMessage.success('删除成功!');
scan();
searchKey();
});
})
.catch(() => {});
};
const ttlConveter = (ttl: any) => {
if (ttl == -1) {
if (ttl == -1 || ttl == 0) {
return '永久';
}
if (!ttl) {

View File

@@ -13,8 +13,14 @@
<el-option v-for="item in envs" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
</el-select>
</el-form-item>
<el-form-item prop="mode" label="mode:" required>
<el-select style="width: 100%" v-model="form.mode" placeholder="请选择模式">
<el-option label="standalone" value="standalone"> </el-option>
<el-option label="cluster" value="cluster"> </el-option>
</el-select>
</el-form-item>
<el-form-item prop="host" label="host:" required>
<el-input v-model.trim="form.host" placeholder="请输入host:port" auto-complete="off"></el-input>
<el-input v-model.trim="form.host" placeholder="请输入host:port,集群模式用','分割" auto-complete="off" type="textarea"></el-input>
</el-form-item>
<el-form-item prop="password" label="密码:">
<el-input
@@ -28,6 +34,9 @@
<el-form-item prop="db" label="库号:" required>
<el-input v-model.number="form.db" placeholder="请输入库号"></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-form>
<template #footer>
@@ -71,12 +80,14 @@ export default defineComponent({
form: {
id: null,
name: null,
mode: "standalone",
host: null,
password: null,
project: null,
projectId: null,
envId: null,
env: null,
remark: "",
},
btnLoading: false,
rules: {
@@ -108,6 +119,13 @@ export default defineComponent({
trigger: ['change', 'blur'],
},
],
mode: [
{
required: true,
message: '请输入模式',
trigger: ['change', 'blur'],
},
],
},
});

View File

@@ -5,16 +5,12 @@
<el-button type="primary" icon="edit" :disabled="currentId == null" @click="editRedis(false)" plain>编辑</el-button>
<el-button type="danger" icon="delete" :disabled="currentId == null" @click="deleteRedis" plain>删除</el-button>
<div style="float: right">
<!-- <el-input placeholder="host" style="width: 140px" v-model="query.host" @clear="search" plain clearable></el-input>
<el-select v-model="params.clusterId" clearable placeholder="集群选择">
<el-option v-for="item in clusters" :key="item.id" :value="item.id" :label="item.name"></el-option>
</el-select> -->
<el-select v-model="query.projectId" placeholder="请选择项目" filterable clearable>
<el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
</el-select>
<el-button class="ml5" @click="search" type="success" icon="search"></el-button>
</div>
<el-table :data="redisTable" style="width: 100%" @current-change="choose" stripe>
<el-table :data="redisTable" @current-change="choose" stripe>
<el-table-column label="选择" width="60px">
<template #default="scope">
<el-radio v-model="currentId" :label="scope.row.id">
@@ -22,19 +18,23 @@
</el-radio>
</template>
</el-table-column>
<el-table-column prop="project" label="项目" width></el-table-column>
<el-table-column prop="env" label="环境" width></el-table-column>
<el-table-column prop="host" label="host:port" width></el-table-column>
<el-table-column prop="createTime" label="创建时间">
<el-table-column prop="project" label="项目" min-width="100"></el-table-column>
<el-table-column prop="env" label="环境" min-width="100"></el-table-column>
<el-table-column prop="host" label="host:port" min-width="150" show-overflow-tooltip> </el-table-column>
<el-table-column prop="mode" label="mode" min-width="100"></el-table-column>
<el-table-column prop="remark" label="备注" min-width="100"></el-table-column>
<el-table-column prop="createTime" label="创建时间" min-width="160">
<template #default="scope">
{{ $filters.dateFormat(scope.row.createTime) }}
</template>
</el-table-column>
<el-table-column prop="creator" label="创建人"></el-table-column>
<el-table-column label="操作" width>
<el-table-column prop="creator" label="创建人" min-width="100"></el-table-column>
<el-table-column label="更多" min-width="130" fixed="right">
<template #default="scope">
<el-button type="primary" @click="info(scope.row)" icon="tickets" plain size="small">info</el-button>
<!-- <el-button type="success" @click="manage(scope.row)" :ref="scope.row" plain>数据管理</el-button> -->
<el-link v-if="scope.row.mode == 'standalone'" type="primary" @click="info(scope.row)" :underline="false">单机信息</el-link>
<el-link @click="onShowClusterInfo(scope.row)" v-if="scope.row.mode == 'cluster'" type="success" :underline="false"
>集群信息</el-link
>
</template>
</el-table-column>
</el-table>
@@ -52,6 +52,84 @@
<info v-model:visible="infoDialog.visible" :title="infoDialog.title" :info="infoDialog.info"></info>
<el-dialog width="1000px" title="集群信息" v-model="clusterInfoDialog.visible">
<el-input type="textarea" :autosize="{ minRows: 12, maxRows: 12 }" v-model="clusterInfoDialog.info"> </el-input>
<el-divider content-position="left">节点信息</el-divider>
<el-table :data="clusterInfoDialog.nodes" stripe size="small" border>
<el-table-column prop="nodeId" label="nodeId" min-width="300">
<template #header>
nodeId
<el-tooltip class="box-item" effect="dark" content="节点id" placement="top">
<el-icon><question-filled /></el-icon>
</el-tooltip>
</template>
</el-table-column>
<el-table-column prop="ip" label="ip" min-width="180">
<template #header>
ip
<el-tooltip
class="box-item"
effect="dark"
content="ip:port1@port2port1指redis服务器与客户端通信的端口port2则是集群内部节点间通信的端口"
placement="top"
>
<el-icon><question-filled /></el-icon>
</el-tooltip>
</template>
<template #default="scope">
<el-tag
@click="info({ id: clusterInfoDialog.redisId, ip: scope.row.ip })"
effect="plain"
type="success"
size="small"
style="cursor: pointer"
>{{ scope.row.ip }}</el-tag
>
</template>
</el-table-column>
<el-table-column prop="flags" label="flags" min-width="110"></el-table-column>
<el-table-column prop="masterSlaveRelation" label="masterSlaveRelation" min-width="300">
<template #header>
masterSlaveRelation
<el-tooltip
class="box-item"
effect="dark"
content="如果节点是slave并且已知master节点则为master节点ID否则为符号'-'"
placement="top"
>
<el-icon><question-filled /></el-icon>
</el-tooltip>
</template>
</el-table-column>
<el-table-column prop="pingSent" label="pingSent" min-width="130" show-overflow-tooltip>
<template #default="scope">
{{ scope.row.pingSent == 0 ? 0 : new Date(parseInt(scope.row.pingSent)).toLocaleString() }}
</template>
</el-table-column>
<el-table-column prop="pongRecv" label="pongRecv" min-width="130" show-overflow-tooltip>
<template #default="scope">
{{ scope.row.pongRecv == 0 ? 0 : new Date(parseInt(scope.row.pongRecv)).toLocaleString() }}
</template>
</el-table-column>
<el-table-column prop="configEpoch" label="configEpoch" min-width="130">
<template #header>
configEpoch
<el-tooltip
class="box-item"
effect="dark"
content="节点的epoch值如果该节点是从节点则为其主节点的epoch值。每当节点发生失败切换时都会创建一个新的独特的递增的epoch。"
placement="top"
>
<el-icon><question-filled /></el-icon>
</el-tooltip>
</template>
</el-table-column>
<el-table-column prop="linkState" label="linkState" min-width="100"></el-table-column>
<el-table-column prop="slot" label="slot" min-width="100"></el-table-column>
</el-table>
</el-dialog>
<redis-edit
@val-change="valChange"
:projects="projects"
@@ -92,6 +170,12 @@ export default defineComponent({
redisInfo: {
url: '',
},
clusterInfoDialog: {
visible: false,
redisId: 0,
info: '',
nodes: [],
},
clusters: [
{
id: 0,
@@ -118,7 +202,7 @@ export default defineComponent({
onMounted(async () => {
search();
state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
state.projects = await projectApi.accountProjects.request(null);
});
const handlePageChange = (curPage: number) => {
@@ -134,12 +218,6 @@ export default defineComponent({
state.currentData = item;
};
// connect() {
// Req.post('/open/redis/connect', this.form, res => {
// this.redisInfo = res
// })
// }
const deleteRedis = async () => {
try {
await ElMessageBox.confirm(`确定删除该redis?`, '提示', {
@@ -155,12 +233,23 @@ export default defineComponent({
} catch (err) {}
};
const info = (redis: any) => {
redisApi.redisInfo.request({ id: redis.id }).then((res: any) => {
state.infoDialog.info = res;
state.infoDialog.title = `'${redis.host}' info`;
state.infoDialog.visible = true;
});
const info = async (redis: any) => {
var host = redis.host;
if (redis.ip) {
host = redis.ip.split('@')[0];
}
const res = await redisApi.redisInfo.request({ id: redis.id, host });
state.infoDialog.info = res;
state.infoDialog.title = `'${host}' info`;
state.infoDialog.visible = true;
};
const onShowClusterInfo = async (redis: any) => {
const ci = await redisApi.clusterInfo.request({ id: redis.id });
state.clusterInfoDialog.info = ci.clusterInfo;
state.clusterInfoDialog.nodes = ci.clusterNodes;
state.clusterInfoDialog.redisId = redis.id;
state.clusterInfoDialog.visible = true;
};
const search = async () => {
@@ -192,6 +281,7 @@ export default defineComponent({
handlePageChange,
choose,
info,
onShowClusterInfo,
deleteRedis,
editRedis,
valChange,

View File

@@ -3,10 +3,11 @@ import Api from '@/common/Api';
export const redisApi = {
redisList : Api.create("/redis", 'get'),
redisInfo: Api.create("/redis/{id}/info", 'get'),
clusterInfo: Api.create("/redis/{id}/cluster-info", 'get'),
saveRedis: Api.create("/redis", 'post'),
delRedis: Api.create("/redis/{id}", 'delete'),
// 获取权限列表
scan: Api.create("/redis/{id}/scan/{cursor}/{count}", 'get'),
scan: Api.create("/redis/{id}/scan", 'post'),
getStringValue: Api.create("/redis/{id}/string-value", 'get'),
saveStringValue: Api.create("/redis/{id}/string-value", 'post'),
getHashValue: Api.create("/redis/{id}/hash-value", 'get'),

View File

@@ -3,7 +3,7 @@
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :show-close="false" width="35%" :destroy-on-close="true">
<el-form :model="form" ref="accountForm" :rules="rules" label-width="85px">
<el-form-item prop="username" label="用户名:" required>
<el-input :disabled="edit" v-model.trim="form.username" placeholder="请输入账号用户名" auto-complete="off"></el-input>
<el-input :disabled="edit" v-model.trim="form.username" placeholder="请输入账号用户名,密码默认与账号名一致" auto-complete="off"></el-input>
</el-form-item>
<!-- <el-form-item prop="password" label="密码:" required>
<el-input type="password" v-model.trim="form.password" placeholder="请输入密码" autocomplete="new-password"></el-input>

View File

@@ -2,7 +2,7 @@
<div class="role-list">
<el-card>
<el-button v-auth="'account:add'" type="primary" icon="plus" @click="editAccount(true)">添加</el-button>
<el-button v-auth="'account:update'" :disabled="chooseId == null" @click="editAccount(false)" type="primary" icon="edit">编辑</el-button>
<!-- <el-button v-auth="'account:update'" :disabled="chooseId == null" @click="editAccount(false)" type="primary" icon="edit">编辑</el-button> -->
<el-button v-auth="'account:saveRoles'" :disabled="chooseId == null" @click="roleEdit()" type="success" icon="setting"
>角色分配</el-button
>

View File

@@ -33,5 +33,5 @@ export const accountApi = {
}
export const logApi = {
list: Api.create("/sys/logs", "get")
list: Api.create("/syslogs", "get")
}

View File

@@ -10,15 +10,14 @@
<div style="float: right">
<el-input
placeholder="请输入角色名称"
placeholder="请输入角色名称"
class="mr2"
size="small"
style="width: 300px"
style="width: 200px"
v-model="query.name"
@clear="search"
clearable
></el-input>
<el-button @click="search" type="success" icon="search" size="small"></el-button>
<el-button @click="search" type="success" icon="search"></el-button>
</div>
<el-table :data="roles" @current-change="choose" ref="table" style="width: 100%">
<el-table-column label="选择" width="50px">

View File

@@ -0,0 +1,103 @@
<template>
<div class="role-list">
<el-card>
<div style="float: right">
<el-select
remote
:remote-method="getAccount"
v-model="query.creatorId"
filterable
placeholder="请输入并选择账号"
clearable
class="mr5"
>
<el-option v-for="item in accounts" :key="item.id" :label="item.username" :value="item.id"> </el-option>
</el-select>
<el-select v-model="query.type" filterable placeholder="请选择操作结果" clearable class="mr5">
<el-option label="成功" :value="1"> </el-option>
<el-option label="失败" :value="2"> </el-option>
</el-select>
<el-button @click="search" type="success" icon="search"></el-button>
</div>
<el-table :data="logs" style="width: 100%">
<el-table-column prop="creator" label="操作人" min-width="100" show-overflow-tooltip></el-table-column>
<el-table-column prop="createTime" label="操作时间" min-width="160">
<template #default="scope">
{{ $filters.dateFormat(scope.row.createTime) }}
</template>
</el-table-column>
<el-table-column prop="type" label="结果" min-width="65">
<template #default="scope">
<el-tag v-if="scope.row.type == 1" type="success" size="small">成功</el-tag>
<el-tag v-if="scope.row.type == 2" type="danger" size="small">失败</el-tag>
</template>
</el-table-column>
<el-table-column prop="description" label="描述" min-width="160" show-overflow-tooltip></el-table-column>
<el-table-column prop="reqParam" label="请求信息" min-width="300" show-overflow-tooltip></el-table-column>
<el-table-column prop="resp" label="响应信息" min-width="200" show-overflow-tooltip></el-table-column>
</el-table>
<el-row style="margin-top: 20px" type="flex" justify="end">
<el-pagination
style="text-align: right"
@current-change="handlePageChange"
:total="total"
layout="prev, pager, next, total, jumper"
v-model:current-page="query.pageNum"
:page-size="query.pageSize"
></el-pagination>
</el-row>
</el-card>
</div>
</template>
<script lang="ts">
import { toRefs, reactive, onMounted, defineComponent } from 'vue';
import { logApi, accountApi } from '../api';
export default defineComponent({
name: 'SyslogList',
components: {},
setup() {
const state = reactive({
query: {
pageNum: 1,
pageSize: 10,
name: null,
},
total: 0,
logs: [],
accounts: [],
});
onMounted(() => {
search();
});
const search = async () => {
let res = await logApi.list.request(state.query);
state.logs = res.list;
state.total = res.total;
};
const handlePageChange = (curPage: number) => {
state.query.pageNum = curPage;
search();
};
const getAccount = (username: any) => {
accountApi.list.request({ username }).then((res) => {
state.accounts = res.list;
});
};
return {
...toRefs(state),
search,
handlePageChange,
getAccount,
};
},
});
</script>
<style lang="scss">
</style>

View File

@@ -30,7 +30,6 @@ const viteConfig: UserConfig = {
target: 'http://localhost:8888',
ws: true,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '/'),
},
},
},

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
app:
name: mayfly-go
version: 1.0.0
version: 1.2.0
server:
# debug release test
@@ -29,10 +29,6 @@ jwt:
# 过期时间单位分钟
expire-time: 1440
redis:
host: 127.0.0.1
port: 6379
mysql:
host: localhost:3306
username: root

View File

@@ -5,8 +5,9 @@ go 1.18
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // jwt
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/websocket v1.5.0
github.com/lib/pq v1.10.6
github.com/mojocn/base64Captcha v1.3.5 //
github.com/pkg/sftp v1.13.4
github.com/robfig/cron/v3 v3.0.1 //
@@ -21,6 +22,8 @@ require (
)
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
@@ -39,8 +42,6 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.18.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
@@ -49,10 +50,11 @@ require (
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

View File

@@ -53,6 +53,7 @@ func InitRouter() *gin.Engine {
sys_router.InitResourceRouter(api)
sys_router.InitRoleRouter(api)
sys_router.InitSystemRouter(api)
sys_router.InitSyslogRouter(api)
devops_router.InitProjectRouter(api)
devops_router.InitDbRouter(api)

View File

@@ -0,0 +1,10 @@
package initialize
import (
sys_application "mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
)
func InitSaveLogFunc() ctx.SaveLogFunc {
return sys_application.SyslogApp.SaveFromReq
}

View File

@@ -45,10 +45,12 @@ func (d *Db) Save(rc *ctx.ReqCtx) {
form := &form.DbForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
rc.ReqParam = form
db := new(entity.Db)
utils.Copy(db, form)
// 密码脱敏记录日志
form.Password = "****"
rc.ReqParam = form
db.SetBaseInfo(rc.LoginAccount)
d.DbApp.Save(db)
}
@@ -136,6 +138,8 @@ func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) {
filename := fileheader.Filename
dbId, db := GetIdAndDb(g)
rc.ReqParam = fmt.Sprintf("dbId: %d, db: %s, filename: %s", dbId, db, filename)
go func() {
db := d.DbApp.GetDbInstance(dbId, db)
@@ -184,13 +188,14 @@ func (d *Db) DumpSql(rc *ctx.ReqCtx) {
// 是否需要导出数据
needData := dumpType == "2" || dumpType == "3"
dbInstance := d.DbApp.GetDbInstance(dbId, db)
biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbInstance.ProjectId), "%s")
now := time.Now()
filename := fmt.Sprintf("%s.%s.sql", db, now.Format("200601021504"))
g.Header("Content-Type", "application/octet-stream")
g.Header("Content-Disposition", "attachment; filename="+filename)
rc.ReqParam = fmt.Sprintf("数据库id: %d -- %s", dbId, db)
dbInstance := d.DbApp.GetDbInstance(dbId, db)
writer := g.Writer
writer.WriteString("-- ----------------------------")
writer.WriteString("\n-- 导出平台: mayfly-go")
@@ -222,7 +227,13 @@ func (d *Db) DumpSql(rc *ctx.ReqCtx) {
pageNum++
}
sqlTmp := "SELECT * FROM %s LIMIT %d, %d"
var sqlTmp string
switch dbInstance.Type {
case "mysql":
sqlTmp = "SELECT * FROM %s LIMIT %d, %d"
case "postgres":
sqlTmp = "SELECT * FROM %s OFFSET %d LIMIT %d"
}
for index := 0; index < pageNum; index++ {
sql := fmt.Sprintf(sqlTmp, table, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
columns, result, _ := dbInstance.SelectData(sql)
@@ -250,6 +261,8 @@ func (d *Db) DumpSql(rc *ctx.ReqCtx) {
writer.WriteString("COMMIT;\n")
}
rc.NoRes = true
rc.ReqParam = fmt.Sprintf("dbId: %d, db: %s, tables: %s, dumpType: %s", dbId, db, tablesStr, dumpType)
}
// @router /api/db/:dbId/t-metadata [get]
@@ -301,7 +314,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
columnName := fmt.Sprintf("%s [%s]", v["columnName"], v["columnType"])
comment := v["columnComment"]
// 如果字段备注不为空,则加上备注信息
if comment != "" {
if comment != nil && comment != "" {
columnName = fmt.Sprintf("%s[%s]", columnName, comment)
}

View File

@@ -8,6 +8,7 @@ type DbForm struct {
Port int `binding:"required" json:"port"`
Username string `binding:"required" json:"username"`
Password string `json:"password"`
Params string `json:"params"`
Database string `binding:"required" json:"database"`
ProjectId uint64 `binding:"required" json:"projectId"`
Project string `json:"project"`
@@ -15,6 +16,13 @@ type DbForm struct {
EnvId uint64 `binding:"required" json:"envId"`
}
type DbSqlSaveForm struct {
Name string
Sql string `binding:"required"`
Type int `binding:"required"`
Db string `binding:"required"`
}
// 数据库SQL执行表单
type DbSqlExecForm struct {
Db string `binding:"required" json:"db"` //数据库名

View File

@@ -38,11 +38,9 @@ type MachineScriptForm struct {
Script string `binding:"required"`
}
type DbSqlSaveForm struct {
Name string
Sql string `binding:"required"`
Type int `binding:"required"`
Db string `binding:"required"`
type MachineCreateFileForm struct {
Path string `binding:"required"`
Type string `binding:"required"`
}
type MachineFileUpdateForm struct {

View File

@@ -4,6 +4,7 @@ type Redis struct {
Id uint64
Host string `binding:"required" json:"host"`
Password string `json:"password"`
Mode string `json:"mode"`
Db int `json:"db"`
ProjectId uint64 `binding:"required" json:"projectId"`
Project string `json:"project"`
@@ -30,3 +31,9 @@ type SetValue struct {
KeyInfo
Value []interface{} `binding:"required" json:"value"`
}
type RedisScanForm struct {
Cursor map[string]uint64 `json:"cursor"`
Match string `json:"match"`
Count int64 `json:"count"`
}

View File

@@ -57,6 +57,10 @@ func (m *Machine) SaveMachine(rc *ctx.ReqCtx) {
entity := new(entity.Machine)
utils.Copy(entity, machineForm)
// 密码脱敏记录日志
machineForm.Password = "****"
rc.ReqParam = machineForm
entity.SetBaseInfo(rc.LoginAccount)
m.MachineApp.Save(entity)
}

View File

@@ -60,6 +60,22 @@ func (m *MachineFile) DeleteFile(rc *ctx.ReqCtx) {
/*** sftp相关操作 */
func (m *MachineFile) CreateFile(rc *ctx.ReqCtx) {
g := rc.GinCtx
fid := GetMachineFileId(g)
form := new(form.MachineCreateFileForm)
ginx.BindJsonAndValid(g, form)
path := form.Path
if form.Type == dir {
m.MachineFileApp.MkDir(fid, form.Path)
} else {
m.MachineFileApp.CreateFile(fid, form.Path)
}
rc.ReqParam = fmt.Sprintf("path: %s, type: %s", path, form.Type)
}
func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) {
g := rc.GinCtx
fid := GetMachineFileId(g)
@@ -104,6 +120,7 @@ func (m *MachineFile) GetDirEntry(rc *ctx.ReqCtx) {
Size: fi.Size(),
Path: readPath + fi.Name(),
Type: getFileType(fi.Mode()),
Mode: fi.Mode().String(),
})
}
rc.ResData = fisVO
@@ -155,7 +172,6 @@ func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) {
func (m *MachineFile) RemoveFile(rc *ctx.ReqCtx) {
g := rc.GinCtx
fid := GetMachineFileId(g)
// mid := GetMachineId(g)
path := g.Query("path")
m.MachineFileApp.RemoveFile(fid, path)
@@ -167,7 +183,10 @@ func getFileType(fm fs.FileMode) string {
if fm.IsDir() {
return dir
}
return file
if fm.IsRegular() {
return file
}
return dir
}
func GetMachineFileId(g *gin.Context) uint64 {

View File

@@ -43,7 +43,8 @@ func (p *Project) GetProjects(rc *ctx.ReqCtx) {
func (p *Project) SaveProject(rc *ctx.ReqCtx) {
project := &entity.Project{}
ginx.BindJsonAndValid(rc.GinCtx, project)
rc.ReqParam = project
rc.ReqParam = fmt.Sprintf("projectId: %d, projectName: %s, remark: %s", project.Id, project.Name, project.Remark)
project.SetBaseInfo(rc.LoginAccount)
p.ProjectApp.SaveProject(project)
@@ -81,7 +82,8 @@ func (p *Project) GetProjectMembers(rc *ctx.ReqCtx) {
func (p *Project) SaveProjectMember(rc *ctx.ReqCtx) {
projectMem := &entity.ProjectMember{}
ginx.BindJsonAndValid(rc.GinCtx, projectMem)
rc.ReqParam = projectMem
rc.ReqParam = fmt.Sprintf("projectId: %d, username: %s", projectMem.ProjectId, projectMem.Username)
// 校验账号并赋值username
account := &sys_entity.Account{}

View File

@@ -1,6 +1,7 @@
package api
import (
"context"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
@@ -11,7 +12,10 @@ import (
"mayfly-go/pkg/utils"
"strconv"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v8"
)
type Redis struct {
@@ -32,10 +36,12 @@ func (r *Redis) Save(rc *ctx.ReqCtx) {
form := &form.Redis{}
ginx.BindJsonAndValid(rc.GinCtx, form)
rc.ReqParam = form
redis := new(entity.Redis)
utils.Copy(redis, form)
// 密码脱敏记录日志
form.Password = "****"
rc.ReqParam = form
redis.SetBaseInfo(rc.LoginAccount)
r.RedisApp.Save(redis)
}
@@ -45,7 +51,40 @@ func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) {
}
func (r *Redis) RedisInfo(rc *ctx.ReqCtx) {
res, _ := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(rc.GinCtx, "id"))).Cli.Info().Result()
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
var res string
var err error
ctx := context.Background()
if ri.Mode == "" || ri.Mode == entity.RedisModeStandalone {
res, err = ri.Cli.Info(ctx).Result()
} else if ri.Mode == entity.RedisModeCluster {
host := rc.GinCtx.Query("host")
biz.NotEmpty(host, "集群模式host信息不能为空")
clusterClient := ri.ClusterCli
var redisClient *redis.Client
// 遍历集群的master节点找到该redis client
clusterClient.ForEachMaster(ctx, func(ctx context.Context, client *redis.Client) error {
if host == client.Options().Addr {
redisClient = client
}
return nil
})
if redisClient == nil {
// 遍历集群的slave节点找到该redis client
clusterClient.ForEachSlave(ctx, func(ctx context.Context, client *redis.Client) error {
if host == client.Options().Addr {
redisClient = client
}
return nil
})
}
biz.NotNil(redisClient, "该实例不在该集群中")
res, err = redisClient.Info(ctx).Result()
}
biz.ErrIsNilAppendErr(err, "获取redis info失败: %s")
datas := strings.Split(res, "\r\n")
i := 0
@@ -81,43 +120,125 @@ func (r *Redis) RedisInfo(rc *ctx.ReqCtx) {
rc.ResData = parseMap
}
func (r *Redis) ClusterInfo(rc *ctx.ReqCtx) {
g := rc.GinCtx
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
biz.IsEquals(ri.Mode, entity.RedisModeCluster, "非集群模式")
info, _ := ri.ClusterCli.ClusterInfo(context.Background()).Result()
nodesStr, _ := ri.ClusterCli.ClusterNodes(context.Background()).Result()
nodesRes := make([]map[string]string, 0)
nodes := strings.Split(nodesStr, "\n")
for _, node := range nodes {
if node == "" {
continue
}
nodeInfos := strings.Split(node, " ")
node := make(map[string]string)
node["nodeId"] = nodeInfos[0]
// ip:port1@port2port1指redis服务器与客户端通信的端口port2则是集群内部节点间通信的端口
node["ip"] = nodeInfos[1]
node["flags"] = nodeInfos[2]
// 如果节点是slave并且已知master节点则为master节点ID否则为符号"-"
node["masterSlaveRelation"] = nodeInfos[3]
// 最近一次发送ping的时间这个时间是一个unix毫秒时间戳0代表没有发送过
node["pingSent"] = nodeInfos[4]
// 最近一次收到pong的时间使用unix时间戳表示
node["pongRecv"] = nodeInfos[5]
// 节点的epoch值如果该节点是从节点则为其主节点的epoch值。每当节点发生失败切换时都会创建一个新的独特的递增的epoch。
// 如果多个节点竞争同一个哈希槽时epoch值更高的节点会抢夺到
node["configEpoch"] = nodeInfos[6]
// node-to-node集群总线使用的链接的状态我们使用这个链接与集群中其他节点进行通信.值可以是 connected 和 disconnected
node["linkState"] = nodeInfos[7]
// slave节点没有插槽信息
if len(nodeInfos) > 8 {
// slotmaster节点第9位为哈希槽值或者一个哈希槽范围代表当前节点可以提供服务的所有哈希槽值。如果只是一个值,那就是只有一个槽会被使用。
// 如果是一个范围,这个值表示为起始槽-结束槽,节点将处理包括起始槽和结束槽在内的所有哈希槽。
node["slot"] = nodeInfos[8]
}
nodesRes = append(nodesRes, node)
}
rc.ResData = map[string]interface{}{
"clusterInfo": info,
"clusterNodes": nodesRes,
}
}
// scan获取redis的key列表信息
func (r *Redis) Scan(rc *ctx.ReqCtx) {
g := rc.GinCtx
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
keys, cursor := ri.Scan(uint64(ginx.PathParamInt(g, "cursor")), g.Query("match"), int64(ginx.PathParamInt(g, "count")))
form := &form.RedisScanForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
var keyInfoSplit []string
if len(keys) > 0 {
keyInfoLua := `
local result = {}
-- KEYS[1]为第1个参数lua数组下标从1开始
local ttl = redis.call('ttl', KEYS[1]);
local keyType = redis.call('type', KEYS[1]);
for i = 1, #KEYS do
local ttl = redis.call('ttl', KEYS[i]);
local keyType = redis.call('type', KEYS[i]);
table.insert(result, string.format("%d,%s", ttl, keyType['ok']));
end;
return table.concat(result, ".");`
// 通过lua获取 ttl,type.ttl2,type2格式以便下面切割获取ttl和type。避免多次调用ttl和type函数
keyInfos, _ := ri.Cli.Eval(keyInfoLua, keys).Result()
keyInfoSplit = strings.Split(keyInfos.(string), ".")
}
cmd := ri.GetCmdable()
ctx := context.Background()
kis := make([]*vo.KeyInfo, 0)
for i, k := range keys {
ttlType := strings.Split(keyInfoSplit[i], ",")
ttl, _ := strconv.Atoi(ttlType[0])
ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
kis = append(kis, ki)
var cursorRes map[string]uint64 = make(map[string]uint64)
if ri.Mode == "" || ri.Mode == entity.RedisModeStandalone {
redisAddr := ri.Cli.Options().Addr
keys, cursor := ri.Scan(form.Cursor[redisAddr], form.Match, form.Count)
cursorRes[redisAddr] = cursor
var keyInfoSplit []string
if len(keys) > 0 {
keyInfosLua := `local result = {}
-- KEYS[1]为第1个参数,lua数组下标从1开始
for i = 1, #KEYS do
local ttl = redis.call('ttl', KEYS[i]);
local keyType = redis.call('type', KEYS[i]);
table.insert(result, string.format("%d,%s", ttl, keyType['ok']));
end;
return table.concat(result, ".");`
// 通过lua获取 ttl,type.ttl2,type2格式以便下面切割获取ttl和type。避免多次调用ttl和type函数
keyInfos, err := cmd.Eval(ctx, keyInfosLua, keys).Result()
biz.ErrIsNilAppendErr(err, "执行lua脚本获取key信息失败: %s")
keyInfoSplit = strings.Split(keyInfos.(string), ".")
}
for i, k := range keys {
ttlType := strings.Split(keyInfoSplit[i], ",")
ttl, _ := strconv.Atoi(ttlType[0])
ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
kis = append(kis, ki)
}
} else if ri.Mode == entity.RedisModeCluster {
var keys []string
mu := &sync.Mutex{}
// 遍历所有master节点并执行scan命令合并keys
ri.ClusterCli.ForEachMaster(ctx, func(ctx context.Context, client *redis.Client) error {
redisAddr := client.Options().Addr
ks, cursor, _ := client.Scan(ctx, form.Cursor[redisAddr], form.Match, form.Count).Result()
// 遍历节点的内部回调函数使用异步调用,如不加锁会导致集合并发错误
mu.Lock()
cursorRes[redisAddr] = cursor
keys = append(keys, ks...)
mu.Unlock()
return nil
})
// 因为redis集群模式执行lua脚本key必须位于同一slot中故单机获取的方式不适合
// 使用lua获取key的ttl以及类型减少网络调用
keyInfoLua := `local ttl = redis.call('ttl', KEYS[1]);
local keyType = redis.call('type', KEYS[1]);
return string.format("%d,%s", ttl, keyType['ok'])`
for _, k := range keys {
keyInfo, err := cmd.Eval(ctx, keyInfoLua, []string{k}).Result()
biz.ErrIsNilAppendErr(err, "执行lua脚本获取key信息失败: %s")
ttlType := strings.Split(keyInfo.(string), ",")
ttl, _ := strconv.Atoi(ttlType[0])
ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
kis = append(kis, ki)
}
}
size, _ := ri.Cli.DBSize().Result()
rc.ResData = &vo.Keys{Cursor: cursor, Keys: kis, DbSize: size}
size, _ := cmd.DBSize(context.TODO()).Result()
rc.ResData = &vo.Keys{Cursor: cursorRes, Keys: kis, DbSize: size}
}
func (r *Redis) DeleteKey(rc *ctx.ReqCtx) {
@@ -129,7 +250,7 @@ func (r *Redis) DeleteKey(rc *ctx.ReqCtx) {
biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
rc.ReqParam = key
ri.Cli.Del(key)
ri.GetCmdable().Del(context.Background(), key)
}
func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) {
@@ -145,14 +266,14 @@ func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) {
func (r *Redis) GetStringValue(rc *ctx.ReqCtx) {
ri, key := r.checkKey(rc)
str, err := ri.Cli.Get(key).Result()
str, err := ri.GetCmdable().Get(context.TODO(), key).Result()
biz.ErrIsNilAppendErr(err, "获取字符串值失败: %s")
rc.ResData = str
}
func (r *Redis) GetHashValue(rc *ctx.ReqCtx) {
ri, key := r.checkKey(rc)
res, err := ri.Cli.HGetAll(key).Result()
res, err := ri.GetCmdable().HGetAll(context.TODO(), key).Result()
biz.ErrIsNilAppendErr(err, "获取hash值失败: %s")
rc.ResData = res
}
@@ -165,7 +286,7 @@ func (r *Redis) SetStringValue(rc *ctx.ReqCtx) {
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
str, err := ri.Cli.Set(keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
str, err := ri.GetCmdable().Set(context.TODO(), keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
biz.ErrIsNilAppendErr(err, "保存字符串值失败: %s")
rc.ResData = str
}
@@ -178,21 +299,22 @@ func (r *Redis) SetHashValue(rc *ctx.ReqCtx) {
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
cmd := ri.GetCmdable()
key := hashValue.Key
// 简单处理->先删除,后新增
ri.Cli.Del(key)
cmd.Del(context.TODO(), key)
for _, v := range hashValue.Value {
res := ri.Cli.HSet(key, v["key"].(string), v["value"])
res := cmd.HSet(context.TODO(), key, v["key"].(string), v["value"])
biz.ErrIsNilAppendErr(res.Err(), "保存hash值失败: %s")
}
if hashValue.Timed != -1 {
ri.Cli.Expire(key, time.Second*time.Duration(hashValue.Timed))
cmd.Expire(context.TODO(), key, time.Second*time.Duration(hashValue.Timed))
}
}
func (r *Redis) GetSetValue(rc *ctx.ReqCtx) {
ri, key := r.checkKey(rc)
res, err := ri.Cli.SMembers(key).Result()
res, err := ri.GetCmdable().SMembers(context.TODO(), key).Result()
biz.ErrIsNilAppendErr(err, "获取set值失败: %s")
rc.ResData = res
}
@@ -204,13 +326,14 @@ func (r *Redis) SetSetValue(rc *ctx.ReqCtx) {
ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
cmd := ri.GetCmdable()
key := keyvalue.Key
// 简单处理->先删除,后新增
ri.Cli.Del(key)
ri.Cli.SAdd(key, keyvalue.Value...)
cmd.Del(context.TODO(), key)
cmd.SAdd(context.TODO(), key, keyvalue.Value...)
if keyvalue.Timed != -1 {
ri.Cli.Expire(key, time.Second*time.Duration(keyvalue.Timed))
cmd.Expire(context.TODO(), key, time.Second*time.Duration(keyvalue.Timed))
}
}

View File

@@ -9,6 +9,7 @@ type SelectDataDbVO struct {
Host *string `json:"host"`
Port *int `json:"port"`
Type *string `json:"type"`
Params *string `json:"params"`
Database *string `json:"database"`
Username *string `json:"username"`
ProjectId *int64 `json:"projectId"`

View File

@@ -9,6 +9,8 @@ type Redis struct {
Db int `json:"db"`
ProjectId *int64 `json:"projectId"`
Project *string `json:"project"`
Mode *string `json:"mode"`
Remark *string `json:"remark"`
Env *string `json:"env"`
EnvId *int64 `json:"envId"`
CreateTime *time.Time `json:"createTime"`
@@ -17,9 +19,9 @@ type Redis struct {
}
type Keys struct {
Cursor uint64 `json:"cursor"`
Keys []*KeyInfo `json:"keys"`
DbSize int64 `json:"dbSize"`
Cursor map[string]uint64 `json:"cursor"`
Keys []*KeyInfo `json:"keys"`
DbSize int64 `json:"dbSize"`
}
type KeyInfo struct {

View File

@@ -56,6 +56,7 @@ type MachineFileInfo struct {
Path string `json:"path"`
Size int64 `json:"size"`
Type string `json:"type"`
Mode string `json:"mode"`
}
type RoleVO struct {

View File

@@ -17,6 +17,8 @@ import (
"strings"
"sync"
"time"
_ "github.com/lib/pq"
)
type Db interface {
@@ -228,10 +230,6 @@ func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interfac
if !isSelect && !isShow && !isExplain {
return nil, nil, errors.New("该sql非查询语句")
}
// 没加limit则默认限制50条
if isSelect && !strings.Contains(execSql, "limit") && !strings.Contains(execSql, "LIMIT") {
execSql = execSql + " LIMIT 50"
}
rows, err := d.db.Query(execSql)
if err != nil {
@@ -335,8 +333,21 @@ func (d *DbInstance) Close() {
// 获取dataSourceName
func getDsn(d *entity.Db) string {
var dsn string
if d.Type == "mysql" {
return fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
dsn = fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
if d.Params != "" {
dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
}
return dsn
}
if d.Type == "postgres" {
dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, d.Database)
if d.Params != "" {
dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
}
return dsn
}
return ""
}
@@ -370,7 +381,7 @@ const (
DEFAULT_COLUMN_SIZE = 2000
// mysql 列信息元数据
MYSQL_COLOUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,
MYSQL_COLUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,
column_comment columnComment, column_key columnKey, extra, is_nullable nullable from information_schema.columns
WHERE table_name in (%s) AND table_schema = (SELECT database()) ORDER BY tableName, ordinal_position LIMIT %d, %d`
@@ -379,10 +390,58 @@ const (
WHERE table_name in (%s) AND table_schema = (SELECT database())`
)
const (
// postgres 表信息元数据
PGSQL_TABLE_MA = `SELECT obj_description(c.oid) AS "tableComment", c.relname AS "tableName" FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = (select current_schema()) AND c.reltype > 0`
PGSQL_TABLE_INFO = `SELECT obj_description(c.oid) AS "tableComment", c.relname AS "tableName" FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = (select current_schema()) AND c.reltype > 0`
PGSQL_INDEX_INFO = `SELECT indexname AS "indexName", indexdef AS "indexComment"
FROM pg_indexes WHERE schemaname = (select current_schema()) AND tablename = '%s'`
PGSQL_COLUMN_MA = `SELECT
C.relname AS "tableName",
A.attname AS "columnName",
concat_ws ( '', t.typname, SUBSTRING ( format_type ( a.atttypid, a.atttypmod ) FROM '\(.*\)' ) ) AS "columnType",
d.description AS "columnComment"
FROM
pg_attribute a LEFT JOIN pg_description d ON d.objoid = a.attrelid
AND d.objsubid = A.attnum
LEFT JOIN pg_class c ON A.attrelid = c.oid
LEFT JOIN pg_namespace pn ON c.relnamespace = pn.oid
LEFT JOIN pg_type t ON a.atttypid = t.oid
WHERE
A.attnum >= 0
AND pn.nspname = (select current_schema())
AND C.relname in (%s)
ORDER BY
C.relname DESC,
A.attnum ASC
OFFSET %d LIMIT %d
`
PGSQL_COLUMN_MA_COUNT = `SELECT COUNT(*) "maNum"
FROM
pg_attribute a LEFT JOIN pg_description d ON d.objoid = a.attrelid
AND d.objsubid = A.attnum
LEFT JOIN pg_class c ON A.attrelid = c.oid
LEFT JOIN pg_namespace pn ON c.relnamespace = pn.oid
LEFT JOIN pg_type t ON a.atttypid = t.oid
WHERE
A.attnum >= 0
AND pn.nspname = (select current_schema())
AND C.relname in (%s)
`
)
func (d *DbInstance) GetTableMetedatas() []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = MYSQL_TABLE_MA
} else if d.Type == "postgres" {
sql = PGSQL_TABLE_MA
}
_, res, _ := d.SelectData(sql)
return res
@@ -401,7 +460,10 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]inter
var sqlTmp string
if d.Type == "mysql" {
countSqlTmp = MYSQL_COLOUMN_MA_COUNT
sqlTmp = MYSQL_COLOUMN_MA
sqlTmp = MYSQL_COLUMN_MA
} else if d.Type == "postgres" {
countSqlTmp = PGSQL_COLUMN_MA_COUNT
sqlTmp = PGSQL_COLUMN_MA
}
countSql := fmt.Sprintf(countSqlTmp, tableName)
@@ -433,6 +495,8 @@ func (d *DbInstance) GetTableInfos() []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = MYSQL_TABLE_INFO
} else if d.Type == "postgres" {
sql = PGSQL_TABLE_INFO
}
_, res, _ := d.SelectData(sql)
return res
@@ -442,6 +506,8 @@ func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = fmt.Sprintf(MYSQL_INDEX_INFO, tableName)
} else if d.Type == "postgres" {
sql = fmt.Sprintf(PGSQL_INDEX_INFO, tableName)
}
_, res, _ := d.SelectData(sql)
return res

View File

@@ -1,6 +1,7 @@
package application
import (
"fmt"
"io"
"io/fs"
"mayfly-go/internal/devops/domain/entity"
@@ -30,6 +31,12 @@ type MachineFile interface {
/** sftp 相关操作 **/
// 创建目录
MkDir(fid uint64, path string)
// 创建文件
CreateFile(fid uint64, path string)
// 读取目录
ReadDir(fid uint64, path string) []fs.FileInfo
@@ -100,6 +107,25 @@ func (m *machineFileAppImpl) ReadDir(fid uint64, path string) []fs.FileInfo {
return fis
}
func (m *machineFileAppImpl) MkDir(fid uint64, path string) {
path, machineId := m.checkAndReturnPathMid(fid, path)
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
sftpCli := m.getSftpCli(machineId)
err := sftpCli.Mkdir(path)
biz.ErrIsNilAppendErr(err, "创建目录失败: %s")
}
func (m *machineFileAppImpl) CreateFile(fid uint64, path string) {
path, machineId := m.checkAndReturnPathMid(fid, path)
sftpCli := m.getSftpCli(machineId)
file, err := sftpCli.Create(path)
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
defer file.Close()
}
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) *sftp.File {
path, machineId := m.checkAndReturnPathMid(fileId, path)
sftpCli := m.getSftpCli(machineId)
@@ -148,6 +174,11 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path string) {
fi, _ := file.Stat()
if fi.IsDir() {
err = sftpCli.RemoveDirectory(path)
// 如果文件夹有内容会删除失败则使用rm -rf命令删除
if err != nil {
MachineApp.GetCli(machineId).Run(fmt.Sprintf("rm -rf %s", path))
err = nil
}
} else {
err = sftpCli.Remove(path)
}

View File

@@ -1,6 +1,7 @@
package application
import (
"context"
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
@@ -9,9 +10,10 @@ import (
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
)
type Redis interface {
@@ -102,24 +104,53 @@ func (r *redisAppImpl) GetRedisInstance(id uint64) *RedisInstance {
// 缓存不存在则回调获取redis信息
re := r.GetById(id)
biz.NotNil(re, "redis信息不存在")
redisMode := re.Mode
ri := &RedisInstance{Id: id, ProjectId: re.ProjectId, Mode: redisMode}
if redisMode == "" || redisMode == entity.RedisModeStandalone {
rcli := getRedisCient(re)
// 测试连接
_, e := rcli.Ping(context.Background()).Result()
if e != nil {
rcli.Close()
panic(biz.NewBizErr(fmt.Sprintf("redis连接失败: %s", e.Error())))
}
ri.Cli = rcli
} else if redisMode == entity.RedisModeCluster {
ccli := getRedisClusterClient(re)
// 测试连接
_, e := ccli.Ping(context.Background()).Result()
if e != nil {
ccli.Close()
panic(biz.NewBizErr(fmt.Sprintf("redis集群连接失败: %s", e.Error())))
}
ri.ClusterCli = ccli
}
global.Log.Infof("连接redis: %s", re.Host)
rcli := redis.NewClient(&redis.Options{
Addr: re.Host,
Password: re.Password, // no password set
DB: re.Db, // use default DB
})
// 测试连接
_, e := rcli.Ping().Result()
biz.ErrIsNilAppendErr(e, "redis连接失败: %s")
ri := &RedisInstance{Id: id, ProjectId: re.ProjectId, Cli: rcli}
if needCache {
redisCache.Put(re.Id, ri)
}
return ri
}
func getRedisCient(re *entity.Redis) *redis.Client {
return redis.NewClient(&redis.Options{
Addr: re.Host,
Password: re.Password, // no password set
DB: re.Db, // use default DB
DialTimeout: 8 * time.Second,
})
}
func getRedisClusterClient(re *entity.Redis) *redis.ClusterClient {
return redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(re.Host, ","),
Password: re.Password,
DialTimeout: 8 * time.Second,
})
}
//------------------------------------------------------------------------------
// redis客户端连接缓存30分钟内没有访问则会被关闭
@@ -127,35 +158,64 @@ var redisCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
WithUpdateAccessTime(true).
OnEvicted(func(key interface{}, value interface{}) {
global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %d", key))
value.(*RedisInstance).Cli.Close()
value.(*RedisInstance).Close()
})
// redis实例
type RedisInstance struct {
Id uint64
ProjectId uint64
Cli *redis.Client
}
// 移除redis连接缓存并关闭redis连接
func CloseRedis(id uint64) {
redisCache.Delete(id)
}
func TestRedisConnection(re *entity.Redis) {
rcli := redis.NewClient(&redis.Options{
Addr: re.Host,
Password: re.Password, // no password set
DB: re.Db, // use default DB
})
defer rcli.Close()
var cmd redis.Cmdable
if re.Mode == "" || re.Mode == entity.RedisModeStandalone {
rcli := getRedisCient(re)
defer rcli.Close()
cmd = rcli
} else if re.Mode == entity.RedisModeCluster {
ccli := getRedisClusterClient(re)
defer ccli.Close()
cmd = ccli
}
// 测试连接
_, e := rcli.Ping().Result()
_, e := cmd.Ping(context.Background()).Result()
biz.ErrIsNilAppendErr(e, "Redis连接失败: %s")
}
// redis实例
type RedisInstance struct {
Id uint64
ProjectId uint64
Mode string
Cli *redis.Client
ClusterCli *redis.ClusterClient
}
// 获取命令执行接口的具体实现
func (r *RedisInstance) GetCmdable() redis.Cmdable {
redisMode := r.Mode
if redisMode == "" || redisMode == entity.RedisModeStandalone {
return r.Cli
}
if r.Mode == entity.RedisModeCluster {
return r.ClusterCli
}
return nil
}
func (r *RedisInstance) Scan(cursor uint64, match string, count int64) ([]string, uint64) {
keys, newcursor, err := r.Cli.Scan(cursor, match, count).Result()
keys, newcursor, err := r.GetCmdable().Scan(context.Background(), cursor, match, count).Result()
biz.ErrIsNilAppendErr(err, "scan失败: %s")
return keys, newcursor
}
func (r *RedisInstance) Close() {
if r.Mode == entity.RedisModeStandalone {
r.Cli.Close()
return
}
if r.Mode == entity.RedisModeCluster {
r.ClusterCli.Close()
}
}

View File

@@ -15,6 +15,7 @@ type Db struct {
Username string `orm:"column(username)" json:"username"`
Password string `orm:"column(password)" json:"-"`
Database string `orm:"column(database)" json:"database"`
Params string `json:"params"`
ProjectId uint64
Project string
EnvId uint64

View File

@@ -8,10 +8,17 @@ type Redis struct {
model.Model
Host string `orm:"column(host)" json:"host"`
Mode string `json:"mode"`
Password string `orm:"column(password)" json:"-"`
Db int `orm:"column(database)" json:"db"`
Remark string
ProjectId uint64
Project string
EnvId uint64
Env string
}
const (
RedisModeStandalone = "standalone"
RedisModeCluster = "cluster"
)

View File

@@ -24,14 +24,14 @@ func InitDbRouter(router *gin.RouterGroup) {
rc.Handle(d.Dbs)
})
saveDb := ctx.NewLogInfo("保存数据库信息")
saveDb := ctx.NewLogInfo("保存数据库信息").WithSave(true)
db.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(saveDb).
Handle(d.Save)
})
deleteDb := ctx.NewLogInfo("删除数据库信息")
deleteDb := ctx.NewLogInfo("删除数据库信息").WithSave(true)
db.DELETE(":dbId", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(deleteDb).
@@ -50,20 +50,23 @@ func InitDbRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(d.GetCreateTableDdl)
})
// db.GET(":dbId/exec-sql", controllers.SelectData)
execSqlLog := ctx.NewLogInfo("执行Sql语句")
db.POST(":dbId/exec-sql", func(g *gin.Context) {
rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("执行Sql语句"))
rc := ctx.NewReqCtxWithGin(g).WithLog(execSqlLog)
rc.Handle(d.ExecSql)
})
execSqlFileLog := ctx.NewLogInfo("执行Sql文件").WithSave(true)
db.POST(":dbId/exec-sql-file", func(g *gin.Context) {
rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("执行Sql文件"))
rc.Handle(d.ExecSqlFile)
ctx.NewReqCtxWithGin(g).
WithLog(execSqlFileLog).
Handle(d.ExecSqlFile)
})
dumpLog := ctx.NewLogInfo("导出sql文件").WithSave(true)
db.GET(":dbId/dump", func(g *gin.Context) {
ctx.NewReqCtxWithGin(g).
WithLog(ctx.NewLogInfo("Sql文件dump")).
WithLog(dumpLog).
Handle(d.DumpSql)
})

View File

@@ -29,7 +29,7 @@ func InitMachineRouter(router *gin.RouterGroup) {
})
// 终止进程
killProcessL := ctx.NewLogInfo("终止进程")
killProcessL := ctx.NewLogInfo("终止进程").WithSave(true)
killProcessP := ctx.NewPermission("machine:killprocess")
machines.DELETE(":machineId/process", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
@@ -38,28 +38,28 @@ func InitMachineRouter(router *gin.RouterGroup) {
Handle(m.KillProcess)
})
saveMachine := ctx.NewLogInfo("保存机器信息")
saveMachine := ctx.NewLogInfo("保存机器信息").WithSave(true)
machines.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(saveMachine).
Handle(m.SaveMachine)
})
changeStatus := ctx.NewLogInfo("调整机器状态")
changeStatus := ctx.NewLogInfo("调整机器状态").WithSave(true)
machines.PUT(":machineId/:status", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(changeStatus).
Handle(m.ChangeStatus)
})
delMachine := ctx.NewLogInfo("删除机器")
delMachine := ctx.NewLogInfo("删除机器").WithSave(true)
machines.DELETE(":machineId", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(delMachine).
Handle(m.DeleteMachine)
})
closeCli := ctx.NewLogInfo("关闭机器客户端")
closeCli := ctx.NewLogInfo("关闭机器客户端").WithSave(true)
machines.DELETE(":machineId/close-cli", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(closeCli).Handle(m.CloseCli)
})

View File

@@ -24,7 +24,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
})
// 新增修改机器文件
addFileConf := ctx.NewLogInfo("新增机器文件配置")
addFileConf := ctx.NewLogInfo("新增机器文件配置").WithSave(true)
afcP := ctx.NewPermission("machine:file:add")
machineFile.POST(":machineId/files", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(addFileConf).
@@ -33,7 +33,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
})
// 删除机器文件
delFileConf := ctx.NewLogInfo("删除机器文件配置")
delFileConf := ctx.NewLogInfo("删除机器文件配置").WithSave(true)
dfcP := ctx.NewPermission("machine:file:del")
machineFile.DELETE(":machineId/files/:fileId", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(delFileConf).
@@ -41,19 +41,19 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
Handle(mf.DeleteFile)
})
getContent := ctx.NewLogInfo("读取机器文件内容")
getContent := ctx.NewLogInfo("读取机器文件内容").WithSave(true)
machineFile.GET(":machineId/files/:fileId/read", func(c *gin.Context) {
rc := ctx.NewReqCtxWithGin(c).WithLog(getContent)
rc.Handle(mf.ReadFileContent)
ctx.NewReqCtxWithGin(c).WithLog(getContent).
Handle(mf.ReadFileContent)
})
getDir := ctx.NewLogInfo("读取机器目录")
machineFile.GET(":machineId/files/:fileId/read-dir", func(c *gin.Context) {
rc := ctx.NewReqCtxWithGin(c).WithLog(getDir)
rc.Handle(mf.GetDirEntry)
ctx.NewReqCtxWithGin(c).WithLog(getDir).
Handle(mf.GetDirEntry)
})
writeFile := ctx.NewLogInfo("写入or下载文件内容")
writeFile := ctx.NewLogInfo("写入or下载文件内容").WithSave(true)
wfP := ctx.NewPermission("machine:file:write")
machineFile.POST(":machineId/files/:fileId/write", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(writeFile).
@@ -61,7 +61,14 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
Handle(mf.WriteFileContent)
})
uploadFile := ctx.NewLogInfo("文件上传")
createFile := ctx.NewLogInfo("创建机器文件or目录").WithSave(true)
machineFile.POST(":machineId/files/:fileId/create-file", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(createFile).
WithRequiredPermission(wfP).
Handle(mf.CreateFile)
})
uploadFile := ctx.NewLogInfo("文件上传").WithSave(true)
ufP := ctx.NewPermission("machine:file:upload")
machineFile.POST(":machineId/files/:fileId/upload", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(uploadFile).
@@ -69,7 +76,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
Handle(mf.UploadFile)
})
removeFile := ctx.NewLogInfo("删除文件or文件夹")
removeFile := ctx.NewLogInfo("删除文件or文件夹").WithSave(true)
rfP := ctx.NewPermission("machine:file:rm")
machineFile.DELETE(":machineId/files/:fileId/remove", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(removeFile).

View File

@@ -22,7 +22,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(ms.MachineScripts)
})
saveMachienScriptLog := ctx.NewLogInfo("保存脚本")
saveMachienScriptLog := ctx.NewLogInfo("保存脚本").WithSave(true)
smsP := ctx.NewPermission("machine:script:save")
// 保存脚本
machines.POST(":machineId/scripts", func(c *gin.Context) {
@@ -31,7 +31,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
Handle(ms.SaveMachineScript)
})
deleteLog := ctx.NewLogInfo("删除脚本")
deleteLog := ctx.NewLogInfo("删除脚本").WithSave(true)
dP := ctx.NewPermission("machine:script:del")
// 保存脚本
machines.DELETE(":machineId/scripts/:scriptId", func(c *gin.Context) {
@@ -40,7 +40,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
Handle(ms.DeleteMachineScript)
})
runLog := ctx.NewLogInfo("执行机器脚本")
runLog := ctx.NewLogInfo("执行机器脚本").WithSave(true)
rP := ctx.NewPermission("machine:script:run")
// 运行脚本
machines.GET(":machineId/scripts/:scriptId/run", func(c *gin.Context) {

View File

@@ -25,7 +25,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(m.GetProjects)
})
saveProjectLog := ctx.NewLogInfo("保存项目信息")
saveProjectLog := ctx.NewLogInfo("保存项目信息").WithSave(true)
savePP := ctx.NewPermission("project:save")
// 保存项目下的环境信息
project.POST("", func(c *gin.Context) {
@@ -34,7 +34,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
Handle(m.SaveProject)
})
delProjectLog := ctx.NewLogInfo("删除项目信息")
delProjectLog := ctx.NewLogInfo("删除项目信息").WithSave(true)
delPP := ctx.NewPermission("project:del")
// 删除项目
project.DELETE("", func(c *gin.Context) {
@@ -48,7 +48,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(m.GetProjectEnvs)
})
saveProjectEnvLog := ctx.NewLogInfo("新增项目环境信息")
saveProjectEnvLog := ctx.NewLogInfo("新增项目环境信息").WithSave(true)
savePeP := ctx.NewPermission("project:env:add")
// 保存项目下的环境信息
project.POST("/:projectId/envs", func(c *gin.Context) {
@@ -63,7 +63,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
})
// 保存项目成员
saveProjectMemLog := ctx.NewLogInfo("新增项目成员")
saveProjectMemLog := ctx.NewLogInfo("新增项目成员").WithSave(true)
savePmP := ctx.NewPermission("project:member:add")
project.POST("/:projectId/members", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(saveProjectMemLog).
@@ -72,7 +72,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
})
// 删除项目成员
delProjectMemLog := ctx.NewLogInfo("删除项目成员")
delProjectMemLog := ctx.NewLogInfo("删除项目成员").WithSave(true)
savePmdP := ctx.NewPermission("project:member:del")
project.DELETE("/:projectId/members/:accountId", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(delProjectMemLog).

View File

@@ -21,12 +21,12 @@ func InitRedisRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(rs.RedisList)
})
save := ctx.NewLogInfo("保存redis信息")
save := ctx.NewLogInfo("保存redis信息").WithSave(true)
redis.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save)
})
delRedis := ctx.NewLogInfo("删除redis信息")
delRedis := ctx.NewLogInfo("删除redis信息").WithSave(true)
redis.DELETE(":id", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis)
})
@@ -35,14 +35,17 @@ func InitRedisRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(rs.RedisInfo)
})
redis.GET(":id/cluster-info", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).Handle(rs.ClusterInfo)
})
// 获取指定redis keys
redis.GET(":id/scan/:cursor/:count", func(c *gin.Context) {
redis.POST(":id/scan", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).Handle(rs.Scan)
})
// 删除key
deleteKeyL := ctx.NewLogInfo("redis删除key")
// deleteKey := ctx.NewPermission("project:save")
deleteKeyL := ctx.NewLogInfo("redis删除key").WithSave(true)
redis.DELETE(":id/key", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(deleteKeyL).Handle(rs.DeleteKey)
})

View File

@@ -10,6 +10,7 @@ import (
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils"
"strconv"
"strings"
@@ -29,7 +30,6 @@ type Account struct {
func (a *Account) Login(rc *ctx.ReqCtx) {
loginForm := &form.LoginForm{}
ginx.BindJsonAndValid(rc.GinCtx, loginForm)
rc.ReqParam = loginForm.Username
// 校验验证码
biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
@@ -54,8 +54,13 @@ func (a *Account) Login(rc *ctx.ReqCtx) {
// 保存该账号的权限codes
ctx.SavePermissionCodes(account.Id, permissions)
clientIp := rc.GinCtx.ClientIP()
// 保存登录消息
go a.saveLogin(account, rc.GinCtx.ClientIP())
go a.saveLogin(account, clientIp)
rc.ReqParam = fmt.Sprintln("登录ip: ", clientIp)
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
rc.LoginAccount = &model.LoginAccount{Id: account.Id, Username: account.Username}
rc.ResData = map[string]interface{}{
"token": ctx.CreateToken(account.Id, account.Username),

View File

@@ -29,6 +29,7 @@ func (r *Role) SaveRole(rc *ctx.ReqCtx) {
g := rc.GinCtx
form := &form.RoleForm{}
ginx.BindJsonAndValid(g, form)
rc.ReqParam = form
role := new(entity.Role)
utils.Copy(role, form)

View File

@@ -0,0 +1,21 @@
package api
import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
)
type Syslog struct {
SyslogApp application.Syslog
}
func (r *Syslog) Syslogs(rc *ctx.ReqCtx) {
g := rc.GinCtx
condition := &entity.Syslog{
Type: int8(ginx.QueryInt(g, "type", 0)),
CreatorId: uint64(ginx.QueryInt(g, "creatorId", 0)),
}
rc.ResData = r.SyslogApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Syslog), "create_time DESC")
}

View File

@@ -0,0 +1,79 @@
package application
import (
"encoding/json"
"fmt"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/internal/sys/infrastructure/persistence"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils"
"reflect"
"time"
)
type Syslog interface {
GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
// 从请求上下文的参数保存系统日志
SaveFromReq(req *ctx.ReqCtx)
}
type syslogAppImpl struct {
syslogRepo repository.Syslog
}
// 实现类单例
var SyslogApp Syslog = &syslogAppImpl{
syslogRepo: persistence.SyslogDao,
}
func (m *syslogAppImpl) GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return m.syslogRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func (m *syslogAppImpl) SaveFromReq(req *ctx.ReqCtx) {
lg := req.LoginAccount
if lg == nil {
return
}
syslog := new(entity.Syslog)
syslog.CreateTime = time.Now()
syslog.Creator = lg.Username
syslog.CreatorId = lg.Id
syslog.Description = req.LogInfo.Description
if req.LogInfo.LogResp {
respB, _ := json.Marshal(req.ResData)
syslog.Resp = string(respB)
}
reqParam := req.ReqParam
if !utils.IsBlank(reflect.ValueOf(reqParam)) {
// 如果是字符串类型则不使用json序列化
if reqStr, ok := reqParam.(string); ok {
syslog.ReqParam = reqStr
} else {
reqB, _ := json.Marshal(reqParam)
syslog.ReqParam = string(reqB)
}
}
if err := req.Err; err != nil {
syslog.Type = entity.SyslogTypeError
var errMsg string
switch t := err.(type) {
case *biz.BizError:
errMsg = fmt.Sprintf("errCode: %d, errMsg: %s", t.Code(), t.Error())
case error:
errMsg = t.Error()
}
syslog.Resp = errMsg
} else {
syslog.Type = entity.SyslogTypeNorman
}
m.syslogRepo.Insert(syslog)
}

View File

@@ -0,0 +1,25 @@
package entity
import "time"
// 系统操作日志
type Syslog struct {
Id uint64 `json:"id"`
CreateTime time.Time `json:"createTime"`
CreatorId uint64 `json:"creatorId"`
Creator string `json:"creator"`
Type int8 `json:"type"`
Description string `json:"description"`
ReqParam string `json:"reqParam"` // 请求参数
Resp string `json:"resp"` // 响应结构
}
func (a *Syslog) TableName() string {
return "t_sys_log"
}
const (
SyslogTypeNorman int8 = 1 // 正常状态
SyslogTypeError int8 = 2 // 错误状态
)

View File

@@ -0,0 +1,12 @@
package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/model"
)
type Syslog interface {
GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
Insert(log *entity.Syslog)
}

View File

@@ -0,0 +1,19 @@
package persistence
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/model"
)
type syslogRepo struct{}
var SyslogDao repository.Syslog = &syslogRepo{}
func (m *syslogRepo) GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
func (m *syslogRepo) Insert(syslog *entity.Syslog) {
model.Insert(syslog)
}

View File

@@ -18,9 +18,12 @@ func InitAccountRouter(router *gin.RouterGroup) {
}
{
// 用户登录
loginLog := ctx.NewLogInfo("用户登录").WithSave(true)
account.POST("login", func(g *gin.Context) {
rc := ctx.NewReqCtxWithGin(g).WithNeedToken(false).WithLog(ctx.NewLogInfo("用户登录"))
rc.Handle(a.Login)
ctx.NewReqCtxWithGin(g).
WithNeedToken(false).
WithLog(loginLog).
Handle(a.Login)
})
// 获取个人账号信息
@@ -44,7 +47,7 @@ func InitAccountRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(a.Accounts)
})
createAccount := ctx.NewLogInfo("创建账号")
createAccount := ctx.NewLogInfo("创建账号").WithSave(true)
addAccountPermission := ctx.NewPermission("account:add")
account.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
@@ -53,14 +56,14 @@ func InitAccountRouter(router *gin.RouterGroup) {
Handle(a.CreateAccount)
})
changeStatus := ctx.NewLogInfo("修改账号状态")
changeStatus := ctx.NewLogInfo("修改账号状态").WithSave(true)
account.PUT("change-status/:id/:status", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(changeStatus).
Handle(a.ChangeStatus)
})
delAccount := ctx.NewLogInfo("删除账号")
delAccount := ctx.NewLogInfo("删除账号").WithSave(true)
delAccountPermission := ctx.NewPermission("account:del")
account.DELETE(":id", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
@@ -75,7 +78,7 @@ func InitAccountRouter(router *gin.RouterGroup) {
})
// 保存用户角色
saveAccountRole := ctx.NewLogInfo("保存用户角色")
saveAccountRole := ctx.NewLogInfo("保存用户角色").WithSave(true)
sarPermission := ctx.NewPermission("account:saveRoles")
account.POST("/roles", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(saveAccountRole).

View File

@@ -12,10 +12,6 @@ func InitResourceRouter(router *gin.RouterGroup) {
r := &api.Resource{ResourceApp: application.ResourceApp}
db := router.Group("sys/resources")
{
// db.GET("/account", func(c *gin.Context) {
// ctx.NewReqCtxWithGin(c).Handle(r.ResourceTree)
// })
db.GET("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).Handle(r.GetAllResourceTree)
})
@@ -24,7 +20,7 @@ func InitResourceRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(r.GetById)
})
saveResource := ctx.NewLogInfo("保存资源")
saveResource := ctx.NewLogInfo("保存资源").WithSave(true)
srPermission := ctx.NewPermission("resource:add")
db.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
@@ -33,7 +29,7 @@ func InitResourceRouter(router *gin.RouterGroup) {
Handle(r.SaveResource)
})
changeStatus := ctx.NewLogInfo("修改资源状态")
changeStatus := ctx.NewLogInfo("修改资源状态").WithSave(true)
csPermission := ctx.NewPermission("resource:changeStatus")
db.PUT(":id/:status", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
@@ -42,7 +38,7 @@ func InitResourceRouter(router *gin.RouterGroup) {
Handle(r.ChangeStatus)
})
delResource := ctx.NewLogInfo("删除资源")
delResource := ctx.NewLogInfo("删除资源").WithSave(true)
dePermission := ctx.NewPermission("resource:delete")
db.DELETE(":id", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).

View File

@@ -20,7 +20,7 @@ func InitRoleRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(r.Roles)
})
saveRole := ctx.NewLogInfo("保存角色")
saveRole := ctx.NewLogInfo("保存角色").WithSave(true)
sPermission := ctx.NewPermission("role:add")
db.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(saveRole).
@@ -28,7 +28,7 @@ func InitRoleRouter(router *gin.RouterGroup) {
Handle(r.SaveRole)
})
delRole := ctx.NewLogInfo("删除角色")
delRole := ctx.NewLogInfo("删除角色").WithSave(true)
drPermission := ctx.NewPermission("role:del")
db.DELETE(":id", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(delRole).
@@ -44,7 +44,7 @@ func InitRoleRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).Handle(r.RoleResource)
})
saveResource := ctx.NewLogInfo("保存角色资源")
saveResource := ctx.NewLogInfo("保存角色资源").WithSave(true)
srPermission := ctx.NewPermission("role:saveResources")
db.POST(":id/resources", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(saveResource).

View File

@@ -0,0 +1,21 @@
package router
import (
"mayfly-go/internal/sys/api"
"mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
)
func InitSyslogRouter(router *gin.RouterGroup) {
s := &api.Syslog{
SyslogApp: application.SyslogApp,
}
sys := router.Group("syslogs")
{
sys.GET("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).Handle(s.Syslogs)
})
}
}

View File

@@ -22,12 +22,13 @@ DROP TABLE IF EXISTS `t_db`;
CREATE TABLE `t_db` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
`host` varchar(20) COLLATE utf8mb4_bin NOT NULL,
`host` varchar(50) COLLATE utf8mb4_bin NOT NULL,
`port` int(8) NOT NULL,
`username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
`type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
`database` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
`params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
`network` varchar(8) COLLATE utf8mb4_bin DEFAULT NULL,
`project_id` bigint(20) DEFAULT NULL,
`project` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
@@ -103,7 +104,7 @@ CREATE TABLE `t_machine` (
`project_id` bigint(20) DEFAULT NULL,
`project_name` varchar(36) COLLATE utf8mb4_bin DEFAULT NULL,
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`ip` varchar(18) COLLATE utf8mb4_bin NOT NULL,
`ip` varchar(36) COLLATE utf8mb4_bin NOT NULL,
`port` int(12) NOT NULL,
`username` varchar(12) COLLATE utf8mb4_bin NOT NULL,
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
@@ -253,9 +254,11 @@ CREATE TABLE `t_project_member` (
DROP TABLE IF EXISTS `t_redis`;
CREATE TABLE `t_redis` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`host` varchar(32) COLLATE utf8mb4_bin NOT NULL,
`host` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`db` int(32) DEFAULT NULL,
`mode` varchar(32) DEFAULT NULL,
`remark` varchar(125) DEFAULT NULL,
`project_id` bigint(20) DEFAULT NULL,
`project` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`env_id` bigint(20) DEFAULT NULL,
@@ -360,12 +363,11 @@ CREATE TABLE `t_sys_resource` (
-- Records of t_sys_resource
-- ----------------------------
BEGIN;
INSERT INTO `t_sys_resource` VALUES (1, 0, 1, 1, '首页', '/home', 1, '{\"component\":\"Home\",\"icon\":\"Home\",\"isAffix\":true,\"isKeepAlive\":true,\"routeName\":\"Home\"}', 1, 'admin', 1, 'admin', '2021-05-25 16:44:41', '2021-05-27 09:12:56');
INSERT INTO `t_sys_resource` VALUES (1, 0, 1, 1, '首页', '/home', 1, '{\"component\":\"Home\",\"icon\":\"HomeFilled\",\"isAffix\":true,\"isKeepAlive\":true,\"routeName\":\"Home\"}', 1, 'admin', 1, 'admin', '2021-05-25 16:44:41', '2021-05-27 09:12:56');
INSERT INTO `t_sys_resource` VALUES (2, 0, 1, 1, '运维', '/ops', 3, '{\"icon\":\"Monitor\",\"isKeepAlive\":true,\"redirect\":\"machine/list\",\"routeName\":\"Ops\"}', 1, 'admin', 1, 'admin', '2021-05-25 16:48:16', '2021-06-08 14:20:24');
INSERT INTO `t_sys_resource` VALUES (3, 2, 1, 1, '机器列表', 'machines', 2, '{\"component\":\"MachineList\",\"icon\":\"Menu\",\"isKeepAlive\":true,\"routeName\":\"MachineList\"}', 2, 'admin', 1, 'admin', '2021-05-25 16:50:04', '2021-06-30 16:20:08');
INSERT INTO `t_sys_resource` VALUES (4, 0, 1, 1, '系统管理', '/sys', 5, '{\"icon\":\"Setting\",\"isKeepAlive\":true,\"redirect\":\"/sys/resources\",\"routeName\":\"sys\"}', 1, 'admin', 1, 'admin', '2021-05-26 15:20:20', '2021-07-29 18:03:06');
INSERT INTO `t_sys_resource` VALUES (5, 4, 1, 1, '资源管理', 'resources', 3, '{\"component\":\"ResourceList\",\"icon\":\"Menu\",\"isKeepAlive\":true,\"routeName\":\"ResourceList\"}', 1, 'admin', 1, 'admin', '2021-05-26 15:23:07', '2021-06-08 11:27:55');
INSERT INTO `t_sys_resource` VALUES (9, 0, 1, 1, 'iframes', '/iframes', 6, '{\"component\":\"RouterParent\",\"icon\":\"Menu\",\"isIframe\":true,\"isKeepAlive\":true,\"link\":\"https://www.baidu.com\",\"routeName\":\"Iframe\"}', 1, 'admin', 1, 'admin', '2021-05-27 09:58:37', '2021-07-29 18:03:13');
INSERT INTO `t_sys_resource` VALUES (11, 4, 1, 1, '角色管理', 'roles', 2, '{\"component\":\"RoleList\",\"icon\":\"Menu\",\"isKeepAlive\":true,\"routeName\":\"RoleList\"}', 1, 'admin', 1, 'admin', '2021-05-27 11:15:35', '2021-06-03 09:59:41');
INSERT INTO `t_sys_resource` VALUES (12, 3, 2, 1, '机器终端按钮', 'machine:terminal', 4, '', 1, 'admin', 1, 'admin', '2021-05-28 14:06:02', '2021-05-31 17:47:59');
INSERT INTO `t_sys_resource` VALUES (14, 4, 1, 1, '账号管理', 'accounts', 1, '{\"component\":\"AccountList\",\"icon\":\"Menu\",\"isKeepAlive\":true,\"routeName\":\"AccountList\"}', 1, 'admin', 1, 'admin', '2021-05-28 14:56:25', '2021-06-03 09:39:22');
@@ -380,7 +382,7 @@ INSERT INTO `t_sys_resource` VALUES (22, 11, 2, 1, '角色删除按钮', 'role:d
INSERT INTO `t_sys_resource` VALUES (23, 11, 2, 1, '角色新增按钮', 'role:add', 3, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:02:44', '2021-05-31 19:33:39');
INSERT INTO `t_sys_resource` VALUES (24, 11, 2, 1, '角色编辑按钮', 'role:update', 4, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:02:57', '2021-05-31 19:33:40');
INSERT INTO `t_sys_resource` VALUES (25, 5, 2, 1, '资源新增按钮', 'resource:add', 1, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:03:33', '2021-05-31 19:31:47');
INSERT INTO `t_sys_resource` VALUES (26, 5, 2, 1, '资源删除按钮', 'resource:del', 2, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:03:47', '2021-05-31 19:29:40');
INSERT INTO `t_sys_resource` VALUES (26, 5, 2, 1, '资源删除按钮', 'resource:delete', 2, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:03:47', '2021-05-31 19:29:40');
INSERT INTO `t_sys_resource` VALUES (27, 5, 2, 1, '资源编辑按钮', 'resource:update', 3, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:04:03', '2021-05-31 19:29:40');
INSERT INTO `t_sys_resource` VALUES (28, 5, 2, 1, '资源禁用启用按钮', 'resource:changeStatus', 4, NULL, 1, 'admin', 1, 'admin', '2021-05-31 18:04:33', '2021-05-31 18:04:33');
INSERT INTO `t_sys_resource` VALUES (29, 14, 2, 1, '账号添加按钮', 'account:add', 3, NULL, 1, 'admin', 1, 'admin', '2021-05-31 19:23:42', '2021-05-31 19:23:42');
@@ -426,6 +428,8 @@ INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `wei
INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `weight`, `meta`, `creator_id`, `creator`, `modifier_id`, `modifier`, `create_time`, `update_time`) VALUES (81, 80, 2, 1, '基本权限', 'mongo:base', 1, 'null', 1, 'admin', 1, 'admin', '2022-05-13 14:04:16', '2022-05-13 14:04:16');
INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `weight`, `meta`, `creator_id`, `creator`, `modifier_id`, `modifier`, `create_time`, `update_time`) VALUES (82, 79, 1, 1, 'Mongo管理', 'mongo-manage', 2, '{\"component\":\"MongoList\",\"icon\":\"Menu\",\"isKeepAlive\":true,\"routeName\":\"MongoList\"}', 1, 'admin', 1, 'admin', '2022-05-16 18:13:06', '2022-05-16 18:13:06');
INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `weight`, `meta`, `creator_id`, `creator`, `modifier_id`, `modifier`, `create_time`, `update_time`) VALUES (83, 82, 2, 1, '基本权限', 'mongo:manage:base', 1, 'null', 1, 'admin', 1, 'admin', '2022-05-16 18:13:25', '2022-05-16 18:13:25');
INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `weight`, `meta`, `creator_id`, `creator`, `modifier_id`, `modifier`, `create_time`, `update_time`) VALUES (84, 4, 1, 1, '操作日志', 'syslogs', 4, '{\"component\":\"SyslogList\",\"icon\":\"Tickets\",\"routeName\":\"SyslogList\"}', 1, 'admin', 1, 'admin', '2022-07-13 19:57:07', '2022-07-13 22:58:19');
INSERT INTO `t_sys_resource`(`id`, `pid`, `type`, `status`, `name`, `code`, `weight`, `meta`, `creator_id`, `creator`, `modifier_id`, `modifier`, `create_time`, `update_time`) VALUES (85, 84, 2, 1, '操作日志基本权限', 'syslog', 1, 'null', 1, 'admin', 1, 'admin', '2022-07-13 19:57:55', '2022-07-13 19:57:55');
COMMIT;
-- ----------------------------
@@ -622,8 +626,34 @@ INSERT INTO `t_sys_role_resource` VALUES (496, 8, 61, 1, 'admin', '2021-11-05 15
INSERT INTO `t_sys_role_resource` VALUES (497, 8, 62, 1, 'admin', '2021-11-05 15:59:16');
INSERT INTO `t_sys_role_resource` VALUES (498, 8, 63, 1, 'admin', '2021-11-05 15:59:16');
INSERT INTO `t_sys_role_resource` VALUES (499, 8, 64, 1, 'admin', '2021-11-05 15:59:16');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (500, 1, 72, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (501, 1, 71, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (502, 1, 79, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (503, 1, 80, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (504, 1, 81, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (505, 1, 82, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (506, 1, 83, 1, 'admin', '2022-07-14 11:03:09');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (507, 1, 84, 1, 'admin', '2022-07-14 11:10:11');
INSERT INTO `t_sys_role_resource`(`id`, `role_id`, `resource_id`, `creator_id`, `creator`, `create_time`) VALUES (508, 1, 85, 1, 'admin', '2022-07-14 11:10:11');
COMMIT;
-- ----------------------------
-- 表结构: t_sys_log
-- ----------------------------
DROP TABLE IF EXISTS `t_sys_log`;
CREATE TABLE `t_sys_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`type` tinyint(4) NOT NULL COMMENT '类型',
`description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '描述',
`req_param` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '请求信息',
`resp` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '响应信息',
`creator` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '调用者',
`creator_id` bigint(20) NOT NULL COMMENT '调用者id',
`create_time` datetime NOT NULL COMMENT '操作时间',
PRIMARY KEY (`id`),
KEY `idx_creator_id` (`creator_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='系统操作日志';
-- ----------------------------
-- Table structure for t_mongo

View File

@@ -12,20 +12,38 @@ import (
"github.com/sirupsen/logrus"
)
type SaveLogFunc func(*ReqCtx)
var saveLog SaveLogFunc
// 设置保存日志处理函数
func SetSaveLogFunc(sl SaveLogFunc) {
saveLog = sl
}
type LogInfo struct {
LogResp bool // 是否记录返回结果
Description string // 请求描述
Save bool // 是否保存日志
}
// 新建日志信息
func NewLogInfo(description string) *LogInfo {
return &LogInfo{Description: description, LogResp: false}
}
// 是否记录返回结果
func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
i.LogResp = logResp
return i
}
// 是否保存日志
func (i *LogInfo) WithSave(saveLog bool) *LogInfo {
i.Save = saveLog
return i
}
func LogHandler(rc *ReqCtx) error {
li := rc.LogInfo
if li == nil {
@@ -41,6 +59,10 @@ func LogHandler(rc *ReqCtx) error {
req := rc.GinCtx.Request
lfs[req.Method] = req.URL.Path
// 如果需要保存日志,并且保存日志处理函数存在则执行保存日志函数
if li.Save && saveLog != nil {
go saveLog(rc)
}
if err := rc.Err; err != nil {
logger.Log.WithFields(lfs).Error(getErrMsg(rc, err))
return nil

View File

@@ -53,7 +53,7 @@ func (rc *ReqCtx) Handle(handler HandlerFunc) {
begin := time.Now()
handler(rc)
rc.timed = time.Now().Sub(begin).Milliseconds()
rc.timed = time.Since(begin).Milliseconds()
if !rc.NoRes {
ginx.SuccessRes(ginCtx, rc.ResData)
}

View File

@@ -1,7 +1,7 @@
package global
import (
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

View File

@@ -1,10 +1,11 @@
package rediscli
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
)
var cli *redis.Client
@@ -19,7 +20,7 @@ func GetCli() *redis.Client {
// get key value
func Get(key string) string {
val, err := cli.Get(key).Result()
val, err := cli.Get(context.TODO(), key).Result()
switch {
case err == redis.Nil:
fmt.Println("key does not exist")
@@ -33,32 +34,32 @@ func Get(key string) string {
// set key value
func Set(key string, val string, expiration time.Duration) {
cli.Set(key, val, expiration)
cli.Set(context.TODO(), key, val, expiration)
}
func HSet(key string, field string, val interface{}) {
cli.HSet(key, field, val)
cli.HSet(context.TODO(), key, field, val)
}
// hget
func HGet(key string, field string) string {
val, _ := cli.HGet(key, field).Result()
val, _ := cli.HGet(context.TODO(), key, field).Result()
return val
}
// hget
func HExist(key string, field string) bool {
val, _ := cli.HExists(key, field).Result()
val, _ := cli.HExists(context.TODO(), key, field).Result()
return val
}
// hgetall
func HGetAll(key string) map[string]string {
vals, _ := cli.HGetAll(key).Result()
vals, _ := cli.HGetAll(context.TODO(), key).Result()
return vals
}
// hdel
func HDel(key string, fields ...string) int {
return int(cli.HDel(key, fields...).Val())
return int(cli.HDel(context.TODO(), key, fields...).Val())
}

View File

@@ -1,11 +1,12 @@
package starter
import (
"context"
"fmt"
"mayfly-go/pkg/config"
"mayfly-go/pkg/global"
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
)
func InitRedis() {
@@ -26,7 +27,7 @@ func ConnRedis() *redis.Client {
DB: redisConf.Db, // use default DB
})
// 测试连接
_, e := rdb.Ping().Result()
_, e := rdb.Ping(context.TODO()).Result()
if e != nil {
global.Log.Panic(fmt.Sprintf("连接redis失败! [%s:%d]", redisConf.Host, redisConf.Port))
}

View File

@@ -13,6 +13,9 @@ func RunWebServer() {
ctx.UseBeforeHandlerInterceptor(ctx.PermissionHandler)
// 日志处理器
ctx.UseAfterHandlerInterceptor(ctx.LogHandler)
// 设置日志保存函数
ctx.SetSaveLogFunc(initialize.InitSaveLogFunc())
// 注册路由
web := initialize.InitRouter()

View File

@@ -1,7 +1,8 @@
后端配置:
服务端口mysql等信息在config.yml里配置即可。
前端配置:
static/config.js中的api地址配成启动后的后端服务的真实地址即可
相关配置文件:
后端:
config.yml: 服务端口mysql等信息在此配置即可。
前端:
static/config.js: 若前后端分开部署则将该文件中的api地址配成后端服务的真实地址即可否则无需修改。
服务启动:./startup.sh
服务关闭:./shutdown.sh