/
handle_1.0.go
executable file
·130 lines (121 loc) · 3.74 KB
/
handle_1.0.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
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"net/http"
"strconv"
"time"
"github.com/EPICPaaS/appmsgsrv/app"
myrpc "github.com/EPICPaaS/appmsgsrv/rpc"
)
// GetServer handle for server get
func GetServer(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
key := params.Get("k")
callback := params.Get("cb")
protoStr := params.Get("p")
res := map[string]interface{}{"ret": app.OK}
defer app.RetWrite(w, r, res, callback, time.Now())
if key == "" {
res["ret"] = app.ParamErr
return
}
proto, err := strconv.Atoi(protoStr)
if err != nil {
logger.Errorf("strconv.Atoi(\"%s\") error(%v)", protoStr, err)
res["ret"] = app.ParamErr
return
}
// Match a push-server with the value computed through ketama algorithm
node := myrpc.GetComet(key)
if node == nil {
res["ret"] = app.NotFoundServer
return
}
addr := node.Addr[proto]
if addr == nil || len(addr) == 0 {
res["ret"] = app.NotFoundServer
return
}
server := ""
// Select the best ip
if app.Conf.Router != "" {
server = routerCN.SelectBest(r.RemoteAddr, addr)
logger.Tracef("select the best ip:\"%s\" match with remoteAddr:\"%s\" , from ip list:\"%v\"", server, r.RemoteAddr, addr)
}
if server == "" {
logger.Tracef("remote addr: \"%s\" chose the ip: \"%s\"", r.RemoteAddr, addr[0])
server = addr[0]
}
res["data"] = map[string]interface{}{"server": server}
return
}
// GetOfflineMsg get offline mesage http handler.
func GetOfflineMsg(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
key := params.Get("k")
midStr := params.Get("m")
callback := params.Get("cb")
res := map[string]interface{}{"ret": app.OK}
defer app.RetWrite(w, r, res, callback, time.Now())
if key == "" || midStr == "" {
res["ret"] = app.ParamErr
return
}
mid, err := strconv.ParseInt(midStr, 10, 64)
if err != nil {
res["ret"] = app.ParamErr
logger.Errorf("strconv.ParseInt(\"%s\", 10, 64) error(%v)", midStr, err)
return
}
// RPC get offline messages
reply := &myrpc.MessageGetResp{}
args := &myrpc.MessageGetPrivateArgs{MsgId: mid, Key: key}
client := myrpc.MessageRPC.Get()
if client == nil {
res["ret"] = app.InternalErr
return
}
if err := client.Call(myrpc.MessageServiceGetPrivate, args, reply); err != nil {
logger.Errorf("myrpc.MessageRPC.Call(\"%s\", \"%v\", reply) error(%v)", myrpc.MessageServiceGetPrivate, args, err)
res["ret"] = app.InternalErr
return
}
if len(reply.Msgs) == 0 {
return
}
res["data"] = map[string]interface{}{"msgs": reply.Msgs}
return
}
// GetTime get server time http handler.
func GetTime(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
callback := params.Get("cb")
res := map[string]interface{}{"ret": app.OK}
now := time.Now()
defer app.RetWrite(w, r, res, callback, now)
res["data"] = map[string]interface{}{"timeid": now.UnixNano() / 100}
return
}