-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
166 lines (154 loc) · 4.67 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package server
import (
"context"
"encoding/json"
"go-common/library/log"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
)
type BroadcastService struct {
wg sync.WaitGroup
server *http.Server
proxy *BroadcastProxy
dispatch *CometDispatcher
}
func NewBroadcastService(addr string, proxy *BroadcastProxy, dispatch *CometDispatcher) (*BroadcastService, error) {
service := &BroadcastService{
proxy: proxy,
dispatch: dispatch,
}
service.wg.Add(1)
go func() {
defer service.wg.Done()
service.httpServerProcess(addr)
}()
return service, nil
}
func (service *BroadcastService) Close() {
service.server.Shutdown(context.Background())
}
func (service *BroadcastService) httpServerProcess(addr string) {
mux := http.NewServeMux()
mux.HandleFunc("/", service.Proxy)
mux.HandleFunc("/monitor/ping", service.Ping)
mux.HandleFunc("/dm/x/internal/v1/dispatch", service.Dispatch)
mux.HandleFunc("/dm/x/internal/v1/set_angry_value", service.SetAngryValue)
service.server = &http.Server{Addr: addr, Handler: mux}
service.server.SetKeepAlivesEnabled(true)
if err := service.server.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
panic(err)
}
}
}
func writeJsonResult(w http.ResponseWriter, r *http.Request, begin time.Time, v interface{}) {
data, err := json.Marshal(v)
if err != nil {
log.Error("[Http] write result json.Marshal:%v error:%v", v, err)
return
}
if _, err := w.Write([]byte(data)); err != nil {
log.Error("[Http] write result socket error:%v", err)
return
}
end := time.Now()
log.Info("request %s, response:%s, time cost:%s", r.RequestURI, data, end.Sub(begin).String())
}
func (service *BroadcastService) Proxy(w http.ResponseWriter, r *http.Request) {
service.proxy.HandleRequest(w, r)
}
func (service *BroadcastService) Ping(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
}
func (service *BroadcastService) Dispatch(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var result struct {
Code int `json:"code"`
Data struct {
DanmakuServer []string `json:"dm_server"`
DanmakuHost []string `json:"dm_host"`
} `json:"data"`
}
defer writeJsonResult(w, r, time.Now(), &result)
ip := r.URL.Query().Get("ip")
uid, _ := strconv.ParseInt(r.URL.Query().Get("uid"), 10, 64)
result.Code = 0
result.Data.DanmakuServer, result.Data.DanmakuHost = service.dispatch.Dispatch(ip, uid)
}
func (service *BroadcastService) SetAngryValue(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// 气人值的workaround
//go func() {
responseCollection, errCollection := service.proxy.RequestAllBackend(r.Method, "/dm/1/num/change", requestBody)
for i := range responseCollection {
if errCollection[i] != nil {
log.Error("SetAngryValue server:%d error:%+v", i, errCollection[i])
} else {
log.Info("SetAngryValue server:%d result:%s", i, responseCollection[i])
}
}
//}()
w.WriteHeader(http.StatusOK)
response, _ := json.Marshal(map[string]interface{}{"ret": 1})
w.Write(response)
return
}
func (service *BroadcastService) SetAngryValueV2(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
//go func() {
responseCollection, errCollection := service.proxy.RequestAllBackend(r.Method,
"/dm/x/internal/v2/set_angry_value", requestBody)
for i := range responseCollection {
if errCollection[i] != nil {
log.Error("SetAngryValueV2 server:%d error:%+v", i, errCollection[i])
w.WriteHeader(http.StatusServiceUnavailable)
response, _ := json.Marshal(map[string]interface{}{"code": -1, "msg": errCollection[i].Error()})
w.Write(response)
return
}
var result struct {
Code int `json:"code"`
Message string `json:"msg"`
}
if err := json.Unmarshal([]byte(responseCollection[i]), &result); err != nil {
log.Error("SetAngryValueV2 server:%d response:%s", i, responseCollection[i])
w.WriteHeader(http.StatusServiceUnavailable)
response, _ := json.Marshal(map[string]interface{}{"code": -2, "msg": responseCollection[i]})
w.Write(response)
return
}
if result.Code != 0 {
w.WriteHeader(http.StatusOK)
w.Write([]byte(responseCollection[i]))
return
}
}
//}()
w.WriteHeader(http.StatusOK)
response, _ := json.Marshal(map[string]interface{}{"code": 0})
w.Write(response)
return
}