Skip to content

Commit e8c4b2b

Browse files
committed
feat: 公开API接口包,方便GUI调用使用本项目作依赖
1 parent 8016a8f commit e8c4b2b

6 files changed

Lines changed: 721 additions & 0 deletions

File tree

api/api.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package api
2+
3+
const (
4+
// Version API版本号
5+
Version = "v1.0.0"
6+
7+
// DefaultVersion 默认的ECS版本号
8+
DefaultVersion = "v0.1.114"
9+
)
10+
11+
// 测试方法常量
12+
const (
13+
// CPU测试方法
14+
CpuMethodSysbench = "sysbench"
15+
CpuMethodGeekbench = "geekbench"
16+
CpuMethodWinsat = "winsat"
17+
18+
// 内存测试方法
19+
MemoryMethodStream = "stream"
20+
MemoryMethodSysbench = "sysbench"
21+
MemoryMethodDD = "dd"
22+
MemoryMethodWinsat = "winsat"
23+
24+
// 硬盘测试方法
25+
DiskMethodFio = "fio"
26+
DiskMethodDD = "dd"
27+
DiskMethodWinsat = "winsat"
28+
29+
// 线程模式
30+
ThreadModeSingle = "single"
31+
ThreadModeMulti = "multi"
32+
33+
// 语言选项
34+
LanguageZH = "zh"
35+
LanguageEN = "en"
36+
37+
// IP检测类型
38+
CheckTypeIPv4 = "ipv4"
39+
CheckTypeIPv6 = "ipv6"
40+
CheckTypeAuto = "auto"
41+
42+
// 测速平台
43+
PlatformCN = "cn"
44+
PlatformNet = "net"
45+
46+
// 运营商类型
47+
OperatorCMCC = "cmcc" // 中国移动
48+
OperatorCU = "cu" // 中国联通
49+
OperatorCT = "ct" // 中国电信
50+
OperatorGlobal = "global" // 全球节点
51+
OperatorOther = "other" // 其他
52+
OperatorHK = "hk" // 香港
53+
OperatorTW = "tw" // 台湾
54+
OperatorJP = "jp" // 日本
55+
OperatorSG = "sg" // 新加坡
56+
)

api/config.go

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package api
2+
3+
import (
4+
"github.com/oneclickvirt/ecs/internal/params"
5+
)
6+
7+
// Config 配置接口,导出用于外部调用
8+
type Config = params.Config
9+
10+
// NewConfig 创建默认配置
11+
// version: 版本号字符串
12+
func NewConfig(version string) *Config {
13+
return params.NewConfig(version)
14+
}
15+
16+
// NewDefaultConfig 创建默认配置(使用默认版本号)
17+
func NewDefaultConfig() *Config {
18+
return params.NewConfig("v0.1.114")
19+
}
20+
21+
// ConfigOption 配置选项函数类型
22+
type ConfigOption func(*Config)
23+
24+
// WithLanguage 设置语言
25+
func WithLanguage(lang string) ConfigOption {
26+
return func(c *Config) {
27+
c.Language = lang
28+
}
29+
}
30+
31+
// WithCpuTestMethod 设置CPU测试方法
32+
// method: "sysbench" 或 "geekbench"
33+
func WithCpuTestMethod(method string) ConfigOption {
34+
return func(c *Config) {
35+
c.CpuTestMethod = method
36+
}
37+
}
38+
39+
// WithCpuTestThreadMode 设置CPU测试线程模式
40+
// mode: "single" 或 "multi"
41+
func WithCpuTestThreadMode(mode string) ConfigOption {
42+
return func(c *Config) {
43+
c.CpuTestThreadMode = mode
44+
}
45+
}
46+
47+
// WithMemoryTestMethod 设置内存测试方法
48+
// method: "stream", "sysbench", "dd"
49+
func WithMemoryTestMethod(method string) ConfigOption {
50+
return func(c *Config) {
51+
c.MemoryTestMethod = method
52+
}
53+
}
54+
55+
// WithDiskTestMethod 设置硬盘测试方法
56+
// method: "fio" 或 "dd"
57+
func WithDiskTestMethod(method string) ConfigOption {
58+
return func(c *Config) {
59+
c.DiskTestMethod = method
60+
}
61+
}
62+
63+
// WithDiskTestPath 设置硬盘测试路径
64+
func WithDiskTestPath(path string) ConfigOption {
65+
return func(c *Config) {
66+
c.DiskTestPath = path
67+
}
68+
}
69+
70+
// WithDiskMultiCheck 设置是否进行硬盘多路径检测
71+
func WithDiskMultiCheck(enable bool) ConfigOption {
72+
return func(c *Config) {
73+
c.DiskMultiCheck = enable
74+
}
75+
}
76+
77+
// WithSpeedTestNum 设置测速节点数量
78+
func WithSpeedTestNum(num int) ConfigOption {
79+
return func(c *Config) {
80+
c.SpNum = num
81+
}
82+
}
83+
84+
// WithWidth 设置输出宽度
85+
func WithWidth(width int) ConfigOption {
86+
return func(c *Config) {
87+
c.Width = width
88+
}
89+
}
90+
91+
// WithFilePath 设置输出文件路径
92+
func WithFilePath(path string) ConfigOption {
93+
return func(c *Config) {
94+
c.FilePath = path
95+
}
96+
}
97+
98+
// WithEnableUpload 设置是否启用上传
99+
func WithEnableUpload(enable bool) ConfigOption {
100+
return func(c *Config) {
101+
c.EnableUpload = enable
102+
}
103+
}
104+
105+
// WithEnableLogger 设置是否启用日志
106+
func WithEnableLogger(enable bool) ConfigOption {
107+
return func(c *Config) {
108+
c.EnableLogger = enable
109+
}
110+
}
111+
112+
// WithBasicTest 设置是否执行基础信息测试
113+
func WithBasicTest(enable bool) ConfigOption {
114+
return func(c *Config) {
115+
c.BasicStatus = enable
116+
}
117+
}
118+
119+
// WithCpuTest 设置是否执行CPU测试
120+
func WithCpuTest(enable bool) ConfigOption {
121+
return func(c *Config) {
122+
c.CpuTestStatus = enable
123+
}
124+
}
125+
126+
// WithMemoryTest 设置是否执行内存测试
127+
func WithMemoryTest(enable bool) ConfigOption {
128+
return func(c *Config) {
129+
c.MemoryTestStatus = enable
130+
}
131+
}
132+
133+
// WithDiskTest 设置是否执行硬盘测试
134+
func WithDiskTest(enable bool) ConfigOption {
135+
return func(c *Config) {
136+
c.DiskTestStatus = enable
137+
}
138+
}
139+
140+
// WithUnlockTest 设置是否执行流媒体解锁测试
141+
func WithUnlockTest(enable bool) ConfigOption {
142+
return func(c *Config) {
143+
c.UtTestStatus = enable
144+
}
145+
}
146+
147+
// WithSecurityTest 设置是否执行IP质量测试
148+
func WithSecurityTest(enable bool) ConfigOption {
149+
return func(c *Config) {
150+
c.SecurityTestStatus = enable
151+
}
152+
}
153+
154+
// WithEmailTest 设置是否执行邮件端口测试
155+
func WithEmailTest(enable bool) ConfigOption {
156+
return func(c *Config) {
157+
c.EmailTestStatus = enable
158+
}
159+
}
160+
161+
// WithBacktraceTest 设置是否执行回程路由测试
162+
func WithBacktraceTest(enable bool) ConfigOption {
163+
return func(c *Config) {
164+
c.BacktraceStatus = enable
165+
}
166+
}
167+
168+
// WithNt3Test 设置是否执行三网路由测试
169+
func WithNt3Test(enable bool) ConfigOption {
170+
return func(c *Config) {
171+
c.Nt3Status = enable
172+
}
173+
}
174+
175+
// WithSpeedTest 设置是否执行测速测试
176+
func WithSpeedTest(enable bool) ConfigOption {
177+
return func(c *Config) {
178+
c.SpeedTestStatus = enable
179+
}
180+
}
181+
182+
// WithPingTest 设置是否执行PING测试
183+
func WithPingTest(enable bool) ConfigOption {
184+
return func(c *Config) {
185+
c.PingTestStatus = enable
186+
}
187+
}
188+
189+
// WithTgdcTest 设置是否执行Telegram DC测试
190+
func WithTgdcTest(enable bool) ConfigOption {
191+
return func(c *Config) {
192+
c.TgdcTestStatus = enable
193+
}
194+
}
195+
196+
// WithWebTest 设置是否执行网站测试
197+
func WithWebTest(enable bool) ConfigOption {
198+
return func(c *Config) {
199+
c.WebTestStatus = enable
200+
}
201+
}
202+
203+
// WithNt3CheckType 设置三网路由检测类型
204+
// checkType: "ipv4", "ipv6" 或 "auto"
205+
func WithNt3CheckType(checkType string) ConfigOption {
206+
return func(c *Config) {
207+
c.Nt3CheckType = checkType
208+
}
209+
}
210+
211+
// WithNt3Location 设置三网路由检测位置
212+
func WithNt3Location(location string) ConfigOption {
213+
return func(c *Config) {
214+
c.Nt3Location = location
215+
}
216+
}
217+
218+
// WithAutoChangeDiskMethod 设置是否自动切换硬盘测试方法
219+
func WithAutoChangeDiskMethod(enable bool) ConfigOption {
220+
return func(c *Config) {
221+
c.AutoChangeDiskMethod = enable
222+
}
223+
}
224+
225+
// WithOnlyChinaTest 设置是否只进行国内测试
226+
func WithOnlyChinaTest(enable bool) ConfigOption {
227+
return func(c *Config) {
228+
c.OnlyChinaTest = enable
229+
}
230+
}
231+
232+
// WithMenuMode 设置是否启用菜单模式
233+
func WithMenuMode(enable bool) ConfigOption {
234+
return func(c *Config) {
235+
c.MenuMode = enable
236+
}
237+
}
238+
239+
// WithOnlyIpInfoCheck 设置是否只进行IP信息检测
240+
func WithOnlyIpInfoCheck(enable bool) ConfigOption {
241+
return func(c *Config) {
242+
c.OnlyIpInfoCheck = enable
243+
}
244+
}
245+
246+
// WithChoice 设置菜单选择
247+
func WithChoice(choice string) ConfigOption {
248+
return func(c *Config) {
249+
c.Choice = choice
250+
}
251+
}
252+
253+
// ApplyOptions 应用配置选项
254+
func ApplyOptions(config *Config, options ...ConfigOption) *Config {
255+
for _, opt := range options {
256+
opt(config)
257+
}
258+
return config
259+
}

api/menu.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package api
2+
3+
import (
4+
"github.com/oneclickvirt/ecs/internal/menu"
5+
"github.com/oneclickvirt/ecs/utils"
6+
)
7+
8+
// GetMenuChoice 获取用户菜单选择
9+
// language: 语言 ("zh" 或 "en")
10+
// 返回: 用户选择的选项
11+
func GetMenuChoice(language string) string {
12+
return menu.GetMenuChoice(language)
13+
}
14+
15+
// PrintMenuOptions 打印菜单选项
16+
// preCheck: 网络检查结果
17+
// config: 配置对象
18+
func PrintMenuOptions(preCheck utils.NetCheckResult, config *Config) {
19+
menu.PrintMenuOptions(preCheck, config)
20+
}
21+
22+
// HandleMenuMode 处理菜单模式
23+
// preCheck: 网络检查结果
24+
// config: 配置对象
25+
func HandleMenuMode(preCheck utils.NetCheckResult, config *Config) {
26+
menu.HandleMenuMode(preCheck, config)
27+
}

0 commit comments

Comments
 (0)