forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
servanthandle.go
75 lines (68 loc) · 1.93 KB
/
servanthandle.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
package tars
import (
"fmt"
"net/http"
"strconv"
"strings"
"tars/transport"
)
//AddServant add dispatch and interface for object.
func AddServant(v dispatch, f interface{}, obj string) {
addServantCommon(v, f, obj, false)
}
//AddServantWithContext add dispatch and interface for object, which have ctx,context
func AddServantWithContext(v dispatch, f interface{}, obj string) {
addServantCommon(v, f, obj, true)
}
func addServantCommon(v dispatch, f interface{}, obj string, withContext bool) {
objRunList = append(objRunList, obj)
cfg, ok := tarsConfig[obj]
if !ok {
TLOG.Debug("servant obj name not found ", obj)
return
}
TLOG.Debug("add:", cfg)
jp := NewTarsProtocol(v, f, withContext)
s := transport.NewTarsServer(jp, cfg)
goSvrs[obj] = s
}
//AddHttpServant add http servant handler for obj.
func AddHttpServant(mux *TarsHttpMux, obj string) {
cfg, ok := tarsConfig[obj]
if !ok {
TLOG.Debug("servant obj name not found ", obj)
return
}
TLOG.Debug("add http server:", cfg)
objRunList = append(objRunList, obj)
appConf := GetServerConfig()
addrInfo := strings.SplitN(cfg.Address, ":", 2)
var port int
if len(addrInfo) == 2 {
port, _ = strconv.Atoi(addrInfo[1])
}
httpConf := &TarsHttpConf{
Container: appConf.Container,
AppName: fmt.Sprintf("%s.%s", appConf.App, appConf.Server),
Version: appConf.Version,
IP: addrInfo[0],
Port: int32(port),
SetId: appConf.Setdivision,
}
mux.SetConfig(httpConf)
s := &http.Server{Addr: cfg.Address, Handler: mux}
httpSvrs[obj] = s
}
// AddServantWithProtocol adds a servant with protocol and obj
func AddServantWithProtocol(proto transport.TarsProtoCol, obj string) {
objRunList = append(objRunList, obj)
cfg, ok := tarsConfig[obj]
if !ok {
//ReportNotifyInfo(NOTIFY_ERROR, "servant obj name not found:"+obj)
TLOG.Debug("servant obj name not found ", obj)
panic(ok)
}
TLOG.Debug("add:", cfg)
s := transport.NewTarsServer(proto, cfg)
goSvrs[obj] = s
}