-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
36 lines (30 loc) · 837 Bytes
/
handlers.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
irccip "github.com/bbrks/irccip-go"
)
func (s *server) handleKeyPress() http.HandlerFunc {
type request struct {
KeyCode irccip.KeyCode `json:"key_code"`
}
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
write(w, http.StatusBadRequest, []byte(err.Error()))
}
defer r.Body.Close()
var req request
err = json.Unmarshal(b, &req)
if err != nil {
write(w, http.StatusBadRequest, []byte(err.Error()))
}
s.Log(LevelDebug, r.Context(), "sending key code: %s", req.KeyCode)
err = s.irccipClient.SendKeyCode(req.KeyCode)
if err != nil {
s.Log(LevelError, r.Context(), "SendKeyCode error: %v", err)
write(w, http.StatusBadRequest, []byte("SendKeyCode error: "+err.Error()))
}
}
}