添加MongoDB认证支持并优化序列号生成工具

This commit is contained in:
2026-02-26 17:47:19 +08:00
committed by 张斌
parent a330577740
commit add07d47f1
3 changed files with 65 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ package mongo
import (
"context"
"fmt"
"net/url"
"os"
"os/signal"
"strings"
@@ -34,6 +35,8 @@ type DataSourceConfig struct {
Name string `json:"name"`
Address string `json:"address"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
MaxPoolSize int32 `json:"maxPoolSize"`
MinPoolSize int32 `json:"minPoolSize"`
ConnectTimeout time.Duration `json:"connectTimeout"`
@@ -110,9 +113,25 @@ func (d *BaseDataSource) Connect(ctx context.Context) error {
dbName = gstr.SubStr(dbName, 0, strings.Index(dbName, "?"))
}
// 构建连接URI
connectionURI := fmt.Sprintf("mongodb://%s", d.config.Address)
// 如果配置了用户名和密码添加到URI中
if d.config.Username != "" {
// URL编码用户名和密码正确处理特殊字符
encodedUsername := url.QueryEscape(d.config.Username)
encodedPassword := url.QueryEscape(d.config.Password)
// 构建认证信息
authInfo := fmt.Sprintf("%s:%s@", encodedUsername, encodedPassword)
// 将认证信息插入到URI中
connectionURI = fmt.Sprintf("mongodb://%s%s", authInfo, d.config.Address)
}
// 构建连接选项
opt := options.Client().
ApplyURI(d.config.Address).
ApplyURI(connectionURI).
SetMaxPoolSize(uint64(d.config.MaxPoolSize)).
SetMinPoolSize(uint64(d.config.MinPoolSize)).
SetConnectTimeout(d.config.ConnectTimeout).
@@ -302,6 +321,8 @@ func (m *DataSourceManager) InitializeFromConfig(ctx context.Context) error {
Name: name,
Address: gconv.String(address),
Database: gconv.String(subMap["database"]),
Username: gconv.String(subMap["username"]),
Password: gconv.String(subMap["password"]),
MaxPoolSize: int32(gconv.Int(subMap["maxPoolSize"])),
MinPoolSize: int32(gconv.Int(subMap["minPoolSize"])),
ConnectTimeout: gconv.Duration(subMap["connectTimeout"]),

View File

@@ -57,7 +57,7 @@ func commandMonitor() *event.CommandMonitor {
fmt.Printf("[%s] 开始执行命令 | 数据库: %s | 集合: %s | 命令: %+v\n",
time.Now().Format("2006-01-02 15:04:05"),
evt.DatabaseName,
evt.Command.Lookup("collection").StringValue(), // 获取集合名
//evt.Command.Lookup("collection").StringValue(), // 获取集合名
evt.Command,
)