-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
806 lines (674 loc) · 20.8 KB
/
Copy pathmain.go
File metadata and controls
806 lines (674 loc) · 20.8 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package main
import (
"context"
"database/sql"
"embed"
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/SherClockHolmes/webpush-go"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/joho/godotenv"
"google.golang.org/genai"
"html/template"
"log"
"log/slog"
"mime"
_ "modernc.org/sqlite"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
)
type location struct {
tz *time.Location
lat float32
lon float32
ianaName string
displayName string
}
// pageTemplate stores all pre-compiled HTML templates for the application
type pageTemplate struct {
summary *template.Template
}
// summaryTemplateData stores template data for summary.html
type summaryTemplateData struct {
Summary string
Location string
LocationName string
}
// updateSubscription is the request body for creating/updating registration
type updateSubscription struct {
Subscription webpush.Subscription `json:"subscription"`
Locations []string `json:"locations"`
RemoveLocations []string `json:"removeLocations"`
}
// registeredSubscription represents a registered webpush subscription.
type registeredSubscription struct {
ID uuid.UUID `json:"id"`
Subscription *webpush.Subscription `json:"-"`
Locations []string `json:"locations"`
}
type webpushNotificationPayload struct {
Summary string `json:"summary"`
Location string `json:"location"`
}
type metAPIData struct {
Properties struct {
TimeSeries []map[string]any `json:"timeseries"`
} `json:"properties"`
}
type updateSummaryOptions struct {
locKey string
location *location
pushUpdate bool
}
type state struct {
ctx context.Context
metAPIUserAgent string
genai *genai.Client
template pageTemplate
db *sql.DB
dbMutex sync.Mutex
// summaries maps location keys to their latest weather summary
summaries sync.Map
// summaryChans stores a map of location key to the corresponding summary channel
// which is used to track summary updates
summaryChans map[string]chan string
// subscriptions maps location keys to the list of registered subscriptions
// that are subscribed to updates for the location
subscriptions map[string][]*registeredSubscription
// subscriptionsMutex syncs writes to subscriptions
subscriptionsMutex sync.Mutex
vapidSubject string
// vapidPublicKey is the base64 url encoded VAPID public key
vapidPublicKey string
// vapidPrivateKey is the base64 url encoded VAPID private key
vapidPrivateKey string
}
//go:embed web
var webDir embed.FS
var envKeys = []string{"GEMINI_API_KEY", "MET_API_USER_AGENT", "VAPID_SUBJECT", "VAPID_PRIVATE_KEY_BASE64", "VAPID_PUBLIC_KEY_BASE64"}
//go:embed prompt.txt
var prompt string
var supportedLocations = map[string]*location{
"london": {nil, 51.507351, -0.127758, "Europe/London", "London"},
"sf": {nil, 37.774929, -122.419418, "America/Los_Angeles", "San Francisco"},
"sj": {nil, 37.338207, -121.886330, "America/Los_Angeles", "San Jose"},
"la": {nil, 34.052235, -118.243683, "America/Los_Angeles", "Los Angeles"},
"nyc": {nil, 40.712776, -74.005974, "America/New_York", "New York City"},
"tokyo": {nil, 35.689487, 139.691711, "Asia/Tokyo", "Tokyo"},
"singapore": {nil, 1.290270, 103.851959, "Asia/Singapore", "Singapore"},
"manila": {nil, 14.599512, 120.984222, "Asia/Manila", "Manila"},
"hk": {nil, 22.317053, 114.169547, "Asia/Hong_Kong", "Hong Kong"},
"warsaw": {nil, 52.229675, 21.012230, "Europe/Warsaw", "Warsaw"},
"zurich": {nil, 47.369019, 8.538030, "Europe/Zurich", "Zurich"},
"berlin": {nil, 52.520008, 13.404954, "Europe/Berlin", "Berlin"},
"dubai": {nil, 25.204849, 55.270782, "Asia/Dubai", "Dubai"},
"paris": {nil, 48.864716, 2.349014, "Europe/Paris", "Paris"},
"stockholm": {nil, 59.329323, 18.068581, "Europe/Stockholm", "Stockholm"},
"amsterdam": {nil, 52.370216, 4.895168, "Europe/Amsterdam", "Amsterdam"},
}
func main() {
port := flag.Int("port", 8080, "the port that the server should listen on")
genKeys := flag.Bool("generate-vapid-keys", false, "generate a new vapid key pair, which will be outputted to stdout.")
flag.Parse()
if *genKeys {
generateKeys()
} else if err := startServer(*port); err != nil {
log.Fatal(err)
}
}
func startServer(port int) error {
slog.Info("starting 7am...")
err := loadTimeZones()
if err != nil {
return err
}
_ = godotenv.Load()
err = checkEnv()
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get cwd: %w", err)
}
p := filepath.Join(wd, "data")
err = os.MkdirAll(p, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create data directory at %v: %w", p, err)
}
slog.Info("data directory created", "path", p)
db, err := initDB()
if err != nil {
return fmt.Errorf("failed to initialize db: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
genaiClient, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
if err != nil {
return fmt.Errorf("failed to initialize gemini client: %w\n", err)
}
summaryHTML, _ := webDir.ReadFile("web/summary.html")
summaryPageTemplate, _ := template.New("summary.html").Parse(string(summaryHTML))
state := state{
ctx: ctx,
metAPIUserAgent: os.Getenv("MET_API_USER_AGENT"),
template: pageTemplate{
summary: summaryPageTemplate,
},
summaries: sync.Map{},
summaryChans: map[string]chan string{},
genai: genaiClient,
db: db,
dbMutex: sync.Mutex{},
subscriptions: map[string][]*registeredSubscription{},
vapidSubject: os.Getenv("VAPID_SUBJECT"),
vapidPublicKey: os.Getenv("VAPID_PUBLIC_KEY_BASE64"),
vapidPrivateKey: os.Getenv("VAPID_PRIVATE_KEY_BASE64"),
}
fetchInitialSummaries(&state)
var schedulers []gocron.Scheduler
// schedule periodic updates of weather summary for each supported location
for locKey, loc := range supportedLocations {
s, err := gocron.NewScheduler(gocron.WithLocation(loc.tz))
if err != nil {
return fmt.Errorf("failed to create gocron scheduler for %v: %w", locKey, err)
}
_, err = s.NewJob(
gocron.DailyJob(1, gocron.NewAtTimes(gocron.NewAtTime(7, 0, 0))),
gocron.NewTask(func(ctx context.Context) {
updateSummary(ctx, &state, updateSummaryOptions{
locKey: locKey,
location: loc,
pushUpdate: true,
})
}),
)
if err != nil {
return fmt.Errorf("failed to scheduel gocron job for %v: %w", locKey, err)
}
schedulers = append(schedulers, s)
c := make(chan string)
state.subscriptions[locKey] = []*registeredSubscription{}
state.summaryChans[locKey] = c
// listen for summary updates, and publish updates to all update subscribers via web push
go listenForSummaryUpdates(&state, locKey, c)
s.Start()
slog.Info("update job scheduled", "location", locKey)
}
err = loadSubscriptions(&state)
if err != nil {
return fmt.Errorf("failed to load existing subscriptions: %w", err)
}
http.HandleFunc("/", handleHTTPRequest(&state))
slog.Info("server starting", "port", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
return fmt.Errorf("failed to start http server: %w", err)
}
for _, s := range schedulers {
s.Shutdown()
}
slog.Info("7am shut down")
return nil
}
func generateKeys() {
priv, pub, err := webpush.GenerateVAPIDKeys()
if err != nil {
log.Fatal(err)
}
fmt.Println("all keys are base64 url encoded.")
fmt.Printf("public key: %v\n", pub)
fmt.Printf("private key: %v\n", priv)
}
func checkEnv() error {
var missing []string
for _, k := range envKeys {
v := os.Getenv(k)
if v == "" {
missing = append(missing, k)
}
}
if len(missing) > 0 {
return fmt.Errorf("missing env: %v", strings.Join(missing, ", "))
}
return nil
}
func handleHTTPRequest(state *state) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
path := strings.TrimPrefix(request.URL.Path, "/")
if path == "" {
if request.Method == "" || request.Method == "GET" {
index, _ := webDir.ReadFile("web/index.html")
writer.Write(index)
} else {
writer.WriteHeader(http.StatusMethodNotAllowed)
}
} else if path == "instructions" {
f, _ := webDir.ReadFile("web/instructions.html")
writer.Write(f)
} else if path == "vapid" {
if request.Method == "" || request.Method == "GET" {
writer.Write([]byte(state.vapidPublicKey))
} else {
writer.WriteHeader(http.StatusMethodNotAllowed)
}
} else if strings.HasPrefix(path, "registrations") {
if path == "registrations" && request.Method == "POST" {
defer request.Body.Close()
update := updateSubscription{}
err := json.NewDecoder(request.Body).Decode(&update)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
reg, err := registerSubscription(state, &update)
if err != nil {
slog.Error("web push subscription registration failed", "error", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
err = json.NewEncoder(writer).Encode(reg)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
} else {
slog.Info("new web push registration", "id", reg.ID)
}
} else if request.Method == "PATCH" || request.Method == "DELETE" {
parts := strings.Split(path, "/")
if len(parts) < 2 {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
regID, err := uuid.Parse(parts[1])
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
switch request.Method {
case "PATCH":
defer request.Body.Close()
update := updateSubscription{}
err = json.NewDecoder(request.Body).Decode(&update)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
reg, err := updateRegisteredSubscription(state, regID, &update)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
writer.WriteHeader(http.StatusNotFound)
} else {
writer.WriteHeader(http.StatusInternalServerError)
}
} else {
json.NewEncoder(writer).Encode(reg)
slog.Info("web push registration updated", "id", reg.ID, "locations", strings.Join(reg.Locations, ","))
}
case "DELETE":
err = deleteSubscription(state, regID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
writer.WriteHeader(http.StatusNotFound)
} else {
writer.WriteHeader(http.StatusInternalServerError)
}
} else {
writer.WriteHeader(http.StatusNoContent)
slog.Info("web push registration deleted", "id", regID)
}
}
} else {
writer.WriteHeader(http.StatusMethodNotAllowed)
}
} else if strings.HasPrefix(path, "api/") {
if origin := request.Header.Get("Origin"); origin != "" {
writer.Header().Set("Access-Control-Allow-Origin", origin)
}
if strings.HasPrefix(path, "api/summary/") {
switch request.Method {
case "OPTIONS":
writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
writer.WriteHeader(http.StatusOK)
case "GET":
location := strings.TrimPrefix(path, "api/summary/")
summary, ok := state.summaries.Load(location)
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
response := map[string]string{
"summary": strings.TrimSpace(summary.(string)),
}
writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(writer).Encode(response)
default:
writer.WriteHeader(http.StatusMethodNotAllowed)
}
} else {
writer.WriteHeader(http.StatusNotFound)
}
} else {
if request.Method != "" && request.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
summary, ok := state.summaries.Load(path)
if ok {
loc := supportedLocations[path]
state.template.summary.Execute(writer, summaryTemplateData{summary.(string), path, loc.displayName})
} else {
f, err := webDir.ReadFile("web/" + path)
if err != nil {
writer.WriteHeader(http.StatusNotFound)
} else {
m := mime.TypeByExtension(filepath.Ext(path))
if m != "" {
writer.Header().Set("Content-Type", m)
}
writer.Write(f)
}
}
}
}
}
func initDB() (*sql.DB, error) {
db, err := sql.Open("sqlite", "file:data/data.sqlite")
if err != nil {
log.Fatalln("failed to initialize database")
}
_, err = db.Exec(`
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
CREATE TABLE IF NOT EXISTS subscriptions(
id TEXT PRIMARY KEY,
locations TEXT NOT NULL,
subscription_json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS summaries(
location TEXT PRIMARY KEY,
summary TEXT NOT NULL
);
`)
if err != nil {
return nil, err
}
return db, nil
}
func loadSubscriptions(state *state) error {
rows, err := state.db.Query(`SELECT id, locations, subscription_json FROM subscriptions;`)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var id string
var locations string
var j string
err := rows.Scan(&id, &locations, &j)
if err != nil {
slog.Warn("unable to load a subscription", "error", err)
continue
}
s := webpush.Subscription{}
err = json.Unmarshal([]byte(j), &s)
if err != nil {
slog.Warn("invalid web push subscription json encountered", "id", id, "error", err)
continue
}
reg := ®isteredSubscription{
ID: uuid.MustParse(id),
Locations: strings.Split(locations, ","),
Subscription: &s,
}
for _, l := range reg.Locations {
state.subscriptions[l] = append(state.subscriptions[l], reg)
}
}
return nil
}
func updateRegisteredSubscription(state *state, id uuid.UUID, update *updateSubscription) (*registeredSubscription, error) {
j, err := json.Marshal(update.Subscription)
if err != nil {
return nil, err
}
rows, err := state.db.Query("SELECT locations FROM subscriptions WHERE id = ?", id)
if err != nil {
return nil, err
}
rows.Next()
var locStr string
err = rows.Scan(&locStr)
if err != nil {
return nil, err
}
rows.Close()
// not very proud of this one
// ideally the list of locations should be stored in a separate table
// but since the list is very small, and im too lazy to bring in a separate table
// this should be fine for now
locs := strings.Split(locStr, ",")
locs = append(locs, update.Locations...)
locs = slices.DeleteFunc(locs, func(l string) bool {
return slices.Contains(update.RemoveLocations, l)
})
locs = slices.Compact(locs)
_, err = state.db.Exec(
"UPDATE subscriptions SET subscription_json = ?, locations = ? WHERE id = ?",
string(j), strings.Join(locs, ","), id,
)
if err != nil {
return nil, err
}
reg := ®isteredSubscription{
ID: id,
Subscription: &update.Subscription,
Locations: locs,
}
state.subscriptionsMutex.Lock()
for _, l := range update.Locations {
state.subscriptions[l] = append(state.subscriptions[l], reg)
}
for _, l := range update.RemoveLocations {
state.subscriptions[l] = slices.DeleteFunc(state.subscriptions[l], func(s *registeredSubscription) bool {
return s.ID == reg.ID
})
}
state.subscriptionsMutex.Unlock()
return reg, nil
}
func registerSubscription(state *state, sub *updateSubscription) (*registeredSubscription, error) {
j, err := json.Marshal(sub.Subscription)
if err != nil {
return nil, fmt.Errorf("invalid web push subscription object: %w", err)
}
id, err := uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("unable to generate id for subscription: %w", err)
}
locs := slices.Compact(sub.Locations)
_, err = state.db.Exec(
"INSERT INTO subscriptions (id, locations, subscription_json) VALUES (?, ?, ?);",
id, strings.Join(locs, ","), string(j),
)
if err != nil {
return nil, fmt.Errorf("unable to insert into subscriptions table: %w", err)
}
reg := registeredSubscription{
ID: id,
Subscription: &sub.Subscription,
Locations: locs,
}
state.subscriptionsMutex.Lock()
for _, l := range sub.Locations {
state.subscriptions[l] = append(state.subscriptions[l], ®)
}
state.subscriptionsMutex.Unlock()
return ®, nil
}
func deleteSubscription(state *state, regID uuid.UUID) error {
_, err := state.db.Exec("DELETE FROM subscriptions WHERE id = ?", regID)
return err
}
func loadTimeZones() error {
for locKey, loc := range supportedLocations {
tz, err := time.LoadLocation(loc.ianaName)
if err != nil {
return fmt.Errorf("failed to load time zone for %v: %w", locKey, err)
}
loc.tz = tz
}
return nil
}
func fetchInitialSummaries(state *state) {
var wg sync.WaitGroup
for locKey, loc := range supportedLocations {
wg.Add(1)
go func() {
ctx, cancel := context.WithCancel(state.ctx)
defer cancel()
defer wg.Done()
summary := ""
rows, err := state.db.QueryContext(ctx, "SELECT summary FROM summaries WHERE location = ?", locKey)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
slog.Warn("unable to get cached weather summary", "location", locKey, "error", err)
} else if err == nil {
defer rows.Close()
ok := rows.Next()
if ok {
err = rows.Scan(&summary)
if err != nil {
slog.Warn("unable to get cached weather summary", "location", locKey, "error", err)
}
}
}
if summary == "" {
updateSummary(state.ctx, state, updateSummaryOptions{
locKey: locKey,
location: loc,
pushUpdate: false,
})
} else {
state.summaries.Store(locKey, summary)
}
}()
}
wg.Wait()
}
func updateSummary(ctx context.Context, state *state, opts updateSummaryOptions) {
locKey := opts.locKey
loc := opts.location
slog.Info("updating weather summary", "location", locKey)
today := time.Now().In(loc.tz)
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=%v&lon=%v", loc.lat, loc.lon), nil)
if err != nil {
slog.Error("failed to query weather data", "location", locKey, "error", err)
return
}
req.Header.Set("User-Agent", state.metAPIUserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
slog.Error("failed to query weather data", "location", locKey, "error", err)
return
}
defer resp.Body.Close()
data := metAPIData{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
slog.Error("failed to decode received weather data", "location", locKey, "error", err)
return
}
y, m, d := today.Date()
t := slices.DeleteFunc(data.Properties.TimeSeries, func(series map[string]any) bool {
if ts, ok := series["time"].(string); ok {
t, err := time.Parse(time.RFC3339, ts)
if err != nil {
return false
}
ty, tm, td := t.In(loc.tz).Date()
return !(y == ty && m == tm && d == td)
}
return false
})
b, err := json.Marshal(t)
if err != nil {
slog.Error("failed to marshal processed time series data", "location", locKey, "error", err)
return
}
weatherJSON := string(b)
result, err := state.genai.Models.GenerateContent(ctx, "gemini-2.0-flash", []*genai.Content{{
Parts: []*genai.Part{
{Text: fmt.Sprintf(prompt, today.Format("2006-02-01"), loc.displayName, loc.displayName)},
{Text: weatherJSON},
},
}}, nil)
if err != nil {
slog.Error("failed to generate weather summary", "location", locKey, "error", err)
return
}
summary := result.Text()
state.dbMutex.Lock()
_, err = state.db.ExecContext(ctx, "INSERT OR REPLACE INTO summaries (location, summary) VALUES (?, ?)", locKey, summary)
if err != nil {
slog.Warn("unable to cache generated weather summary to db", "location", locKey, "error", err)
}
state.dbMutex.Unlock()
state.summaries.Store(locKey, summary)
if opts.pushUpdate {
c := state.summaryChans[locKey]
if len(state.subscriptions[locKey]) > 0 {
c <- summary
}
}
slog.Info("updated weather summary", "location", locKey)
}
func listenForSummaryUpdates(state *state, locKey string, c <-chan string) {
opts := webpush.Options{
Subscriber: state.vapidSubject,
VAPIDPublicKey: state.vapidPublicKey,
VAPIDPrivateKey: state.vapidPrivateKey,
TTL: 30,
}
for {
select {
case summary := <-c:
payload := webpushNotificationPayload{
Summary: summary,
Location: locKey,
}
b, err := json.Marshal(&payload)
if err != nil {
slog.Error("failed to create web push notification payload", "location", locKey, "error", err)
continue
}
subs := state.subscriptions[locKey]
slog.Info("pushing weather summary to subscribers", "count", len(subs), "location", locKey)
var wg sync.WaitGroup
for _, sub := range subs {
wg.Add(1)
go func() {
defer wg.Done()
_, err := webpush.SendNotificationWithContext(state.ctx, b, sub.Subscription, &opts)
if err != nil {
slog.Warn("unable to send web push to subscription", "id", sub.ID, "location", locKey, "error", err)
}
}()
}
wg.Wait()
slog.Info("pushed weather summary to subscribers", "count", len(subs), "location", locKey)
case <-state.ctx.Done():
return
}
}
}