You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wt-game/point/point.go

90 lines
2.6 KiB
Go

package point
import (
"gitea.weitiangame.com/sdk/wt-game/utils/ahttp"
"github.com/go-resty/resty/v2"
"go.uber.org/zap"
"sync"
)
var (
instance *Point
once sync.Once
)
2 months ago
// Point 是用于事件上报的核心结构体
// Point is the core structure for event reporting
type Point struct {
clientID string
logger *zap.Logger
2 months ago
client *resty.Client // 线程安全的HTTP客户端 / Thread-safe HTTP client
debug bool
curl bool
mu sync.Mutex // 保护并发访问配置字段 / Mutex to protect concurrent access to configuration fields
}
2 months ago
// New 返回Point的单例实例
// New returns the singleton instance of Point
// 注意:首次调用后的参数变更将不会生效(单例模式特性)
// Note: Parameter changes after the first call will not take effect (singleton pattern characteristic)
func New(clientID string, logger *zap.Logger) *Point {
once.Do(func() {
2 months ago
// 初始化线程安全的HTTP客户端
// Initialize thread-safe HTTP client
baseClient := ahttp.New(nil).SetLog(logger).Client()
instance = &Point{
clientID: clientID,
logger: logger,
2 months ago
client: baseClient,
}
})
return instance
}
2 months ago
// Debug 启用调试模式(非线程安全,建议初始化阶段调用)
// Debug enables debug mode (not thread-safe, recommended to call during initialization)
func (p *Point) Debug() *Point {
p.mu.Lock()
defer p.mu.Unlock()
p.debug = true
return p
}
// Curl 启用CURL命令生成非线程安全建议初始化阶段调用
// Curl enables CURL command generation (not thread-safe, recommended to call during initialization)
func (p *Point) Curl() *Point {
p.mu.Lock()
defer p.mu.Unlock()
p.curl = true
return p
}
// Event 发送事件到指定端点
// Event sends an event to the specified endpoint
// 参数data会被复制以避免原始数据修改并发安全设计
// The data parameter is copied to avoid original data modification (concurrency-safe design)
func (p *Point) Event(data map[string]interface{}) (*resty.Response, error) {
2 months ago
data["client_id"] = p.clientID
// 获取当前配置的调试和CURL设置
// Get current debug and curl configuration
p.mu.Lock()
debugEnabled := p.debug
curlEnabled := p.curl
p.mu.Unlock()
// 创建新的请求实例并配置参数
// Create new request instance and configure parameters
request := p.client.R().
SetBody(data).
SetDebug(debugEnabled)
if curlEnabled {
request.EnableTrace() // 启用跟踪以生成CURL命令 / Enable trace to generate CURL command
}
// 发送请求并返回结果
// Send request and return results
return request.Post("https://e.tapdb.net/v2/event")
}