添加mongodb使用,添加用户前端配置的上传及获取接口

This commit is contained in:
junleea 2025-05-20 17:36:54 +08:00
parent a2e0dae65a
commit 97dfd18eb4
2 changed files with 16 additions and 8 deletions

View File

@ -148,21 +148,25 @@ var mongoClient *mongo.Client
//var collection *mongo.Collection
func InitMongoDB() {
func InitMongoDB() error {
// 设置 MongoDB 客户端选项
clientOptions := options.Client().ApplyURI(proto.Config.MONGO_URI)
// 连接到 MongoDB
var err error
mongoClient, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
log.Println("Error connecting to MongoDB:", err)
} else {
// 检查连接
err = mongoClient.Ping(context.TODO(), nil)
if err != nil {
log.Println("Error pinging MongoDB:", err)
} else {
log.Println("Connected to MongoDB!")
}
}
// 检查连接
err = mongoClient.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected to MongoDB!")
return err
}
func CloseMongoDB() {

View File

@ -27,6 +27,10 @@ func main() {
if err != nil {
panic("failed to connect database:" + err.Error())
}
err = dao.InitMongoDB()
if err != nil {
panic("failed to connect mongodb:" + err.Error())
}
err = worker.InitRedis()
if err != nil {
panic("failed to connect redis:" + err.Error())