-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (135 loc) · 3.54 KB
/
main.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"sync"
"time"
)
var (
roomName = envRequired("PEEPHOLE_ROOM_NAME")
httpAddr = envOrDefault("PEEPHOLE_HTTP_ADDR", ":9339")
cacheExpiry = envDuration("PEEPHOLE_CACHE_EXPIRY", 5*time.Second)
prosodyHTTPHost = envOrDefault("XMPP_SERVER", "xmpp.meet.jitsi")
prosodyHTTPPort = envOrDefault("PROSODY_HTTP_PORT", "5280")
roomCensusURL = (&url.URL{
Scheme: "http",
Host: net.JoinHostPort(prosodyHTTPHost, prosodyHTTPPort),
Path: "/room-census",
}).String()
)
func envRequired(name string) string {
val := os.Getenv(name)
if val == "" {
slog.Error("missing environment variable", slog.String("name", name))
os.Exit(1)
}
return val
}
func envOrDefault(name, fallback string) string {
val := os.Getenv(name)
if val == "" {
return fallback
}
return val
}
func envDuration(name string, fallback time.Duration) time.Duration {
val := os.Getenv(name)
if val == "" {
return fallback
}
t, err := time.ParseDuration(val)
if err != nil {
slog.Error("invalid environment variable", slog.String("name", name), slog.Any("error", err))
os.Exit(1)
}
return t
}
type room struct {
RoomName string `json:"room_name"`
Participants int `json:"participants"`
CreatedTime int64 `json:"created_time,omitempty"`
}
type roomList []room
func (l *roomList) UnmarshalJSON(data []byte) error {
// Attempt to unmarshal data as a array of rooms
err := json.Unmarshal(data, (*[]room)(l))
if err != nil {
// Check if the empty list is represented as `{}`
if emptyErr := json.Unmarshal(data, &struct{}{}); emptyErr == nil {
return nil
}
}
return err
}
var cache struct {
sync.Mutex
lastUpdated time.Time
value room
}
func fetchRoom() (*room, error) {
cache.Lock()
defer cache.Unlock()
// Return cached value if not yet expired
if time.Since(cache.lastUpdated) < cacheExpiry {
return &cache.value, nil
}
// Fetch "room census" from internal API
resp, err := http.Get(roomCensusURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch room census from %q: %w", roomCensusURL, err)
}
defer resp.Body.Close()
// Parse JSON response payload into roomList
var payload struct {
RoomCensus roomList `json:"room_census,omitempty"`
}
err = json.NewDecoder(resp.Body).Decode(&payload)
if err != nil {
return nil, fmt.Errorf("failed to parse room census payload: %w", err)
}
// Extract configured room from room list. If no census for the configured
// room is found, we fall back on just displaying zero participants
var found = room{
RoomName: roomName,
Participants: 0,
}
for _, r := range payload.RoomCensus {
if r.RoomName == roomName {
found = r
break
}
}
cache.lastUpdated = time.Now()
cache.value = found
return &found, nil
}
func peephole(w http.ResponseWriter) error {
room, err := fetchRoom()
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(room)
}
func main() {
slog.Info("starting HTTP server", "addr", httpAddr)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if err := peephole(w); err != nil {
slog.Error("failed to serve request", slog.Any("error", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
})
err := http.ListenAndServe(httpAddr, handler)
if !errors.Is(err, http.ErrServerClosed) {
slog.Error("listener failed", slog.Any("error", err))
os.Exit(1)
}
}