Files
waf-platform/EdgeAdmin/internal/web/actions/default/db/testClickhouse.go
2026-02-13 22:36:17 +08:00

93 lines
1.8 KiB
Go

//go:build plus
package db
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type TestClickHouseAction struct {
actionutils.ParentAction
}
func (this *TestClickHouseAction) Init() {
this.Nav("db", "db", "clickhouse")
}
func (this *TestClickHouseAction) RunPost(params struct {
Host string
Port int
User string
Password string
Database string
Scheme string
Must *actions.Must
}) {
params.Must.
Field("host", params.Host).
Require("请输入 ClickHouse 连接地址")
if params.Database == "" {
params.Database = "default"
}
scheme := "https"
if strings.EqualFold(params.Scheme, "http") {
scheme = "http"
}
if params.Port <= 0 {
params.Port = 8443
}
// 构造测试请求
testURL := fmt.Sprintf("%s://%s:%d/?query=SELECT+1&database=%s",
scheme, params.Host, params.Port, params.Database)
transport := &http.Transport{}
if scheme == "https" {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
client := &http.Client{
Timeout: 5 * time.Second,
Transport: transport,
}
req, err := http.NewRequest(http.MethodGet, testURL, nil)
if err != nil {
this.Fail("请求构造失败: " + err.Error())
return
}
if params.User != "" || params.Password != "" {
req.SetBasicAuth(params.User, params.Password)
}
resp, err := client.Do(req)
if err != nil {
this.Fail("连接失败: " + err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
msg := strings.TrimSpace(string(body))
if len(msg) > 200 {
msg = msg[:200] + "..."
}
this.Fail(fmt.Sprintf("ClickHouse 返回 HTTP %d: %s", resp.StatusCode, msg))
return
}
this.Success()
}