-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
150 lines (117 loc) · 2.88 KB
/
chain.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
package SteGo
import (
"fmt"
"net/http"
"time"
)
type ServiceOperative interface {
BeforeCall(original, specific *http.Request) *http.Request
AfterCall(original *http.Request, res *http.Response) bool
}
type ServiceCall struct {
Method string
Name string
TimeoutSec int
Operative ServiceOperative
client *http.Client
}
type ServiceChainOperative interface {
Top(r *http.Request)
Bottom(r *http.Request, w http.ResponseWriter)
}
type ServiceChain struct {
Services []*ServiceCall
Operative ServiceChainOperative
}
func (sc *ServiceCall) Check() error {
//Check Method is correct
return nil
}
func (sc *ServiceCall) Call(sv *ServiceVersion, original *http.Request) error {
if sv == nil {
}
//Create the request for the single service
req, err := http.NewRequest(sc.Method, sv.Address, nil)
if err != nil {
}
newSpecific := sc.Operative.BeforeCall(original, req)
//if the client is not set
if sc.client == nil {
//Set the Timeout
sc.client = &http.Client{Timeout: time.Duration(sc.TimeoutSec) * time.Second}
}
fmt.Printf("address of the request:%v\n", newSpecific.URL)
res, err := sc.client.Do(newSpecific)
if err != nil {
return err
}
//the afterCall should set what it needs in the context of the original Requst
if ok := sc.Operative.AfterCall(original, res); !ok {
}
return nil
}
func (sc *ServiceChain) indexOf(name string) int {
for i, s := range sc.Services {
if s.Name == name {
return i
}
}
return -1
}
//The default is to add the Service Call at the bottom
func (sc *ServiceChain) AddServiceCall(s ServiceCall) error {
if err := s.Check(); err != nil {
return err
}
sc.Services = append(sc.Services, &s)
return nil
}
func (sc *ServiceChain) AddServiceCallAt(s ServiceCall, pos int) error {
if err := s.Check(); err != nil {
return err
}
sc.Services = append(append(sc.Services[:pos], &s), sc.Services[pos:]...)
return nil
}
func (sc *ServiceChain) AddServiceCallAfter(s ServiceCall, serviceName string) error {
if err := s.Check(); err != nil {
return err
}
pos := sc.indexOf(serviceName)
if pos == -1 {
}
sc.AddServiceCallAt(s, pos+1)
return nil
}
func (sc *ServiceChain) AddServiceCallBefore(s ServiceCall, serviceName string) error {
if err := s.Check(); err != nil {
return err
}
pos := sc.indexOf(serviceName)
if pos == -1 {
}
sc.AddServiceCallAt(s, pos)
return nil
}
func (sChain *ServiceChain) walkTheChain(srv map[string]*Service, version int, req *http.Request, w http.ResponseWriter) {
if sChain.Operative != nil {
sChain.Operative.Top(req)
}
for _, sc := range sChain.Services {
// Get the service from the name
s, ok := srv[sc.Name]
if !ok {
}
sv := s.Version(version)
if sv == nil {
}
err := sc.Call(sv, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
break
}
}
if sChain.Operative != nil {
sChain.Operative.Bottom(req, w)
}
}