73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
|
||
"git.toowon.com/jimmy/go-common/config"
|
||
"git.toowon.com/jimmy/go-common/factory"
|
||
)
|
||
|
||
func main() {
|
||
// 加载配置
|
||
cfg, err := config.LoadFromFile("./config/example.json")
|
||
if err != nil {
|
||
log.Fatal("Failed to load config:", err)
|
||
}
|
||
|
||
// 创建工厂实例
|
||
fac := factory.NewFactory(cfg)
|
||
|
||
// 示例1:获取邮件客户端(已初始化,可直接使用)
|
||
fmt.Println("=== Example 1: Get Email Client ===")
|
||
emailClient, err := fac.GetEmailClient()
|
||
if err != nil {
|
||
log.Printf("Email client not available: %v", err)
|
||
} else {
|
||
fmt.Println("Email client created successfully")
|
||
// 直接使用,无需再创建
|
||
_ = emailClient // 示例中不使用,实际使用时可以直接调用方法
|
||
// err = emailClient.SendSimple(
|
||
// []string{"recipient@example.com"},
|
||
// "测试邮件",
|
||
// "这是测试内容",
|
||
// )
|
||
fmt.Println("Email client is ready to use")
|
||
}
|
||
|
||
// 示例2:获取短信客户端(已初始化,可直接使用)
|
||
fmt.Println("\n=== Example 2: Get SMS Client ===")
|
||
smsClient, err := fac.GetSMSClient()
|
||
if err != nil {
|
||
log.Printf("SMS client not available: %v", err)
|
||
} else {
|
||
fmt.Println("SMS client created successfully")
|
||
// 直接使用,无需再创建
|
||
_ = smsClient // 示例中不使用,实际使用时可以直接调用方法
|
||
// resp, err := smsClient.SendSimple(
|
||
// []string{"13800138000"},
|
||
// map[string]string{"code": "123456"},
|
||
// )
|
||
fmt.Println("SMS client is ready to use")
|
||
}
|
||
|
||
// 示例3:访问配置对象
|
||
fmt.Println("\n=== Example 3: Access Config Object ===")
|
||
cfgObj := fac.GetConfig()
|
||
dsn, err := cfgObj.GetDatabaseDSN()
|
||
if err != nil {
|
||
log.Printf("Database DSN not available: %v", err)
|
||
} else {
|
||
fmt.Printf("Database DSN: %s\n", dsn)
|
||
}
|
||
|
||
redisAddr := cfgObj.GetRedisAddr()
|
||
if redisAddr != "" {
|
||
fmt.Printf("Redis Address: %s\n", redisAddr)
|
||
}
|
||
|
||
fmt.Println("\nNote: Factory provides initialized clients directly,")
|
||
fmt.Println("no need to implement creation logic in your code.")
|
||
}
|
||
|