annotate calsync/notificationrouter/notificationrouter.go @ 66:629d9ad289fe

doc & refactor
author drewp@bigasterisk.com
date Fri, 06 Sep 2024 17:26:11 -0700
parents 8aee4f5c4bdd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
1 package notificationrouter
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
2
66
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
3 // Holds the map of (random) watchIds and the sync functions for the affected
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
4 // calendars. When google sends a notification, this object calls the right
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
5 // sync.
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
6
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
7 import (
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
8 "log"
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
9 "net/http"
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
10 "time"
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
11
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
12 "github.com/dchest/uniuri"
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
13 )
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
14
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
15 type SyncFunc func()
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
16
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
17 type NotificationRouter struct {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
18 handlers map[string]SyncFunc
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
19 }
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
20
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
21 func New() *NotificationRouter {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
22 return &NotificationRouter{
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
23 handlers: make(map[string]SyncFunc),
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
24 }
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
25 }
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
26
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
27 func (nr *NotificationRouter) AddHandler(watchId string, handler SyncFunc, lifetime time.Duration) {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
28 nr.handlers[watchId] = handler
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
29 go func() {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
30 time.Sleep(lifetime)
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
31 delete(nr.handlers, watchId)
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
32 }()
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
33 }
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
34
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
35 func (nr *NotificationRouter) NotificationHandler(w http.ResponseWriter, r *http.Request) {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
36 watchId := r.Header.Get("X-Goog-Channel-ID")
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
37 if nr.handlers[watchId] == nil {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
38 log.Println("incoming notification for unknown watch id", watchId, "(ignoring)")
66
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
39 return
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
40 }
66
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
41 // todo: The first notification on a given watchId is the confirmation,
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
42 // and the sync func is probably (surely?) not needed.
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
43 log.Println("incoming notification for known watch id", watchId)
629d9ad289fe doc & refactor
drewp@bigasterisk.com
parents: 61
diff changeset
44 nr.handlers[watchId]()
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
45 }
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
46
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
47 func NewWatchId() string {
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
48 return uniuri.New() + "_" + time.Now().Format("15_04_05")
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
diff changeset
49 }