This repository was archived by the owner on Mar 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
187 lines (153 loc) · 3.77 KB
/
Copy pathapi.go
File metadata and controls
187 lines (153 loc) · 3.77 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
package main
import (
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
)
var port = os.Getenv("PORT")
var calendarConfig = os.Getenv("MEETING_ROOM_CALENDARS")
var calendars map[string]string = make(map[string]string)
var client ApiClient
var googleApiKey = os.Getenv("MEETING_ROOM_API_KEY")
var googleClientId = os.Getenv("MEETING_ROOM_CLIENT_ID")
var authUsername = os.Getenv("MEETING_ROOM_AUTH_USER")
var authPassword = os.Getenv("MEETING_ROOM_AUTH_PASS")
var rooms map[string]Room = make(map[string]Room)
func main() {
client = ApiClient{
ClientId: googleClientId,
EncodedKey: googleApiKey,
}
calendars = parseCalendarConfig(calendarConfig)
startTicker()
go loadEvents()
log.Println("API is starting up on :" + port)
log.Println("Use Ctrl+C to stop")
http.HandleFunc("/rooms", roomsIndexHandler)
http.HandleFunc("/rooms/", roomsShowHandler)
http.ListenAndServe(":"+port, nil)
}
func Authenticate(user, realm string) string {
if user == authUsername {
d := sha1.New()
d.Write([]byte(authPassword))
e := base64.StdEncoding.EncodeToString(d.Sum(nil))
return "{SHA}" + e
}
return ""
}
func roomsIndexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
roomSet := RoomSet{
Rooms: rooms,
TotalRooms: len(calendars),
RoomsLoaded: len(rooms),
}
apiResponse := RoomSetApiResponse{
RoomSet: roomSet,
}
status := "ok"
if !roomsLoaded() {
status = "incomplete"
}
b, err := json.Marshal(apiResponse.present(status))
if err != nil {
log.Fatal("Error preparing JSON: ", err)
}
response := string(b)
fmt.Fprintf(w, response)
}
func roomsShowHandler(w http.ResponseWriter, r *http.Request) {
roomExp := regexp.MustCompile("^/rooms/([a-zA-Z0-9]+)$")
dummyReq := http.Request{}
var roomId string
m := roomExp.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, &dummyReq)
return
} else {
roomId = m[1]
}
room, ok := rooms[roomId]
if !ok {
http.NotFound(w, &dummyReq)
return
}
apiResponse := RoomApiResponse{
Room: room,
}
status := "ok"
if !roomsLoaded() {
status = "incomplete"
}
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(apiResponse.present(status))
if err != nil {
log.Fatal("Error preparing JSON: ", err)
}
response := string(b)
fmt.Fprintf(w, response)
}
func roomsLoaded() bool {
if len(calendars) > len(rooms) {
return false
}
return true
}
func loadEvents() {
log.Print("Loading events...")
client.Token = client.GetToken()
for calendarName, calendarId := range calendars {
go loadEventsForRoom(calendarName, calendarId)
}
}
func loadEventsForRoom(calendarName string, calendarId string) {
log.Printf("Loading %v", calendarName)
events, err := client.Api().Events.List(calendarId).
TimeMin(time.Now().Format(time.RFC3339)).
TimeMax(time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).Format(time.RFC3339)).
SingleEvents(true).
OrderBy("startTime").Do()
if err != nil {
log.Printf("Error loading room %v: %v", calendarName, err)
} else {
rooms[calendarName] = CreateRoomFromEvents(calendarName, events.Items)
log.Printf("Finished loading %v events for %v", len(rooms[calendarName].Events), calendarName)
}
}
func parseCalendarConfig(config string) map[string]string {
calendarMap := map[string]string{}
lines := strings.Split(config, ";")
for _, line := range lines {
if line == "" {
continue
}
parts := strings.Split(line, ",")
name := parts[0]
url := parts[1]
calendarMap[name] = url
}
return calendarMap
}
func startTicker() {
ticker := time.NewTicker(60 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
loadEvents()
case <-quit:
ticker.Stop()
return
}
}
}()
}