初始化 zhub Go 客户端项目
新增 zhub 客户端完整实现,包括配置加载、TCP通信、消息发布/订阅、 RPC 调用、分布式锁等功能。支持从项目配置文件、环境变量等多种方式初始化客户端,并提供便捷的全局调用接口。- 添加 `.gitignore` 忽略 IDE 和临时文件 - 实现 `api.go` 提供全局便捷方法封装客户端调用 - 实现 `client.go` 核心客户端逻辑,包含网络通信、消息处理等 - 添加 `client_test.go` 单元测试和集成测试示例 - 实现 `config.go` 支持灵活的配置加载机制 - 添加示例配置文件 `example-config.yml` - 初始化 Go 模块依赖 `go.mod` 和 `go.sum` - 实现 `init.go` 提供多种初始化方式 - 添加 MIT 许可证文件 `LICENSE` - 新增使用示例 `example/main.go` 展示基本功能调用 - 实现 `manager.go` 管理默认客户端实例及初始化逻辑
This commit is contained in:
53
init.go
Normal file
53
init.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package zhub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// InitFromCurrentDir 从当前目录的配置文件初始化
|
||||
func InitFromCurrentDir() error {
|
||||
// 尝试多个可能的配置文件
|
||||
possibleConfigs := []string{
|
||||
"app.yml",
|
||||
"config.yml",
|
||||
"config.yaml",
|
||||
"app.yaml",
|
||||
"./conf/app.yml",
|
||||
"./config/app.yml",
|
||||
}
|
||||
|
||||
for _, configPath := range possibleConfigs {
|
||||
if _, err := os.Stat(configPath); err == nil {
|
||||
return InitWithProjectConfig(configPath)
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("no config file found in current directory")
|
||||
}
|
||||
|
||||
// InitFromEnv 从环境变量指定的配置文件初始化
|
||||
func InitFromEnv() error {
|
||||
configPath := os.Getenv("ZHUB_CONFIG_PATH")
|
||||
if configPath == "" {
|
||||
return fmt.Errorf("ZHUB_CONFIG_PATH environment variable not set")
|
||||
}
|
||||
|
||||
return InitWithProjectConfig(configPath)
|
||||
}
|
||||
|
||||
// InitWithWorkingDir 使用工作目录下的配置文件
|
||||
func InitWithWorkingDir(configName string) error {
|
||||
if configName == "" {
|
||||
configName = "app.yml"
|
||||
}
|
||||
|
||||
workDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get working directory: %w", err)
|
||||
}
|
||||
|
||||
configPath := filepath.Join(workDir, configName)
|
||||
return InitWithProjectConfig(configPath)
|
||||
}
|
||||
Reference in New Issue
Block a user