@@ -24,15 +24,19 @@ type Config struct {
24
24
DbPassword string
25
25
}
26
26
27
- var config Config
27
+ type app struct {
28
+ conf Config
29
+ api * cloudflare.API
30
+ zoneId string
31
+ }
28
32
29
- func loadConfig (fn string ) error {
33
+ func ( a * app ) loadConfig (fn string ) error {
30
34
f , err := os .Open (fn )
31
35
if err != nil {
32
36
return err
33
37
}
34
38
defer f .Close ()
35
- return json .NewDecoder (f ).Decode (& config )
39
+ return json .NewDecoder (f ).Decode (& a . conf )
36
40
}
37
41
38
42
func loadAvg (text string ) ([]float64 , error ) {
@@ -54,25 +58,27 @@ func loadAvg(text string) ([]float64, error) {
54
58
return res , nil
55
59
}
56
60
57
- // setSecurityLevel sets the security level. value must be one of
58
- // off, essentially_off, low, medium, high, under_attack.
59
- func setSecurityLevel (value string ) error {
60
- api , err := cloudflare .NewWithAPIToken (config .ApiKey )
61
- if err != nil {
62
- return err
63
- }
64
- zoneID , err := api .ZoneIDByName (config .Domain )
61
+ func (a * app ) init () error {
62
+ var err error
63
+ a .api , err = cloudflare .NewWithAPIToken (a .conf .ApiKey )
65
64
if err != nil {
66
65
return err
67
66
}
67
+ a .zoneId , err = a .api .ZoneIDByName (a .conf .Domain )
68
+ return err
69
+ }
68
70
69
- currentLevel , err := currentLevel (api , zoneID )
71
+ // setSecurityLevel sets the security level. value must be one of
72
+ // off, essentially_off, low, medium, high, under_attack.
73
+ // If the cf security is already at the reauested level, then do nothing.
74
+ func (a * app ) setSecurityLevel (value string ) error {
75
+ currentLevel , err := a .currentLevel ()
70
76
if currentLevel == value {
71
77
return nil
72
78
}
73
79
74
80
log .Println ("setting security level to" , value )
75
- _ , err = api .UpdateZoneSettings (context .TODO (), zoneID , []cloudflare.ZoneSetting {
81
+ _ , err = a . api .UpdateZoneSettings (context .TODO (), a . zoneId , []cloudflare.ZoneSetting {
76
82
{
77
83
ID : securityLevel ,
78
84
Value : value ,
@@ -81,15 +87,15 @@ func setSecurityLevel(value string) error {
81
87
return err
82
88
}
83
89
84
- func mustSetSecurityLevel (value string ) {
85
- err := setSecurityLevel (value )
90
+ func ( a * app ) mustSetSecurityLevel (value string ) {
91
+ err := a . setSecurityLevel (value )
86
92
if err != nil {
87
93
log .Fatalln (err )
88
94
}
89
95
}
90
96
91
- func currentLevel ( api * cloudflare. API , zoneID string ) (string , error ) {
92
- settings , err := api .ZoneSettings (context .TODO (), zoneID )
97
+ func ( a * app ) currentLevel ( ) (string , error ) {
98
+ settings , err := a . api .ZoneSettings (context .TODO (), a . zoneId )
93
99
if err != nil {
94
100
return "" , err
95
101
}
@@ -114,7 +120,13 @@ func main() {
114
120
if err != nil {
115
121
log .Fatalln (err )
116
122
}
117
- err = loadConfig (* cf )
123
+ var a app
124
+ err = a .loadConfig (* cf )
125
+ if err != nil {
126
+ log .Fatalln (err )
127
+ }
128
+
129
+ err = a .init ()
118
130
if err != nil {
119
131
log .Fatalln (err )
120
132
}
@@ -131,23 +143,23 @@ func main() {
131
143
log .Println ("freeMem" , humanize .Bytes (freeMem ), "load" , la )
132
144
if freeMem < mb {
133
145
log .Println ("free memory is below" , * minBytesStr )
134
- mustSetSecurityLevel ("under_attack" )
146
+ a . mustSetSecurityLevel ("under_attack" )
135
147
return
136
148
}
137
- err = checkDb (config )
149
+ err = a . checkDb ()
138
150
if err != nil {
139
151
log .Println ("checkDb returned" , err )
140
- mustSetSecurityLevel ("under_attack" )
152
+ a . mustSetSecurityLevel ("under_attack" )
141
153
return
142
154
}
143
155
144
156
if la [0 ] >= * maxLoad {
145
157
log .Println ("Load average is" , la , "setting level to under_attack" )
146
- mustSetSecurityLevel ("under_attack" )
158
+ a . mustSetSecurityLevel ("under_attack" )
147
159
return
148
160
}
149
161
if la [0 ] < * minLoad && la [1 ] < * minLoad && la [2 ] < * minLoad {
150
- mustSetSecurityLevel (* defaultSecurityLevel )
162
+ a . mustSetSecurityLevel (* defaultSecurityLevel )
151
163
return
152
164
}
153
165
}
0 commit comments