7
7
"io"
8
8
"log"
9
9
"net/http"
10
+ "os"
10
11
"regexp"
11
12
"strings"
12
- "sync"
13
13
"time"
14
14
15
15
"github.com/schachmat/wego/iface"
@@ -119,7 +119,39 @@ func (opmeteo *openmeteoConfig) Setup() {
119
119
flag .StringVar (& opmeteo .apiKey , "openmeteo-api-key" , "" , "openmeteo backend: the api `KEY` to use if commercial usage" )
120
120
flag .BoolVar (& opmeteo .debug , "openmeteo-debug" , false , "openmeteo backend: print raw requests and responses" )
121
121
opmeteo .cache = make (map [string ]cachedData ) // initializing a cache
122
- opmeteo .cacheDuration = 1 * time .Hour // setting the cache duation to 1 hour
122
+ flag .DurationVar (& opmeteo .cacheDuration , "openmeteo-cache-duration" , 1 * time .Hour , "openmeteo backend: duration for caching weather data" ) // adding the cache duration flag
123
+ opmeteo .LoadCacheFromDisk ()
124
+ }
125
+
126
+ func (opmeteo * openmeteoConfig ) SaveCacheToDisk () {
127
+ file , err := os .Create ("/tmp/wego_cache.json" )
128
+
129
+ if err != nil {
130
+ log .Fatal (err )
131
+ }
132
+
133
+ defer file .Close ()
134
+
135
+ json .NewEncoder (file ).Encode (opmeteo .cache )
136
+ }
137
+
138
+ func (opmeteo * openmeteoConfig ) LoadCacheFromDisk () {
139
+ file , err := os .Open ("/tmp/wego_cache.json" )
140
+
141
+ if os .IsNotExist (err ) {
142
+ log .Println ("Cache file not found" )
143
+ return
144
+ }
145
+
146
+ if err != nil {
147
+ log .Fatal (err )
148
+ }
149
+
150
+ defer file .Close ()
151
+
152
+ if err := json .NewDecoder (file ).Decode (& opmeteo .cache ); err != nil {
153
+ log .Println ("Failed to load cache from disk, starting with an empty cache:" , err )
154
+ }
123
155
}
124
156
125
157
func (opmeteo * openmeteoConfig ) parseDaily (dailyInfo Hourly ) []iface.Day {
@@ -171,21 +203,17 @@ func parseCurCond(current curCond) (ret iface.Cond) {
171
203
172
204
}
173
205
174
- var mu sync.Mutex
175
-
176
206
func (opmeteo * openmeteoConfig ) Fetch (location string , numdays int ) iface.Data {
177
207
var ret iface.Data
178
208
var params []string
179
209
var loc string
180
210
181
211
//checking cache before fething the data
182
- mu .Lock ()
183
212
if cached , ok := opmeteo .cache [location ]; ok {
184
213
if time .Since (cached .Timestamp ) < opmeteo .cacheDuration {
185
214
return cached .Data
186
215
}
187
216
}
188
- mu .Unlock ()
189
217
190
218
if numdays <= 0 {
191
219
log .Fatal ("Number of days less than 1 " )
@@ -207,9 +235,11 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
207
235
208
236
res , err := http .Get (requri )
209
237
if err != nil {
210
- log .Fatal ("Unable to get weather data: " , err )
238
+ log .Println ("Unable to get weather data: " , err )
239
+ return ret
211
240
} else if res .StatusCode != 200 {
212
- log .Fatal ("Unable to get weather data: http status " , res .StatusCode )
241
+ log .Println ("Unable to get weather data: http status " , res .StatusCode )
242
+ return ret
213
243
}
214
244
defer res .Body .Close ()
215
245
@@ -226,7 +256,8 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
226
256
227
257
var resp openmeteoResponse
228
258
if err = json .Unmarshal (body , & resp ); err != nil {
229
- log .Println (err )
259
+ log .Println ("Failed to unmarshal weather data:" , err )
260
+ return ret
230
261
}
231
262
232
263
ret .Current = parseCurCond (resp .Current )
@@ -240,12 +271,12 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
240
271
ret .Forecast = forecast
241
272
}
242
273
243
- mu .Lock ()
244
274
opmeteo .cache [location ] = cachedData {
245
275
Data : ret , //store the fetched data in the cache
246
276
Timestamp : time .Now (), // store the current time in the cache
247
277
}
248
- mu .Unlock ()
278
+
279
+ opmeteo .SaveCacheToDisk ()
249
280
250
281
return ret
251
282
}
0 commit comments