-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-id.go
117 lines (102 loc) · 3.51 KB
/
http-id.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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Inbound support for the "/device/<device_urn>" HTTP topic
package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
// Types of redirect page
const (
deviceDashboard = iota
deviceMap
deviceProfile
)
// Handle inbound HTTP requests to redirect to the appropriate page
func inboundWebIDHandler(rw http.ResponseWriter, req *http.Request) {
webPageRedirectHandler(rw, req, req.RequestURI[len(TTServerTopicDashboard):], deviceDashboard)
}
func inboundWebDashboardHandler(rw http.ResponseWriter, req *http.Request) {
webPageRedirectHandler(rw, req, req.RequestURI[len(TTServerTopicDashboard):], deviceDashboard)
}
func inboundWebMapHandler(rw http.ResponseWriter, req *http.Request) {
webPageRedirectHandler(rw, req, req.RequestURI[len(TTServerTopicMap):], deviceMap)
}
func inboundWebProfileHandler(rw http.ResponseWriter, req *http.Request) {
webPageRedirectHandler(rw, req, req.RequestURI[len(TTServerTopicProfile):], deviceProfile)
}
// Handle inbound HTTP requests to redirect to a certain page
func webPageRedirectHandler(rw http.ResponseWriter, req *http.Request, deviceUID string, pageType int) {
stats.Count.HTTP++
// Because it came from an URL, decode the deviceUID string
decodedDeviceUID, decodeErr := url.QueryUnescape(deviceUID)
if decodeErr == nil {
deviceUID = decodedDeviceUID
}
// Read the device status
isAvail, isReset, ds := ReadDeviceStatus(deviceUID)
if !isAvail || isReset {
io.WriteString(rw, "device is not recognized")
return
}
// Construct a redirect URL based upon the class of the device
url := ""
switch ds.DeviceClass {
case "geigiecast":
fallthrough
case "geigiecast-zen":
fallthrough
case "pointcast":
fallthrough
case "safecast-air":
fallthrough
case "ngeigie":
fallthrough
case "":
switch pageType {
case deviceDashboard:
url = "https://grafana.safecast.cc/d/DFSxrOLWk/safecast-device-details?orgId=1&from=now-7d&to=now&refresh=15m&var-device_urn=" + deviceUID
case deviceMap:
url = "https://map.safecast.org"
case deviceProfile:
url = "https://api.safecast.org/en-US/device_stories/" + deviceUID
}
case "product:com.blues.radnote":
fallthrough
case "product:net.ozzie.ray:radnote":
fallthrough
case "product:org.airnote.solar.rad.v1":
switch pageType {
case deviceDashboard:
url = "https://grafana.safecast.cc/d/ndnJJuYMk/safecast-radnote?orgId=1&var-device_urn=" + deviceUID
case deviceMap:
url = "https://grafana.safecast.cc/d/t_Z6DlbGz/safecast-all-airnotes?orgId=1"
case deviceProfile:
url = "https://api.safecast.org/en-US/device_stories/" + deviceUID
}
case "product:org.airnote.solar.air.v1":
fallthrough
case "product:com.blues.airnote":
fallthrough
case "product:org.airnote.solar.v1":
switch pageType {
case deviceDashboard:
url = "https://grafana.safecast.cc/d/7wsttvxGk/airnote-device-details?orgId=1&var-device_urn=" + deviceUID
case deviceMap:
url = "https://grafana.safecast.cc/d/t_Z6DlbGz/safecast-all-airnotes?orgId=1"
case deviceProfile:
url = "https://api.safecast.org/en-US/device_stories/" + deviceUID
}
default:
errMsg := fmt.Sprintf("class %s for device %s is not recognized", ds.DeviceClass, deviceUID)
fmt.Printf("%s\n", errMsg)
io.WriteString(rw, errMsg)
return
}
// Perform the redirect
fmt.Printf("REDIRECTING %s to %s\n", ds.DeviceClass, url)
http.Redirect(rw, req, url, http.StatusTemporaryRedirect)
}