-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
153 lines (131 loc) · 2.68 KB
/
config.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/json"
"errors"
"log"
"os"
"path"
"time"
"gopkg.in/yaml.v3"
)
// last update: 2023/07/28
var AreaNameStrings = []string{
"基隆市",
"臺北市",
"新北市",
"桃園市",
"新竹市",
"新竹縣",
"苗栗縣",
"臺中市",
"彰化縣",
"雲林縣",
"南投縣",
"嘉義市",
"嘉義縣",
"臺南市",
"高雄市",
"屏東縣",
"宜蘭縣",
"花蓮縣",
"臺東縣",
"澎湖縣",
"連江縣",
"金門縣",
}
type Config struct {
Discord DiscordConfig `yaml:"discord"`
Line LineConfig `yaml:"line"`
AreaNames []string `yaml:"area_name"`
}
type DiscordConfig struct {
Enable bool `yaml:"enable"`
TOKEN string `yaml:"TOKEN"`
Webhooks []string `yaml:"webhook"`
ChannelIDs []int64 `yaml:"channel_ids"`
}
type LineConfig struct {
Enable bool `yaml:"enable"`
Tokens []string `yaml:"tokens"`
}
var (
ConfigData = &Config{}
StopWatch = false
)
const (
ConfigFilePath = "./data/config.yaml"
TmpFilePath = "./data/tmp"
)
func init() {
yamlFile, err := os.ReadFile(ConfigFilePath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
log.Fatal(err)
}
config := NewConfig()
data, _ := yaml.Marshal(config)
os.MkdirAll(path.Dir(ConfigFilePath), 0777)
os.WriteFile(ConfigFilePath, data, 0777)
} else {
yaml.Unmarshal(yamlFile, &ConfigData)
}
// watch config file
go func() {
for !StopWatch {
err := watchFile(ConfigFilePath)
if err != nil {
log.Println("config watch error", err)
time.Sleep(time.Second * 5)
}
// reload config
yamlFile, err := os.ReadFile(ConfigFilePath)
if err != nil {
log.Println("config watch error", err)
time.Sleep(time.Second * 5)
} else {
yaml.Unmarshal(yamlFile, &ConfigData)
}
}
}()
}
func NewConfig() *Config {
return &Config{
Discord: DiscordConfig{Enable: false},
Line: LineConfig{Enable: false},
AreaNames: AreaNameStrings,
}
}
// is ease watch file func
func watchFile(filePath string) error {
initialStat, err := os.Stat(filePath)
if err != nil {
return err
}
for {
stat, err := os.Stat(filePath)
if err != nil {
return err
}
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
break
}
// sleep 1 second
time.Sleep(time.Second)
}
return nil
}
func GetTmpDate() (value map[string]map[string]bool) {
if data, err := os.ReadFile(TmpFilePath); err == nil {
json.Unmarshal(data, &value)
return
}
return map[string]map[string]bool{} // if file not exist
}
func WriteTmpDate(v any) (err error) {
bytes, err := json.Marshal(v)
if err == nil {
os.MkdirAll(path.Dir(ConfigFilePath), 0777)
os.WriteFile(TmpFilePath, bytes, 0777)
}
return
}