2022-05-17 20:23:08 +08:00
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/internal/common/consts"
|
2024-04-27 01:35:21 +08:00
|
|
|
|
"mayfly-go/internal/event"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/mongo/api/form"
|
2024-02-29 22:12:50 +08:00
|
|
|
|
"mayfly-go/internal/mongo/api/vo"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/mongo/application"
|
|
|
|
|
|
"mayfly-go/internal/mongo/domain/entity"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2024-02-29 22:12:50 +08:00
|
|
|
|
tagentity "mayfly-go/internal/tag/domain/entity"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
2024-04-27 01:35:21 +08:00
|
|
|
|
"mayfly-go/pkg/global"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-10-12 12:14:56 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2022-08-26 09:58:01 +08:00
|
|
|
|
"regexp"
|
2022-05-17 20:23:08 +08:00
|
|
|
|
"strconv"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"strings"
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Mongo struct {
|
2024-01-21 22:52:20 +08:00
|
|
|
|
MongoApp application.Mongo `inject:""`
|
|
|
|
|
|
TagApp tagapp.TagTree `inject:"TagTreeApp"`
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) Mongos(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
queryCond, page := req.BindQueryAndPage[*entity.MongoQuery](rc, new(entity.MongoQuery))
|
2022-10-26 20:49:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 不存在可访问标签id,即没有可操作数据
|
2024-04-12 13:24:20 +08:00
|
|
|
|
codes := m.TagApp.GetAccountTagCodes(rc.GetLoginAccount().Id, consts.ResourceTypeMongo, queryCond.TagPath)
|
2023-12-05 23:03:51 +08:00
|
|
|
|
if len(codes) == 0 {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
rc.ResData = model.EmptyPageResult[any]()
|
2022-10-26 20:49:29 +08:00
|
|
|
|
return
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
queryCond.Codes = codes
|
2023-07-08 20:05:55 +08:00
|
|
|
|
|
2024-02-29 22:12:50 +08:00
|
|
|
|
var mongovos []*vo.Mongo
|
|
|
|
|
|
res, err := m.MongoApp.GetPageList(queryCond, page, &mongovos)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
2024-02-29 22:12:50 +08:00
|
|
|
|
|
|
|
|
|
|
// 填充标签信息
|
2024-04-12 13:24:20 +08:00
|
|
|
|
m.TagApp.FillTagInfo(tagentity.TagType(consts.ResourceTypeMongo), collx.ArrayMap(mongovos, func(mvo *vo.Mongo) tagentity.ITagResource {
|
2024-02-29 22:12:50 +08:00
|
|
|
|
return mvo
|
|
|
|
|
|
})...)
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
rc.ResData = res
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
func (m *Mongo) TestConn(rc *req.Ctx) {
|
|
|
|
|
|
form := &form.Mongo{}
|
2024-02-24 16:30:29 +08:00
|
|
|
|
mongo := req.BindJsonAndCopyTo[*entity.Mongo](rc, form, new(entity.Mongo))
|
2023-11-12 20:14:44 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(m.MongoApp.TestConn(mongo), "连接失败: %s")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) Save(rc *req.Ctx) {
|
2022-05-17 20:23:08 +08:00
|
|
|
|
form := &form.Mongo{}
|
2024-02-24 16:30:29 +08:00
|
|
|
|
mongo := req.BindJsonAndCopyTo[*entity.Mongo](rc, form, new(entity.Mongo))
|
2022-08-26 09:58:01 +08:00
|
|
|
|
|
2022-08-25 17:58:07 +08:00
|
|
|
|
// 密码脱敏记录日志
|
|
|
|
|
|
form.Uri = func(str string) string {
|
|
|
|
|
|
reg := regexp.MustCompile(`(^mongodb://.+?:)(.+)(@.+$)`)
|
|
|
|
|
|
return reg.ReplaceAllString(str, `${1}****${3}`)
|
|
|
|
|
|
}(form.Uri)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
rc.ReqParam = form
|
|
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
biz.ErrIsNil(m.MongoApp.SaveMongo(rc.MetaCtx, mongo, form.TagCodePaths...))
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) DeleteMongo(rc *req.Ctx) {
|
2024-02-25 12:46:18 +08:00
|
|
|
|
idsStr := rc.PathParam("id")
|
2023-07-01 14:34:42 +08:00
|
|
|
|
rc.ReqParam = idsStr
|
|
|
|
|
|
ids := strings.Split(idsStr, ",")
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range ids {
|
|
|
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
|
2023-11-07 21:05:21 +08:00
|
|
|
|
m.MongoApp.Delete(rc.MetaCtx, uint64(value))
|
2023-07-01 14:34:42 +08:00
|
|
|
|
}
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) Databases(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
res, err := conn.Cli.ListDatabases(context.TODO(), bson.D{})
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取mongo所有库信息失败: %s")
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) Collections(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
2024-04-27 01:35:21 +08:00
|
|
|
|
|
2024-05-08 21:04:25 +08:00
|
|
|
|
global.EventBus.Publish(rc.MetaCtx, event.EventTopicResourceOp, conn.Info.CodePath[0])
|
2024-04-27 01:35:21 +08:00
|
|
|
|
|
2024-02-25 12:46:18 +08:00
|
|
|
|
db := rc.Query("database")
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.NotEmpty(db, "database不能为空")
|
|
|
|
|
|
ctx := context.TODO()
|
2023-10-27 17:41:45 +08:00
|
|
|
|
res, err := conn.Cli.Database(db).ListCollectionNames(ctx, bson.D{})
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取库集合信息失败: %s")
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) RunCommand(rc *req.Ctx) {
|
2022-05-17 20:23:08 +08:00
|
|
|
|
commandForm := new(form.MongoRunCommand)
|
2024-02-24 16:30:29 +08:00
|
|
|
|
req.BindJsonAndValid(rc, commandForm)
|
2023-08-25 19:41:52 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
2023-08-25 10:20:32 +08:00
|
|
|
|
|
|
|
|
|
|
// 顺序执行
|
|
|
|
|
|
commands := bson.D{}
|
|
|
|
|
|
for _, cmd := range commandForm.Command {
|
|
|
|
|
|
e := bson.E{}
|
|
|
|
|
|
for k, v := range cmd {
|
|
|
|
|
|
e.Key = k
|
|
|
|
|
|
e.Value = v
|
|
|
|
|
|
}
|
|
|
|
|
|
commands = append(commands, e)
|
|
|
|
|
|
}
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
var bm bson.M
|
2023-10-27 17:41:45 +08:00
|
|
|
|
err = conn.Cli.Database(commandForm.Database).RunCommand(
|
2022-05-17 20:23:08 +08:00
|
|
|
|
ctx,
|
2023-08-25 10:20:32 +08:00
|
|
|
|
commands,
|
2022-05-17 20:23:08 +08:00
|
|
|
|
).Decode(&bm)
|
|
|
|
|
|
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "执行命令失败: %s")
|
|
|
|
|
|
rc.ResData = bm
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) FindCommand(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
commandForm := req.BindJsonAndValid(rc, new(form.MongoFindCommand))
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
cli := conn.Cli
|
|
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
limit := commandForm.Limit
|
|
|
|
|
|
if limit != 0 {
|
|
|
|
|
|
biz.IsTrue(limit <= 100, "limit不能超过100")
|
|
|
|
|
|
}
|
|
|
|
|
|
opts := options.Find().SetSort(commandForm.Sort).
|
|
|
|
|
|
SetSkip(commandForm.Skip).
|
|
|
|
|
|
SetLimit(limit)
|
|
|
|
|
|
ctx := context.TODO()
|
2022-06-08 10:21:02 +08:00
|
|
|
|
|
|
|
|
|
|
filter := commandForm.Filter
|
|
|
|
|
|
// 处理_id查询字段,使用ObjectId函数包装
|
|
|
|
|
|
id, ok := filter["_id"].(string)
|
|
|
|
|
|
if ok && id != "" {
|
|
|
|
|
|
objId, err := primitive.ObjectIDFromHex(id)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
filter["_id"] = objId
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
cur, err := cli.Database(commandForm.Database).Collection(commandForm.Collection).Find(ctx, commandForm.Filter, opts)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")
|
|
|
|
|
|
|
|
|
|
|
|
var res []bson.M
|
|
|
|
|
|
cur.All(ctx, &res)
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
commandForm := req.BindJsonAndValid(rc, new(form.MongoUpdateByIdCommand))
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
2023-08-25 19:41:52 +08:00
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
// 解析docId文档id,如果为string类型则使用ObjectId解析,解析失败则为普通字符串
|
|
|
|
|
|
docId := commandForm.DocId
|
|
|
|
|
|
docIdVal, ok := docId.(string)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
objId, err := primitive.ObjectIDFromHex(docIdVal)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
docId = objId
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
res, err := conn.Cli.Database(commandForm.Database).Collection(commandForm.Collection).UpdateByID(context.TODO(), docId, commandForm.Update)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")
|
|
|
|
|
|
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
commandForm := req.BindJsonAndValid(rc, new(form.MongoUpdateByIdCommand))
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
2023-08-25 19:41:52 +08:00
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
// 解析docId文档id,如果为string类型则使用ObjectId解析,解析失败则为普通字符串
|
|
|
|
|
|
docId := commandForm.DocId
|
|
|
|
|
|
docIdVal, ok := docId.(string)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
objId, err := primitive.ObjectIDFromHex(docIdVal)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
docId = objId
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
res, err := conn.Cli.Database(commandForm.Database).Collection(commandForm.Collection).DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: docId}})
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
commandForm := req.BindJsonAndValid(rc, new(form.MongoInsertCommand))
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
2023-10-27 17:41:45 +08:00
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
res, err := conn.Cli.Database(commandForm.Database).Collection(commandForm.Collection).InsertOne(context.TODO(), commandForm.Doc)
|
2023-08-25 19:41:52 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")
|
2022-05-17 20:23:08 +08:00
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取请求路径上的mongo id
|
2024-02-24 16:30:29 +08:00
|
|
|
|
func (m *Mongo) GetMongoId(rc *req.Ctx) uint64 {
|
2024-02-25 12:46:18 +08:00
|
|
|
|
dbId := rc.PathParamInt("id")
|
2022-05-17 20:23:08 +08:00
|
|
|
|
biz.IsTrue(dbId > 0, "mongoId错误")
|
|
|
|
|
|
return uint64(dbId)
|
|
|
|
|
|
}
|