-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiconnector.go
92 lines (76 loc) · 2.23 KB
/
apiconnector.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
package core
import (
"github.com/drharryhe/has/common/hconf"
"github.com/drharryhe/has/common/herrors"
"github.com/drharryhe/has/common/htypes"
"github.com/drharryhe/has/utils/hrandom"
"github.com/drharryhe/has/utils/hruntime"
)
/**********************
This package is to defined the API interfaces based on kern service/server. including:
* Web API
* GRPC API
* RPCX API
* WebSocket API
* Socket API
* UDP API
问题: 是否允许同一个端口支持多种API???
*********************/
type ConnectorConf struct {
EntityConfBase
Lang string
Packer string
}
type BaseConnector struct {
Gateway IAPIGateway
server IServer
Packer IAPIDataPacker
instance IAPIConnector
class string
Options *ConnectorOptions
}
type DecoderFunc func(body interface{}) (htypes.Map, *herrors.Error)
type ConnectorOptions struct {
// The key of the map is MIME, should be like application/json or fiber.MIMEApplicationJSON.
BodyDecoders map[string]DecoderFunc
}
func (this *BaseConnector) Open(gw IAPIGateway, ins IAPIConnector) *herrors.Error {
this.Gateway = gw
this.server = gw.Server()
this.instance = ins
this.class = hruntime.GetObjectName(ins.(IEntity).Config())
hconf.Load(ins.(IEntity).Config())
if val, err := ins.(IEntity).EntityStub().Manage(ManageGetConfigItems, htypes.Map{"Packer": nil}); err != nil {
return err
} else {
packer, _ := val.(htypes.Map)["Packer"].(string)
if packer == "" {
return herrors.ErrSysInternal.New("[%s]'s [Packer] not configured or invalid type", this.class)
}
this.Packer = this.Gateway.Packer(packer)
if this.Packer == nil {
return herrors.ErrSysInternal.New("packer [" + packer + "] not found")
}
}
return nil
}
func (this *BaseConnector) Class() string {
return this.class
}
func (this *BaseConnector) Close() {
}
func (this *BaseConnector) Server() IServer {
return this.server
}
func (this *BaseConnector) EntityMeta() *EntityMeta {
if this.instance.(IEntity).Config().GetEID() == "" {
this.instance.(IEntity).Config().SetEID(hrandom.UuidWithoutDash())
hconf.Save()
}
return &EntityMeta{
ServerEID: this.Server().(IEntity).Config().GetEID(),
EID: this.instance.(IEntity).Config().GetEID(),
Type: EntityTypeConnector,
Class: this.class,
}
}