/
api_v2.go
81 lines (66 loc) · 1.96 KB
/
api_v2.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
package api
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"
"github.com/Bnei-Baruch/gxydb-api/common"
"github.com/Bnei-Baruch/gxydb-api/pkg/httputil"
)
func (a *App) V2GetConfig(w http.ResponseWriter, r *http.Request) {
cfg := V2Config{
Gateways: make(map[string]map[string]*V2Gateway),
IceServers: common.Config.IceServers,
DynamicConfig: make(map[string]string),
}
gateways := a.cache.gateways.Values()
for _, gateway := range gateways {
if gateway.Disabled || gateway.RemovedAt.Valid {
continue
}
token, _ := a.cache.gatewayTokens.ByID(gateway.ID)
respGateway := &V2Gateway{
Name: gateway.Name,
URL: gateway.URL,
Type: gateway.Type,
Token: token,
}
if cfg.Gateways[gateway.Type] == nil {
cfg.Gateways[gateway.Type] = make(map[string]*V2Gateway)
}
cfg.Gateways[gateway.Type][gateway.Name] = respGateway
}
kvs := a.cache.dynamicConfig.Values()
for _, kv := range kvs {
cfg.DynamicConfig[kv.Key] = kv.Value
}
cfg.LastModified = a.cache.dynamicConfig.LastModified()
httputil.RespondWithJSON(w, http.StatusOK, cfg)
}
func (a *App) V2GetRoomsStatistics(w http.ResponseWriter, r *http.Request) {
stats, err := a.roomsStatisticsManager.GetAll()
if err != nil {
httputil.NewInternalError(err).Abort(w, r)
return
}
data := make(map[int]*V2RoomStatistics, len(stats))
for _, roomStats := range stats {
data[roomStats.R.Room.GatewayUID] = &V2RoomStatistics{OnAir: roomStats.OnAir}
}
httputil.RespondWithJSON(w, http.StatusOK, data)
}
func (a *App) HealthCheck(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
err := a.DB.(*sql.DB).PingContext(ctx)
if err != nil {
httputil.RespondWithError(w, http.StatusFailedDependency, fmt.Sprintf("DB ping: %s", err.Error()))
return
}
if ctx.Err() == context.DeadlineExceeded {
httputil.RespondWithError(w, http.StatusServiceUnavailable, "timeout")
return
}
httputil.RespondSuccess(w)
}