-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
108 lines (96 loc) · 2.32 KB
/
command.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const (
updatePeerType = yota
udpateViewType
updateRandomType
joinType
)
type updatePeerCommand struct {
Id string `json:"name"`
Content string `json:"content"`
}
type updateViewCommand struct {
Content string `json:"content"`
}
type updateRandomCommand struct {
Percentage int `json:"percentage"`
Num int `json:"num"`
Content string `json:"content"`
}
type joinCommand struct {
Id string `json:"id"`
Bind string `json:"bind"`
}
type commandServer struct {
server *http.Server
commands chan<- interface{}
}
func newCommandServer(bind string, commands chan<- interface{}) *commandServer {
var (
mux *http.ServeMux
server *http.Server
cs CommandServer
)
mux = http.NewServeMux()
server = &http.Server{
Handler: mux,
Addr: bind,
}
cs = CommandServer{server, commands}
mux.HandleFunc("/update/peer", cs.commandHandler(updatePeerType))
mux.HandleFunc("/update/view", cs.commandHandler(updateViewType))
mux.HandleFunc("/update/random", cs.commandHandler(updateRandomType))
mux.HandleFunc("/join", cs.commandHandler(joinType))
return &cs
}
func (cs *commandServer) listen() error {
log.Printf("HTTP command server started on %s\n", cs.server.Addr)
return cs.server.ListenAndServe()
}
func (cs *commandServer) terminate() error {
log.Println("HTTP command server terminating")
return cs.server.Close()
}
type httpHandler func(http.ResponseWriter, *http.Request)
func (cs *commandServer) commandHandler(typ int) httpHandler {
return func(w http.ResponseWriter, r *http.Request) {
var (
decoder = json.NewDecoder(r.Body)
err error
cmd interface{}
)
switch typ {
case updatePeerType:
cmd = updatePeerCommand{}
err = decoder.Decode(&cmd)
case updateViewType:
cmd = updateViewCommand{}
err = decoder.Decode(&cmd)
case updateRandomType:
cmd = updateRandomCommand{}
err = decoder.Decode(&cmd)
case joinType:
cmd = joinCommand{}
err = decoder.Decode(&cmd)
default:
err = fmt.Errorf("unrecognized payload command type: %d", typ)
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
select {
case cs.commands <- &cmd:
case r.Context().Done():
http.Error(w, "server closing. retry!", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}