-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
298 lines (236 loc) · 6.42 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"context"
"fmt"
"io"
"net"
"net/url"
"os"
"os/signal"
"path"
"path/filepath"
"sync"
"time"
"github.com/google/uuid"
"github.com/jpillora/overseer"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/TykTechnologies/mserv/api"
config "github.com/TykTechnologies/mserv/conf"
coprocess "github.com/TykTechnologies/mserv/coprocess/bindings/go"
_ "github.com/TykTechnologies/mserv/doc"
"github.com/TykTechnologies/mserv/health"
"github.com/TykTechnologies/mserv/http_funcs"
"github.com/TykTechnologies/mserv/storage"
"github.com/TykTechnologies/mserv/util/logger"
utilStore "github.com/TykTechnologies/mserv/util/storage"
)
var (
moduleName = "mserv.main"
log = logger.GetLogger(moduleName)
grpcServer *grpc.Server
)
func main() {
conf := config.GetConf()
overseer.Run(overseer.Config{
Program: prog,
Address: conf.Mserv.HTTPAddr,
Debug: true,
})
}
func prog(state overseer.State) {
conf := config.GetConf()
storage.GlobalRtStore = storage.NewRuntimeStore()
// start the http API
iStore, err := utilStore.GetSpecificStoreType(conf.Mserv.StoreType, conf.Mserv.StorageTag)
if err != nil {
log.Fatal(err)
}
store, ok := iStore.(storage.MservStore)
if !ok {
log.Fatal("store does not implement MservStore")
}
err = store.InitMservStore(context.Background(), conf.Mserv.StorageTag)
if err != nil {
log.Fatal("store failed to init: ", err)
}
srv := http_funcs.NewServer(conf.Mserv.HTTPAddr, store)
mux := http_funcs.GetRouter()
// start required endpoints
http_funcs.InitEndpoints(mux, srv)
http_funcs.InitAPI(mux, srv)
// if enabled start the http test server
if conf.Mserv.AllowHttpInvocation {
http_funcs.InitHttpInvocationServer(mux, srv)
}
go func() {
log.WithField("address", conf.Mserv.HTTPAddr).Info("HTTP listening")
if err := srv.Listen(mux, state.Listener); err != nil {
log.Fatal(err)
}
health.HttpStopped()
}()
health.HttpStarted()
if conf.Mserv.GrpcServer.Enabled {
// First run, fetch all plugins so we can init properly
// log.Warning("SKIPPING PLUGIN FETCH AND INIT")
log.Warning("fetching latest plugin list")
alPLs, err := store.GetAllActive(context.Background())
if err != nil {
log.Fatal(err)
}
// Can be used on any MW list, here we fetch everything active
log.Warning("fetching plugin files")
err = fetchAndProcessPlugins(alPLs)
if err != nil {
log.Fatal(err)
}
// start polling
go pollForActiveMWs(store)
grpcAddr := ":9898"
if conf.Mserv.GrpcServer.Address != "" {
grpcAddr = conf.Mserv.GrpcServer.Address
}
lis, _ := net.Listen("tcp", grpcAddr)
go startGRPCServer(lis, grpcAddr)
health.GrpcStarted()
log.WithField("address", conf.Mserv.GrpcServer.Address).Info("GRPC listening")
}
// Wait to quit
waitForCtrlC()
fmt.Println()
}
func pollForActiveMWs(store storage.MservStore) {
interval := time.Second * 5
log.WithField("interval", interval).Info("polling for changes in active middleware")
for {
time.Sleep(interval)
alPLs, err := store.GetAllActive(context.Background())
if err != nil {
log.Error(err)
}
// Can be used on any MW list, here we fetch everything active
log.Debug("fetching plugin files")
pls, err := getOnlyNew(alPLs)
if err != nil {
log.Error(err)
}
if len(pls.Added) > 0 || len(pls.Removed) > 0 {
// only fetch new when there's a change
if health.Report.GRPCStarted {
grpcServer.GracefulStop()
}
log.Info("active middleware change(s) detected; calling overseer for restart")
overseer.Restart()
} else {
log.Debug("no changes in active middleware")
}
}
}
func startGRPCServer(lis net.Listener, listenAddress string) {
log.Info("starting grpc server on ", listenAddress)
grpcServer = grpc.NewServer()
coprocess.RegisterDispatcherServer(grpcServer, &api.Dispatcher{})
err := grpcServer.Serve(lis)
if err != nil {
log.Fatal(err)
}
health.GrpcStopped()
}
func getOnlyNew(alPLs []*storage.MW) (*storage.DiffReport, error) {
pls, err := storage.GlobalRtStore.FilterNewMW(alPLs)
if err != nil {
return nil, err
}
return pls, nil
}
func processPlugins(pls []*storage.MW) error {
// Download plugin files from new or updated plugins
for _, p := range pls {
for _, pf := range p.Plugins {
iLog := log.WithFields(logrus.Fields{
"name": pf.Name,
"file": pf.FileName,
"owner": p.OrgID,
"api": p.APIID,
})
location, err := api.GetFileStore()
if err != nil {
return err
}
defer location.Close()
iLog.Info("fetching plugin")
tmpDir := path.Join(config.GetConf().Mserv.PluginDir, uuid.NewString())
err = os.MkdirAll(tmpDir, os.ModePerm)
if err != nil {
return err
}
fURL, err := url.Parse(pf.FileRef)
if err != nil {
return fmt.Errorf("could not parse '%s': %w", pf.FileRef, err)
}
item, err := location.ItemByURL(fURL)
if err != nil {
return err
}
fullPath := filepath.Join(tmpDir, pf.FileName)
f, err := os.Create(fullPath)
if err != nil {
return err
}
rc, err := item.Open()
if err != nil {
return fmt.Errorf("could not open item '%s': %w", item.URL(), err)
}
_, err = io.Copy(f, rc)
if err != nil {
return err
}
rc.Close()
log.Info("loading plugin: ", pf.Name)
// Load the plugin function into memory so we can call it
hFunc, err := api.LoadPlugin(pf.Name, tmpDir, pf.FileName)
if err != nil {
iLog.Fatal("failed to load plugin file: ", pf.FileName, " err: ", err)
}
if hFunc == nil {
continue
}
// Store a reference
hookKey := storage.GenerateStoreKey(p.OrgID, p.APIID, pf.Type.String(), pf.Name)
updated, err := storage.GlobalRtStore.UpdateOrStoreHook(hookKey, hFunc)
if err != nil {
iLog.Fatal(err)
}
msg := "added"
if updated {
msg = "updated"
}
iLog.Infof("status: %s plugin %s", msg, hookKey)
}
// Ensure we have processed the MW
log.Info("storing reference for bundle ID: ", p.UID)
storage.GlobalRtStore.AddMW(p.UID, p)
}
return nil
}
func fetchAndProcessPlugins(alPLs []*storage.MW) error {
// We only want to process ones we haven;t seen, or have been updated
pls, err := getOnlyNew(alPLs)
if err != nil {
return err
}
return processPlugins(pls.Added)
}
func waitForCtrlC() {
var endWaiter sync.WaitGroup
endWaiter.Add(1)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)
go func() {
<-signalChannel
endWaiter.Done()
}()
log.Info("press Ctrl+C to end")
endWaiter.Wait()
}