-
Notifications
You must be signed in to change notification settings - Fork 182
/
access_log_storage_policy.go
108 lines (96 loc) · 2.57 KB
/
access_log_storage_policy.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
package teaconfigs
import (
"github.com/TeaWeb/build/internal/teaconfigs/shared"
"github.com/TeaWeb/build/internal/teautils"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/rands"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
)
// 日志存储策略
// 存储在configs/accesslog.storage.$id.conf
type AccessLogStoragePolicy struct {
Id string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"`
On bool `yaml:"bool" json:"on"`
Type string `yaml:"type" json:"type"` // 存储类型
Options map[string]interface{} `yaml:"options" json:"options"` // 存储选项
Cond []*shared.RequestCond `yaml:"cond" json:"cond"` // 请求条件
}
// 创建新策略
func NewAccessLogStoragePolicy() *AccessLogStoragePolicy {
return &AccessLogStoragePolicy{
Id: rands.HexString(16),
On: true,
}
}
// 从文件中加载策略
func NewAccessLogStoragePolicyFromId(id string) *AccessLogStoragePolicy {
filename := "accesslog.storage." + id + ".conf"
data, err := ioutil.ReadFile(Tea.ConfigFile(filename))
if err != nil {
logs.Error(err)
return nil
}
policy := NewAccessLogStoragePolicy()
err = yaml.Unmarshal(data, policy)
if err != nil {
logs.Error(err)
return nil
}
return policy
}
// 校验
func (this *AccessLogStoragePolicy) Validate() error {
// cond
if len(this.Cond) > 0 {
for _, cond := range this.Cond {
err := cond.Validate()
if err != nil {
return err
}
}
}
return nil
}
// 保存
func (this *AccessLogStoragePolicy) Save() error {
shared.Locker.Lock()
defer shared.Locker.WriteUnlockNotify()
data, err := yaml.Marshal(this)
if err != nil {
return err
}
filename := "accesslog.storage." + this.Id + ".conf"
return ioutil.WriteFile(Tea.ConfigFile(filename), data, 0666)
}
// 删除
func (this *AccessLogStoragePolicy) Delete() error {
filename := "accesslog.storage." + this.Id + ".conf"
return os.Remove(Tea.ConfigFile(filename))
}
// 匹配关键词
func (this *AccessLogStoragePolicy) MatchKeyword(keyword string) (matched bool, name string, tags []string) {
if teautils.MatchKeyword(this.Name, keyword) || teautils.MatchKeyword(this.Type, keyword) {
matched = true
name = this.Name
if len(this.Type) > 0 {
tags = []string{"类型:" + this.Type}
}
}
return
}
// 匹配条件
func (this *AccessLogStoragePolicy) MatchConds(formatter func(string) string) bool {
if len(this.Cond) == 0 {
return true
}
for _, cond := range this.Cond {
if !cond.Match(formatter) {
return false
}
}
return true
}