From 935240b36b6b6229d8e86e3c287bbce10e13f503 Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Thu, 28 Nov 2024 14:27:05 +0800 Subject: [PATCH] =?UTF-8?q?db=20=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/backend/db_controller.go | 7 ++++++ app/http/entities/backend/db.go | 4 ++++ app/http/requestmapping/backend.go | 1 + app/http/routes/admin.go | 1 + app/services/db_service/db_service.go | 23 +++++++++++++++++++ 5 files changed, 36 insertions(+) diff --git a/app/http/controllers/backend/db_controller.go b/app/http/controllers/backend/db_controller.go index 98a9b72..11871cb 100644 --- a/app/http/controllers/backend/db_controller.go +++ b/app/http/controllers/backend/db_controller.go @@ -39,3 +39,10 @@ func DbDel(c *gin.Context) { err := db_service.DbDel(request) 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) +} diff --git a/app/http/entities/backend/db.go b/app/http/entities/backend/db.go index 6672401..8a49ceb 100644 --- a/app/http/entities/backend/db.go +++ b/app/http/entities/backend/db.go @@ -29,6 +29,10 @@ type DbAddRequest struct { Status int `json:"status" form:"status" example:"1"` } +type DbTestRequest struct { + Source string `json:"source" validate:"required" form:"source" example:""` +} + type DbEditRequest struct { DbId int `json:"db_id" validate:"required" form:"db_id" example:""` DbName string `json:"db_name" validate:"required" form:"db_name" example:""` diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index 5787e1e..fae6d82 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -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/edit": func() interface{} { return new(backend.DbEditRequest) }, 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) }, diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index abdf8b1..ad1207d 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -44,6 +44,7 @@ func RegisterAdminRoute(router *gin.Engine) { sql.POST("/add", backend.DbAdd) sql.POST("/edit", backend.DbEdit) sql.DELETE("/del", backend.DbDel) + sql.POST("/test", backend.DbTest) } //任务 cmd := v1.Group("/cmd") diff --git a/app/services/db_service/db_service.go b/app/services/db_service/db_service.go index 9ee6d30..b4e4b09 100644 --- a/app/services/db_service/db_service.go +++ b/app/services/db_service/db_service.go @@ -1,10 +1,15 @@ package db_service import ( + "context" "cron_admin/app/constants/common" + "cron_admin/app/constants/errorcode" "cron_admin/app/http/entities/backend" "cron_admin/app/models/crondbmodel" "cron_admin/app/utils/mapstructure" + "database/sql" + "fmt" + "time" "xorm.io/builder" ) @@ -60,3 +65,21 @@ func DbDel(request *backend.DbDeleteRequest) (err error) { _, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Delete(&crondbmodel.CronDb{}) 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 +}