-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-gstatus.go
78 lines (61 loc) · 1.82 KB
/
http-gstatus.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
// 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 "/gateway/<gatewayid>" HTTP topic
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
// Handle inbound HTTP requests to fetch log files
func inboundWebGatewayUpdateHandler(rw http.ResponseWriter, req *http.Request) {
stats.Count.HTTP++
// We have an update request
body, err := ioutil.ReadAll(req.Body)
if err != nil {
fmt.Printf("GW: Error reading HTTP request body: \n%v\n", req)
return
}
// Unmarshal it
var ttg TTGateReq
err = json.Unmarshal(body, &ttg)
if err != nil {
fmt.Printf("*** Received badly formatted Device Update request:\n%v\n", body)
return
}
requestor, _, abusive := getRequestorIPv4(req)
if abusive {
return
}
fmt.Printf("\n%s Received gateway update for %s %s (%s)\n", LogTime(), ttg.GatewayID, ttg.GatewayName, ttg.GatewayRegion)
go WriteGatewayStatus(ttg, requestor)
stats.Count.HTTPGUpdate++
}
// Handle inbound HTTP requests to fetch log files
func inboundWebGatewayStatusHandler(rw http.ResponseWriter, req *http.Request) {
stats.Count.HTTP++
// Set response mime type
rw.Header().Set("Content-Type", "application/json")
// Log it
if len(req.RequestURI) > len(TTServerTopicGatewayStatus) {
filename := req.RequestURI[len(TTServerTopicGatewayStatus):]
if filename != "" {
fmt.Printf("%s Gateway information request for %s\n", LogTime(), filename)
// Open the file
file := SafecastDirectory() + TTGatewayStatusPath + "/" + filename + ".json"
fd, err := os.Open(file)
if err != nil {
io.WriteString(rw, ErrorString(err))
return
}
defer fd.Close()
// Copy the file to output
io.Copy(rw, fd)
return
}
}
}