Files
mayfly-go/server/internal/es/esm/esi/conn.go
zongyangleo 142bbd265d !134 feat: 新增支持es和连接池
* feat: 各连接,支持连接池
* feat:支持es
2025-05-21 04:42:30 +00:00

56 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package esi
import (
"fmt"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/logx"
"net/http"
"net/http/httputil"
"net/url"
)
type EsConn struct {
Id uint64
Info *EsInfo
proxy *httputil.ReverseProxy
}
// StartProxy 开始代理
func (d *EsConn) StartProxy() error {
// 目标 URL
targetURL, err := url.Parse(d.Info.baseUrl)
if err != nil {
logx.Errorf("Error parsing URL: %v", err)
return err
}
// 创建反向代理
d.proxy = httputil.NewSingleHostReverseProxy(targetURL)
// 设置 proxy buffer pool
d.proxy.BufferPool = NewBufferPool()
return nil
}
func (d *EsConn) Proxy(w http.ResponseWriter, r *http.Request, path string) {
r.URL.Path = path
if d.Info.authorization != "" {
r.Header.Set("Authorization", d.Info.authorization)
}
r.Header.Set("connection", "keep-alive")
r.Header.Set("Accept", "application/json")
d.proxy.ServeHTTP(w, r)
}
func (d *EsConn) Close() error {
// 如果是使用了ssh隧道转发则需要手动将其关闭
if d.Info.useSshTunnel {
mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("es:%d", d.Id))
}
return nil
}
func (d *EsConn) Ping() error {
_, err := d.Info.Ping()
return err
}