This repository has been archived by the owner on Apr 23, 2019. It is now read-only.
/
api.go
153 lines (124 loc) · 3.24 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
package main
import (
"encoding/json"
"net/http"
"golang.org/x/crypto/bcrypt"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/syhlion/gwspack"
)
func AppList(w http.ResponseWriter, r *http.Request) {
rs, err := Model.GetAll()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), 500)
return
}
var tmprs []AppDataResult
for _, v := range rs {
tmprs = append(tmprs, v)
}
result := AppListResult{
Total: len(rs),
Data: tmprs,
}
log.Info(r.RemoteAddr, " ListApp Scuess")
json.NewEncoder(w).Encode(result)
}
func Unregister(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
app_key := params["app_key"]
if app_key == "" {
log.Warn(r.RemoteAddr, " app_key empty")
http.Error(w, "app_key empty", 404)
return
}
err := Model.Delete(app_key)
if err != nil {
log.Warn(r.RemoteAddr, " ", err)
http.Error(w, err.Error(), 500)
return
}
nr := NormalResult{Message: "Scuess"}
log.Info(r.RemoteAddr, " ", app_key, " Unregister Scuess")
json.NewEncoder(w).Encode(nr)
}
func ListClient(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
app_key := params["app_key"]
if app_key == "" {
log.Warn(r.RemoteAddr, " app_key empty")
http.Error(w, "app_key empty", 404)
return
}
app := gwspack.Get(app_key)
onlineUsers := app.List()
lo := ListOnlineResult{
AppKey: app_key,
Total: len(onlineUsers),
UserTags: onlineUsers,
}
log.Info(r.RemoteAddr, " GetAppUsers")
json.NewEncoder(w).Encode(lo)
}
//註冊
func Register(w http.ResponseWriter, r *http.Request) {
app_key := r.FormValue("app_key")
auth_password := r.FormValue("auth_password")
auth_account := r.FormValue("auth_account")
connect_hook := r.FormValue("connect_hook")
request_ip := r.RemoteAddr
if app_key == "" || request_ip == "" || auth_password == "" || auth_account == "" {
log.Warn(r.RemoteAddr, " ", "app_name || request_ip empty")
http.Error(w, "app_name || request_op empty", 404)
return
}
//bcrypt encoding
hash_password, err := bcrypt.GenerateFromPassword([]byte(auth_account+auth_password), 5)
if err != nil {
log.Warn(r.RemoteAddr, " ", app_key, " ", auth_password, " hash error")
http.Error(w, "hash error", 404)
return
}
auth_password = string(hash_password)
err = Model.Register(app_key, auth_account, auth_password, connect_hook, request_ip)
if err != nil {
log.Warn(r.RemoteAddr, " ", err)
http.Error(w, "app_key repeat", 500)
return
}
result := AppResult{
AppKey: app_key,
ConnectHook: connect_hook,
RequestIP: request_ip,
}
json.NewEncoder(w).Encode(result)
}
//Push
func Push(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
app_key := params["app_key"]
content := r.FormValue("content")
//option
user_tag := r.FormValue("user_tag")
if app_key == "" || content == "" {
log.Warn(r.RemoteAddr, " empty app_key || content")
http.Error(w, "app_key || content empty", 400)
return
}
app := gwspack.Get(app_key)
b := []byte(content)
if user_tag == "" {
app.SendAll(b)
} else {
app.SendByRegex(user_tag, b)
}
pushResult := &PushResult{
AppKey: app_key,
Content: content,
UserTag: user_tag,
Total: app.CountById(),
}
log.Info(r.RemoteAddr, " message send ", content)
json.NewEncoder(w).Encode(pushResult)
}