| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package handler
- import (
- "context"
- "errors"
- "net/http"
- "strings"
- "dbview/service/internal/common/response"
- "dbview/service/internal/modules/connection_pool/api"
- "dbview/service/internal/modules/connection_pool/service"
- "github.com/gin-gonic/gin"
- )
- // RegisterConnection 注册新的数据库连接
- func RegisterConnection(c *gin.Context) {
- var req api.RegisterConnectionRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ParamErrorWithDetail("请求参数错误", err))
- return
- }
- connID := buildConnectionID(req.ConnID, c.ClientIP())
- req.ConnID = connID
- stat, err := service.RegisterConnection(&req)
- if err != nil {
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("注册连接失败", err))
- return
- }
- c.JSON(http.StatusOK, response.SuccessWithMsg("注册连接成功", stat))
- }
- func buildConnectionID(baseID, clientIP string) string {
- if clientIP == "" {
- return baseID
- }
- replacer := strings.NewReplacer("::", "-", ":", "-", ".", "-", "%", "-", "[", "", "]", "")
- sanitized := replacer.Replace(clientIP)
- if baseID == "" {
- return sanitized
- }
- return baseID + "@" + sanitized
- }
- // GetConnectionStats 返回连接池状态
- func GetConnectionStats(c *gin.Context) {
- stats, err := service.GetConnectionStats()
- if err != nil {
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("获取连接池状态失败", err))
- return
- }
- c.JSON(http.StatusOK, response.Success(api.ConnectionStatsResponse{Connections: stats}))
- }
- // CloseConnection 关闭指定连接
- func CloseConnection(c *gin.Context) {
- var req api.CloseConnectionRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ParamErrorWithDetail("请求参数错误", err))
- return
- }
- connID := buildConnectionID(req.ConnID, c.ClientIP())
- if err := service.CloseConnection(connID); err != nil {
- if errors.Is(err, response.ErrConnNotExist) {
- c.JSON(http.StatusNotFound, response.NotFoundWithDetail("连接不存在", err))
- return
- }
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("关闭连接失败", err))
- return
- }
- c.JSON(http.StatusOK, response.SuccessWithMsg("连接已关闭", nil))
- }
- // CloseAllConnections 关闭所有连接
- func CloseAllConnections(c *gin.Context) {
- if err := service.CloseAllConnections(); err != nil {
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("关闭所有连接失败", err))
- return
- }
- c.JSON(http.StatusOK, response.SuccessWithMsg("所有连接已关闭", nil))
- }
- // PingConnection 检查连接是否可用
- func PingConnection(c *gin.Context) {
- var req api.PingConnectionRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ParamErrorWithDetail("请求参数错误", err))
- return
- }
- ctx := c.Request.Context()
- if ctx == nil {
- ctx = context.Background()
- }
- connID := buildConnectionID(req.ConnID, c.ClientIP())
- if err := service.PingConnection(ctx, connID); err != nil {
- if errors.Is(err, response.ErrConnNotExist) {
- c.JSON(http.StatusNotFound, response.NotFoundWithDetail("连接不存在", err))
- return
- }
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("连接检测失败", err))
- return
- }
- c.JSON(http.StatusOK, response.SuccessWithMsg("连接可用", nil))
- }
|