Mercurial > code > home > repos > gcalendarwatch
annotate calsync/main.go @ 91:62caecb41dfd default tip
fix tag
author | drewp@bigasterisk.com |
---|---|
date | Fri, 03 Jan 2025 18:06:13 -0800 |
parents | c2578b4a8d9d |
children |
rev | line source |
---|---|
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
1 package main |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
3 /* |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
4 go build && ./gcalendarwatch |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
5 |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
6 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
7 let python continue to serve these: |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
8 Route('/', getRoot), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
9 Route('/graph/calendar/upcoming', StaticGraph(agendaGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
10 use https://github.com/cayleygraph/quad |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
11 Route('/graph/calendar/upcoming/events', GraphEvents(agendaGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
12 use https://github.com/tmaxmax/go-sse |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
13 Route('/graph/calendar/countdown', StaticGraph(countdownGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
14 Route('/graph/calendar/countdown/events', GraphEvents(countdownGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
15 Route('/graph/currentEvents', StaticGraph(currentEventsGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
16 Route('/graph/currentEvents/events', GraphEvents(currentEventsGraph)), |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
17 */ |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
19 import ( |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
20 "context" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
21 "log" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
22 "net/http" |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
23 "time" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
24 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
25 "bigasterisk.com/go/gcalendarwatch/gcalclient" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
26 "bigasterisk.com/go/gcalendarwatch/mongoclient" |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
27 "bigasterisk.com/go/gcalendarwatch/notificationrouter" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
28 "github.com/gorilla/mux" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
29 ) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
30 |
82 | 31 const startupJitter = 60 * time.Second |
80 | 32 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
33 func main() { |
60 | 34 startLogging() |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
35 ctx := context.Background() |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
36 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
37 gc, err := gcalclient.New(ctx) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
38 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
39 log.Fatal(err) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
40 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
41 defer gc.Close() |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
42 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
43 mc, err := mongoclient.New(ctx) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
44 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
45 log.Fatal(err) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
46 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
47 defer mc.Close() |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
48 |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
49 notifications := notificationrouter.New() |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
50 |
60 | 51 // todo: if a cal is deleted, we don't clean up its events, even upon |
52 // restart. | |
57 | 53 err = updateMongoCalsToMatchGoogleOnce(mc, gc) |
54 if err != nil { | |
55 log.Fatal(err) | |
56 } | |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
57 |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
58 initialSyncBack := (7 * 24) * time.Hour |
86 | 59 initialSyncAhead := (120 * 24) * time.Hour |
80 | 60 err = updateMongoEventsToMatchGoogleForever(mc, gc, initialSyncBack, initialSyncAhead, notifications, startupJitter) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
61 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
62 log.Fatal(err) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
63 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
64 |
57 | 65 r := mux.NewRouter() |
66 http.Handle("/", r) | |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
59 | 68 r.HandleFunc("/", homePage) |
69 r.HandleFunc("/gcalendarwatch", homePage) | |
70 r.HandleFunc("/gcalendarwatch/notifications", notifications.NotificationHandler).Methods("POST") | |
65 | 71 |
72 addr := ":8080" | |
59 | 73 log.Println("serving /gcalendarwatch/notifications on", addr) |
74 log.Fatal(http.ListenAndServe(addr, nil)) | |
75 } | |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
76 |
60 | 77 func startLogging() { |
78 log.SetFlags(log.LstdFlags | log.Lshortfile) | |
79 log.Println(` | |
80 ================================================== | |
81 calsync | |
65 | 82 ----------------------------------------------`) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
83 } |
59 | 84 |
85 func homePage(w http.ResponseWriter, r *http.Request) { | |
65 | 86 w.Write([]byte("calsync service for calendar updates. See https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/metrics?project=bigasterisk-910")) |
87 } |