Files
kenaito-conf/kenaito-conf-go-client-demo/main.go
T
odboy 2f5c343347 refactor(go-client): 拆分 demo 到独立目录并添加 README
- 将 kenaito-conf-go-client/example/ 拆分为独立的 kenaito-conf-go-client-demo/
- 添加 kenaito-conf-go-client/README.md 使用说明
- 更新根目录 README.md 项目结构
2026-03-03 21:10:12 +08:00

58 lines
1.2 KiB
Go

package main
import (
"fmt"
"kenaito-conf/go-client"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
// create config
cfg := &client.Config{
BaseURL: "http://localhost:8080",
AppName: "kenaito-springboot-demo",
Env: "daily",
Version: "latest",
//Token: "6f6966fe196572954b176c0e2a97bc399b050c6be31454b8d6b0f7f622b5d6f1",
SyncInterval: 30, // sync every 30 seconds
CacheDir: ".",
}
// create client
c := client.New(cfg)
// register change handler
c.OnChange(func(changed map[string]string) {
fmt.Println("Config changed:")
for k, v := range changed {
fmt.Printf(" %s = %s\n", k, v)
}
})
// start client
if err := c.Start(); err != nil {
log.Fatalf("Failed to start config client: %v", err)
}
defer c.Stop()
// read configs
fmt.Println("Current configs:")
for k, v := range c.GetAll() {
fmt.Printf(" %s = %s\n", k, v)
}
// get specific config
dbHost := c.Get("database.host")
fmt.Printf("\ndatabase.host = %s\n", dbHost)
// keep running until signal
fmt.Println("\nListening for config changes... (press Ctrl+C to stop)")
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
<-sigCh
fmt.Println("\nShutting down...")
}