-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
62 lines (50 loc) · 1.19 KB
/
api.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
package go_remote
import (
"context"
"reflect"
)
type ServiceAPI map[string]int
type API struct {
Services map[string]ServiceAPI `json:"api"`
Data map[string]interface{} `json:"data"`
WebSocket bool `json:"websocket,omitempty"`
}
// JSON returns a json string representation of the end point
func (s *Server) GetAPI(ctx context.Context) API {
out := API{}
out.Services = make(map[string]ServiceAPI)
out.Data = make(map[string]interface{})
for key, value := range s.services {
out.Services[key] = value.GetAPI()
}
for key, value := range s.data {
if value.isConstant {
out.Data[key] = value.value
} else {
raw, ok, err := s.Dependencies.Value(value.rtype, ctx)
if !ok {
log.Errorf("can't resolve api variable: %s", key)
continue
}
if err != nil {
log.Errorf("error during resolving api variable: %s\n%f", key, err)
continue
}
if raw.Kind() == reflect.Ptr {
raw = raw.Elem()
}
out.Data[key] = raw.Interface()
}
}
if s.config.WebSocket {
out.WebSocket = true
}
return out
}
func (s *service) GetAPI() ServiceAPI {
out := ServiceAPI(make(map[string]int))
for key := range s.method {
out[key] = 1
}
return out
}