-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
147 lines (129 loc) · 3.48 KB
/
http.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
package main
import (
"fmt"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"html/template"
"io/ioutil"
"net/http"
"strings"
"time"
)
func serveHTTP() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fn := "html/index.html"
t, err := template.ParseFiles(fn)
if err != nil {
fmt.Fprint(w, "Error parsing template file:", fn, " with error:", err)
return
}
if t.Execute(w, map[string]string{"ServerHost": r.Host}) != nil {
fmt.Fprint(w, "Internal error: cannot execute template")
}
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := WS.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
go WSwriter(conn)
WSreader(conn)
})
log.WithFields(log.Fields{"listen": *listen}).Warn("Starting HTTP Listener")
err := http.ListenAndServe(*listen, nil)
log.WithFields(log.Fields{"listen": *listen}).Fatal("Received an error from HTTP Listener: ", err)
}
func WSwriter(ws *websocket.Conn) {
pingTicker := time.NewTicker(pingPeriod)
dataTicker := time.NewTicker(100 * time.Millisecond)
var lastUpdate time.Time
defer func() {
pingTicker.Stop()
dataTicker.Stop()
ws.Close()
}()
for {
select {
case <-pingTicker.C:
if ws.SetWriteDeadline(time.Now().Add(writeWait)) != nil {
return
}
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
case <-dataTicker.C:
// Check for LastUpdate
broadcastData.RLock()
if broadcastData.LastUpdate.After(lastUpdate) {
data := broadcastData.Data
lastUpdate = broadcastData.LastUpdate
broadcastData.RUnlock()
if ws.SetWriteDeadline(time.Now().Add(writeWait)) != nil {
return
}
if err := ws.WriteMessage(websocket.TextMessage, []byte(data)); err != nil {
return
}
} else {
broadcastData.RUnlock()
}
}
}
}
func WSreader(ws *websocket.Conn) {
defer ws.Close()
ws.SetReadLimit(512)
if ws.SetReadDeadline(time.Now().Add(pongWait)) != nil {
return
}
ws.SetPongHandler(func(string) error {
return ws.SetReadDeadline(time.Now().Add(pongWait))
})
for {
_, _, err := ws.ReadMessage()
if err != nil {
break
}
}
}
func makeRequest(event ConfigEvent, params map[string]string) {
// Prepare request params
method := "GET"
data := ""
url := event.HttpGet
if len(event.HttpPost) > 0 {
method = "POST"
url = event.HttpPost
data = event.HttpPostContent
}
for k, v := range params {
url = strings.ReplaceAll(url, "{"+k+"}", v)
data = strings.ReplaceAll(data, "{"+k+"}", v)
}
// Prepare request
client := &http.Client{}
req, err := http.NewRequest(method, url, strings.NewReader(data))
if err != nil {
log.WithFields(log.Fields{"action": "notify", "url": url}).Info("Error creating HTTP request: ", err)
return
}
// Add headers
for k, v := range event.HttpHeader {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{"action": "notify", "method": method, "url": url, "state": "fail"}).Info("Error making HTTP request: ", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.WithFields(log.Fields{"action": "notify", "method": method, "url": url, "state": "fail"}).Info("Error reading body of HTTP request: ", err)
return
}
log.WithFields(log.Fields{"action": "notify", "method": method, "url": url, "state": "ok", "resp-body-len": len(body)}).Debug("HTTP Notification is sent")
}