/
gateway.go
66 lines (57 loc) · 1.88 KB
/
gateway.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
package resource
import (
"github.com/yarf-framework/yarf"
)
// EnableMQTTGateway controls whether clients are informed about the MQTT gateway
var EnableMQTTGateway = true
// Gateway composites resource
// Gateways are the UnderLX's form of real-time communication between server and clients
// They are used to support communication paradigms that are poorly supported by conventional HTTP,
// such as publish-subscribe or unreliable connections (as in UDP)
type Gateway struct {
resource
mqttGateways []MQTTGatewayInfoProvider
}
// MQTTGatewayInfoProvider contains the methods the Gateway resource uses to provide information about a MQTTGateway
type MQTTGatewayInfoProvider interface {
Hostname() string
Port() uint16
IsTLS() bool
MQTTVersion() string
}
// apiGateway contains information about a gateway
type apiGateway struct {
Protocol string `msgpack:"protocol" json:"protocol"` // only current valid value is "mqtt"
}
// apiGateway contains information about a MQTT gateway
type apiMQTTGateway struct {
apiGateway `msgpack:",inline"`
Host string `msgpack:"host" json:"host"`
Port uint16 `msgpack:"port" json:"port"`
MqttVersion string `msgpack:"pVer" json:"pVer"`
TLS bool `msgpack:"tls" json:"tls"`
}
// RegisterMQTTGateway associates another MQTTGateway with this resource
func (r *Gateway) RegisterMQTTGateway(mqtt MQTTGatewayInfoProvider) *Gateway {
r.mqttGateways = append(r.mqttGateways, mqtt)
return r
}
// Get serves HTTP GET requests on this resource
func (r *Gateway) Get(c *yarf.Context) error {
data := []interface{}{}
if EnableMQTTGateway {
for _, g := range r.mqttGateways {
data = append(data, apiMQTTGateway{
apiGateway: apiGateway{
Protocol: "mqtt",
},
Host: g.Hostname(),
Port: g.Port(),
MqttVersion: g.MQTTVersion(),
TLS: g.IsTLS(),
})
}
}
RenderData(c, data, "s-maxage=10")
return nil
}