-
Notifications
You must be signed in to change notification settings - Fork 35
/
manager.go
174 lines (159 loc) · 4.37 KB
/
manager.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package endly
import (
_ "embed"
"fmt"
"github.com/satori/go.uuid"
"github.com/viant/scy/cred/secret"
"github.com/viant/toolbox"
"reflect"
"strings"
"sync"
"time"
)
// RunWithoutLogging runs action with logging disabled for supplied context request and response. Response has to be pointer or nil
func RunWithoutLogging(context *Context, request, result interface{}) error {
logging := context.Logging
context.SetLogging(false)
defer func() {
context.Logging = logging
}()
return Run(context, request, result)
}
// Run runs action for supplied context request and response. Response has to be pointer or nil
func Run(context *Context, request, result interface{}) error {
if context == nil {
manager := New()
context = manager.NewContext(nil)
defer context.Close()
}
var resultValue reflect.Value
if result != nil {
resultValue = reflect.ValueOf(result)
if resultValue.Kind() != reflect.Ptr {
return fmt.Errorf("expected result as pointer, but had %T", result)
}
}
manager, err := context.Manager()
if err != nil {
return err
}
response, err := manager.Run(context, request)
if err != nil {
return err
}
if serviceResponse, ok := result.(*ServiceResponse); ok {
serviceResponse.Response = response
serviceResponse.Status = "ok"
if err != nil {
serviceResponse.Status = "error"
serviceResponse.Err = err
serviceResponse.Error = err.Error()
}
return err
}
if result == nil || response == nil {
return nil
}
return toolbox.DefaultConverter.AssignConverted(result, response)
}
type manager struct {
name string
version string
serviceByID map[string]Service
serviceByRequestType map[reflect.Type]Service
}
func (m *manager) Name() string {
return m.name
}
func (m *manager) Version() string {
return m.version
}
// Service returns service for supplied request or name.
func (m *manager) Service(input interface{}) (Service, error) {
if serviceID, ok := input.(string); ok {
if result, found := m.serviceByID[serviceID]; found {
return result, nil
}
} else if toolbox.IsStruct(input) {
if result, found := m.serviceByRequestType[reflect.TypeOf(input)]; found {
return result, nil
}
}
var available = toolbox.MapKeysToStringSlice(m.serviceByID)
return nil, fmt.Errorf("failed to lookup service: '%T' in [%v]", input, strings.Join(available, ","))
}
func (m *manager) Register(service Service) {
m.serviceByID[service.ID()] = service
for _, action := range service.Actions() {
if actionRoute, err := service.Route(action); err == nil {
request := actionRoute.RequestProvider()
m.serviceByRequestType[reflect.TypeOf(request)] = service
}
}
}
func (m *manager) NewContext(ctx toolbox.Context) *Context {
if ctx == nil {
ctx = toolbox.NewContext()
}
sessionID := toolbox.AsString(time.Now().Unix())
if UUID, err := uuid.NewV1(); err == nil {
sessionID = UUID.String()
}
var result = &Context{
SessionID: sessionID,
Context: ctx,
Wait: &sync.WaitGroup{},
AsyncUnsafeKeys: make(map[interface{}]bool),
Secrets: secret.New(),
}
_ = result.Put(serviceManagerKey, m)
return result
}
// New returns a new manager.
func New() Manager {
var result = &manager{
name: AppName,
version: GetVersion(),
serviceByID: make(map[string]Service),
serviceByRequestType: make(map[reflect.Type]Service),
}
result.Register(newNopService())
for _, provider := range *Registry {
result.Register(provider())
}
return result
}
// Run runs action for supplied request, returns service action response or error
func (m *manager) Run(context *Context, request interface{}) (interface{}, error) {
if !toolbox.IsStruct(request) {
return nil, fmt.Errorf("expected request but had %T", request)
}
manager, err := context.Manager()
if err != nil {
manager = m
}
service, err := manager.Service(request)
if err != nil {
return nil, err
}
if context == nil {
context = manager.NewContext(toolbox.NewContext())
defer context.Close()
}
response := service.Run(context, request)
return response.Response, response.Err
}
// Services returns manager serviceByID or error
func Services(mgr interface{}) map[string]Service {
var manager, ok = mgr.(*manager)
if !ok {
return nil
}
return manager.serviceByID
}
//go:embed Version
var version string
// GetVersion returns endly version
func GetVersion() string {
return version
}