-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpublic.go
92 lines (81 loc) · 2.04 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package public
import (
"context"
"os"
"sync"
"time"
"github.com/allegro/bigcache/v3"
"github.com/eryajf/cloud_dns_exporter/public/logger"
"github.com/rs/xid"
"gopkg.in/yaml.v2"
)
// InitSvc 初始化服务
func InitSvc() {
LoadConfig()
InitCache()
}
const (
// Custom
CustomRecords string = "custom_records"
// Cloud Providers
TencentDnsProvider string = "tencent"
AliyunDnsProvider string = "aliyun"
GodaddyDnsProvider string = "godaddy"
DNSLaDnsProvider string = "dnsla"
AmazonDnsProvider string = "amazon"
CloudFlareDnsProvider string = "cloudflare"
// Metrics Name
DomainList string = "domain_list"
RecordList string = "record_list"
RecordCertInfo string = "record_cert_info"
)
var (
once sync.Once
Config *Configuration
Cache *bigcache.BigCache
CertCache *bigcache.BigCache
)
type Account struct {
CloudProvider string `yaml:"cloud_provider"`
CloudName string `yaml:"cloud_name"`
SecretID string `yaml:"secretId"`
SecretKey string `yaml:"secretKey"`
}
// Config 表示配置文件的结构
type Configuration struct {
CustomRecords []string `yaml:"custom_records"`
CloudProviders map[string]struct {
Accounts []map[string]string `yaml:"accounts"`
} `yaml:"cloud_providers"`
}
// LoadConfig 加载配置
func LoadConfig() *Configuration {
once.Do(func() {
Config = &Configuration{}
data, err := os.ReadFile("config.yaml")
if err != nil {
logger.Fatal("read config file failed: ", err)
}
err = yaml.Unmarshal(data, &Config)
if err != nil {
logger.Fatal("unmarshal config file failed: ", err)
}
})
return Config
}
// InitCache 初始化缓存
func InitCache() {
var err error
Cache, err = bigcache.New(context.Background(), bigcache.DefaultConfig(5*time.Minute))
if err != nil {
logger.Fatal("init cache failed: ", err)
}
CertCache, err = bigcache.New(context.Background(), bigcache.DefaultConfig(25*time.Hour))
if err != nil {
logger.Fatal("init cache failed: ", err)
}
}
// GetID 获取唯一ID
func GetID() string {
return xid.New().String()
}