-
Notifications
You must be signed in to change notification settings - Fork 202
/
driver.go
75 lines (66 loc) · 2.04 KB
/
driver.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 grpc_router
import (
"fmt"
"sync"
"github.com/eolinker/apinto/drivers/router/grpc-router/manager"
"github.com/eolinker/apinto/plugin"
"github.com/eolinker/apinto/service"
"github.com/eolinker/apinto/template"
"github.com/eolinker/eosc/log"
"github.com/eolinker/eosc/utils/config"
"github.com/eolinker/eosc"
)
var (
routerManager manager.IManger
pluginManager plugin.IPluginManager
once sync.Once
)
func Check(v *Config, workers map[eosc.RequireId]eosc.IWorker) error {
_, _, _, err := check(v, workers)
if err != nil {
return err
}
return nil
}
// Create 创建一个http路由驱动实例
func Create(id, name string, v *Config, workers map[eosc.RequireId]eosc.IWorker) (eosc.IWorker, error) {
log.Debug("create http router worker: ", pluginManager)
r := &GrpcRouter{
id: id,
name: name,
routerManager: routerManager,
pluginManager: pluginManager,
}
err := r.reset(v, workers)
if err != nil {
return nil, err
}
return r, err
}
// check 检查http路由驱动配置
func check(v interface{}, workers map[eosc.RequireId]eosc.IWorker) (*Config, service.IService, template.ITemplate, error) {
conf, ok := v.(*Config)
if !ok {
return nil, nil, nil, fmt.Errorf("get %s but %s %w", config.TypeNameOf(v), config.TypeNameOf(new(Config)), eosc.ErrorRequire)
}
ser, has := workers[conf.Service]
if !has {
return nil, nil, nil, fmt.Errorf("target %s: %w", conf.Service, eosc.ErrorRequire)
}
target, ok := ser.(service.IService)
if !ok {
return nil, nil, nil, fmt.Errorf("target name: %s type of %s,target %w", conf.Service, config.TypeNameOf(ser), eosc.ErrorNotGetSillForRequire)
}
var tmp template.ITemplate
if conf.Template != "" {
tp, has := workers[conf.Template]
if !has {
return nil, nil, nil, fmt.Errorf("target %s %w", conf.Template, eosc.ErrorRequire)
}
tmp, ok = tp.(template.ITemplate)
if !ok {
return nil, nil, nil, fmt.Errorf("target name: %s type of %s,target %w", conf.Template, config.TypeNameOf(tp), eosc.ErrorNotGetSillForRequire)
}
}
return conf, target, tmp, nil
}