-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendarpi.go
73 lines (65 loc) · 1.75 KB
/
calendarpi.go
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"time"
)
/*
TODO: Only allow /scripts, /css, and /images to be downloaded
TODO: When adding disable button and show spinner
TODO: Make private key for user and embed as hidden on page. Verify this before saving.
TODO: support adding a new calendar (To add a new calendar to srv.Insert(calendar) - returns CalendarsInsertCall)
*/
const configfile = "conf.json"
func main() {
config := readConfig()
SetOauthConfig(config)
SetExchangeConfig(config)
go RunServer(config)
runSyncLoop(config)
log.Println("Server is exiting")
}
func readConfig() Config {
file, err := os.Open("conf.json")
if err != nil {
if os.IsExist(err) {
log.Fatal(err)
}
data, _ := ioutil.ReadFile("conf.template.json")
ioutil.WriteFile("conf.json", data, 777)
log.Fatal("No configuration file found. A new configuration file has automatically been created for you. Please edit conf.json and fill in the correct values.")
}
decoder := json.NewDecoder(file)
config := Config{}
if err = decoder.Decode(&config); err != nil {
log.Fatal("Unable to parse the configuration file 'conf.json'", err)
}
return config
}
func runSyncLoop(config Config) {
for true {
sleepTime := time.Duration(config.MinutesBetweenSync() * 60 * 1e9)
time.Sleep(sleepTime)
users := GetUsers()
for _, user := range users {
user.State = syncing
user.Save()
appointments := GetExchangeAppointments(user)
log.Println("len:", len(appointments))
events, err := getGCalAppointments(user)
if err != nil {
log.Fatal(err)
}
err = mergeEvents(user, appointments, events)
if err != nil {
user.State = successfulsync
} else {
user.State = syncingerror
}
user.LastSync = time.Now()
user.Save()
}
}
}