-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
168 lines (154 loc) · 4.11 KB
/
api.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
167
168
package main
import (
"fmt"
"golang.org/x/net/websocket"
"net/http"
"time"
"strings"
)
//handle api request from api
type ApiServer struct {
ApiName string
AppId string
}
//TODO create a error standard
func (this *ApiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := this.CheckParams(r); err != nil {
returnMsg := fmt.Sprintf("{\"code\":400,\"msg\":\"%v\",\"time\":%v}", err.Error(), time.Now().Unix())
fmt.Fprint(w, returnMsg)
return
}
switch this.ApiName {
case "create-channel":
this.CreateChannel(w, r)
case "push":
this.Push(w, r)
case "broadcast":
this.Broadcast(w, r)
case "get-channel":
this.GetChannel(w, r)
case "close-channel":
this.CloseChannel(w, r)
case "app-status":
this.AppStatus(w, r)
default:
fmt.Fprint(w, "Invalid api")
}
}
func (this *ApiServer) CheckParams(r *http.Request) error {
appId := r.PostFormValue("app_id")
if appId == "" {
return Error("app_id missing")
}
appSecret := r.PostFormValue("app_secret")
if appSecret == "" {
return Error("app_secret missing")
}
config, ok := applications_config[appId]
if ok == false {
return Error("app_id invalid")
}
if config.AppSecret != appSecret {
return Error("app_secret invalid")
}
this.AppId = appId
return nil
}
func (this *ApiServer) CreateChannel(w http.ResponseWriter, r *http.Request) error {
uid := r.PostFormValue("uid")
if uid == "" {
return Error("uid missing")
}
appId := this.AppId
c, ok := applications[appId].Services[uid]
if ok == true {
msg := fmt.Sprintf("{\"uid\":\"%v\",\"token\":\"%v\"}", c.Uid, c.Token)
this.Success(msg, w)
return nil
}
token := GenerateId()
fmt.Println("token: ", token)
channelService := ChannelService{Uid: uid, Token: token}
applications[appId].Services[uid] = channelService
msg := fmt.Sprintf("{\"uid\":\"%v\",\"token\":\"%v\"}", channelService.Uid, channelService.Token)
this.Success(msg, w)
return nil
}
func (this *ApiServer) Push(w http.ResponseWriter, r *http.Request) error {
uidStr := r.PostFormValue("uid")
if uidStr == "" {
return Error("uid missing")
}
appId := this.AppId
uidSlice := strings.Split(uidStr, ",")
for _,uid := range uidSlice{
channelService, ok := applications[appId].Services[uid]
if ok == false {
continue
}
msg := r.PostFormValue("message")
for _, conn := range channelService.Conns {
if err := websocket.Message.Send(conn, msg); err != nil {
applications.removeConn(appId, uid, conn)
}
}
}
this.Success("", w)
return nil
}
func (this *ApiServer) Broadcast(w http.ResponseWriter, r *http.Request) error {
appId := this.AppId
channelServices := applications[appId]
msg := r.PostFormValue("message")
for uid, cs := range channelServices.Services {
for _, conn := range cs.Conns {
if err := websocket.Message.Send(conn, msg); err != nil {
applications.removeConn(appId, uid, conn)
}
}
}
this.Success("", w)
return nil
}
func (this *ApiServer) GetChannel(w http.ResponseWriter, r *http.Request) error {
uid := r.PostFormValue("uid")
if uid == "" {
return Error("uid missing")
}
appId := this.AppId
channelService, ok := applications[appId].Services[uid]
if ok == false {
return Error("channel not created")
}
msg := fmt.Sprintf("{\"uid\":\"%v\",\"token\":\"%v\",\"connections\":%d}", channelService.Uid, channelService.Token, len(channelService.Conns))
this.Success(msg, w)
return nil
}
func (this *ApiServer) CloseChannel(w http.ResponseWriter, r *http.Request) error {
uid := r.PostFormValue("uid")
if uid == "" {
return Error("uid missing")
}
appId := this.AppId
_, ok := applications[appId].Services[uid]
if ok == false {
return Error("uid invalid")
}
applications.removeChannel(appId, uid)
this.Success("", w)
return nil
}
func (this *ApiServer) AppStatus(w http.ResponseWriter, r *http.Request) error {
appId := this.AppId
channelNum := len(applications[appId].Services)
msg := fmt.Sprintf("{\"channelNum\":%d}", channelNum)
this.Success(msg, w)
return nil
}
func (this *ApiServer) Success(msg string, w http.ResponseWriter) {
if msg == "" {
msg = "\"success\""
}
returnMsg := fmt.Sprintf("{\"code\":0,\"result\":%v}", msg)
fmt.Fprint(w, returnMsg)
}