db 测试数据库

This commit is contained in:
wuchao 2024-11-28 14:27:05 +08:00
parent 7cab8f82d7
commit 935240b36b
5 changed files with 36 additions and 0 deletions

View File

@ -39,3 +39,10 @@ func DbDel(c *gin.Context) {
err := db_service.DbDel(request) err := db_service.DbDel(request)
controllers.HandRes(c, nil, err) controllers.HandRes(c, nil, err)
} }
// 测试连接
func DbTest(c *gin.Context) {
request := controllers.GetRequest(c).(*backend.DbTestRequest)
err := db_service.DbTest(request)
controllers.HandRes(c, nil, err)
}

View File

@ -29,6 +29,10 @@ type DbAddRequest struct {
Status int `json:"status" form:"status" example:"1"` Status int `json:"status" form:"status" example:"1"`
} }
type DbTestRequest struct {
Source string `json:"source" validate:"required" form:"source" example:""`
}
type DbEditRequest struct { type DbEditRequest struct {
DbId int `json:"db_id" validate:"required" form:"db_id" example:""` DbId int `json:"db_id" validate:"required" form:"db_id" example:""`
DbName string `json:"db_name" validate:"required" form:"db_name" example:""` DbName string `json:"db_name" validate:"required" form:"db_name" example:""`

View File

@ -22,6 +22,7 @@ var BackendRequestMap = map[string]func() interface{}{
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) }, common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) }, common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) },
common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) }, common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) },
common.ADMIN_OAUTH_V1 + "/sql/test": func() interface{} { return new(backend.DbTestRequest) },
// 消息渠道 // 消息渠道
common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) }, common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) },

View File

@ -44,6 +44,7 @@ func RegisterAdminRoute(router *gin.Engine) {
sql.POST("/add", backend.DbAdd) sql.POST("/add", backend.DbAdd)
sql.POST("/edit", backend.DbEdit) sql.POST("/edit", backend.DbEdit)
sql.DELETE("/del", backend.DbDel) sql.DELETE("/del", backend.DbDel)
sql.POST("/test", backend.DbTest)
} }
//任务 //任务
cmd := v1.Group("/cmd") cmd := v1.Group("/cmd")

View File

@ -1,10 +1,15 @@
package db_service package db_service
import ( import (
"context"
"cron_admin/app/constants/common" "cron_admin/app/constants/common"
"cron_admin/app/constants/errorcode"
"cron_admin/app/http/entities/backend" "cron_admin/app/http/entities/backend"
"cron_admin/app/models/crondbmodel" "cron_admin/app/models/crondbmodel"
"cron_admin/app/utils/mapstructure" "cron_admin/app/utils/mapstructure"
"database/sql"
"fmt"
"time"
"xorm.io/builder" "xorm.io/builder"
) )
@ -60,3 +65,21 @@ func DbDel(request *backend.DbDeleteRequest) (err error) {
_, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Delete(&crondbmodel.CronDb{}) _, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Delete(&crondbmodel.CronDb{})
return return
} }
func DbTest(request *backend.DbTestRequest) (err error) {
// 打开数据库连接
db, err := sql.Open("mysql", request.Source)
if err != nil {
return err
}
defer db.Close()
// 使用 context 来设置 Ping 的超时时间
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// 测试连接
err = db.PingContext(ctx)
if err != nil {
return errorcode.NewBusinessErr(301, fmt.Sprintf("数据库连接失败: %v", err))
}
return
}