-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgateway.go
More file actions
955 lines (771 loc) · 25.1 KB
/
gateway.go
File metadata and controls
955 lines (771 loc) · 25.1 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gateway
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/url"
"sort"
"strings"
"sync"
"time"
"github.com/asaskevich/EventBus"
"github.com/fasthttp/router"
"github.com/fasthttp/websocket"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/valyala/fasthttp"
"google.golang.org/protobuf/types/known/structpb"
"github.com/nitrictech/cli/pkg/cloud/apis"
"github.com/nitrictech/cli/pkg/cloud/batch"
"github.com/nitrictech/cli/pkg/cloud/http"
"github.com/nitrictech/cli/pkg/cloud/schedules"
"github.com/nitrictech/cli/pkg/cloud/topics"
"github.com/nitrictech/cli/pkg/cloud/websockets"
"github.com/nitrictech/cli/pkg/netx"
"github.com/nitrictech/cli/pkg/project/localconfig"
"github.com/nitrictech/cli/pkg/system"
base_http "github.com/nitrictech/nitric/cloud/common/runtime/gateway"
"github.com/nitrictech/nitric/core/pkg/gateway"
apispb "github.com/nitrictech/nitric/core/pkg/proto/apis/v1"
batchpb "github.com/nitrictech/nitric/core/pkg/proto/batch/v1"
schedulespb "github.com/nitrictech/nitric/core/pkg/proto/schedules/v1"
topicspb "github.com/nitrictech/nitric/core/pkg/proto/topics/v1"
websocketspb "github.com/nitrictech/nitric/core/pkg/proto/websockets/v1"
)
type apiServer struct {
lis net.Listener
srv *fasthttp.Server
tlsCredentials *TLSCredentials
name string // name of the API or host
}
type socketServer struct {
lis net.Listener
srv *fasthttp.Server
workerCount int
}
type TLSCredentials struct {
// CertFile - Path to the certificate file
CertFile string
// KeyFile - Path to the private key file
KeyFile string
}
var upgrader = websocket.FastHTTPUpgrader{}
type LocalGatewayService struct {
apiServers []*apiServer
httpServers []*apiServer
apis []string
httpWorkers []string
websocketWorkers []string
socketServer map[string]*socketServer
serviceServer *fasthttp.Server
apisPlugin *apis.LocalApiGatewayService
websocketPlugin *websockets.LocalWebsocketService
topicsPlugin *topics.LocalTopicsAndSubscribersService
schedulesPlugin *schedules.LocalSchedulesService
batchPlugin *batch.LocalBatchService
serviceListener net.Listener
localConfig localconfig.LocalConfiguration
hostname string
logWriter io.Writer
ApiTlsCredentials *TLSCredentials
lock sync.RWMutex
gateway.UnimplementedGatewayPlugin
stop chan bool
options *gateway.GatewayStartOpts
bus EventBus.Bus
}
var _ gateway.GatewayService = &LocalGatewayService{}
// GetTriggerAddress - Returns the base address built-in nitric services, like schedules and topics, will be exposed on.
func (s *LocalGatewayService) GetTriggerAddress() string {
if s.serviceListener != nil {
return strings.Replace(s.serviceListener.Addr().String(), "[::]", s.hostname, 1)
}
return ""
}
// GetApiAddresses - Returns a map of API names to their addresses, including protocol and port
func (s *LocalGatewayService) GetApiAddresses() map[string]string {
s.lock.RLock()
defer s.lock.RUnlock()
addresses := make(map[string]string)
if len(s.apiServers) > 0 && len(s.apis) == len(s.apiServers) {
for _, srv := range s.apiServers {
protocol := "http"
if srv.tlsCredentials != nil {
protocol = "https"
}
address := strings.Replace(srv.lis.Addr().String(), "[::]", s.hostname, 1)
addresses[srv.name] = fmt.Sprintf("%s://%s", protocol, address)
}
}
return addresses
}
func (s *LocalGatewayService) GetApiAddress(apiName string) string {
s.lock.RLock()
defer s.lock.RUnlock()
addresses := s.GetApiAddresses()
if address, ok := addresses[apiName]; ok {
return address
}
return ""
}
func (s *LocalGatewayService) GetWebsocketAddress(socketName string) string {
s.lock.RLock()
defer s.lock.RUnlock()
addresses := s.GetWebsocketAddresses()
if address, ok := addresses[socketName]; ok {
return address
}
return ""
}
func (s *LocalGatewayService) GetHttpWorkerAddresses() map[string]string {
s.lock.RLock()
defer s.lock.RUnlock()
addresses := make(map[string]string)
if len(s.httpServers) > 0 && len(s.httpWorkers) == len(s.httpServers) {
for _, srv := range s.httpServers {
protocol := "http"
if srv.tlsCredentials != nil {
protocol = "https"
}
address := strings.Replace(srv.lis.Addr().String(), "[::]", s.hostname, 1)
addresses[srv.name] = fmt.Sprintf("%s://%s", protocol, address)
}
}
return addresses
}
func (s *LocalGatewayService) GetWebsocketAddresses() map[string]string {
s.lock.RLock()
defer s.lock.RUnlock()
addresses := make(map[string]string)
for socket, srv := range s.socketServer {
if srv.workerCount > 0 {
srvAddress := strings.Replace(srv.lis.Addr().String(), "[::]", s.hostname, 1)
addresses[socket] = srvAddress
}
}
return addresses
}
func (s *LocalGatewayService) handleHttpProxyRequest(idx int) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
port := s.httpWorkers[idx]
// set port so http plugin can find server from state
requestCopy := &fasthttp.Request{}
ctx.Request.CopyTo(requestCopy)
requestCopy.URI().SetHost(port)
// TODO: Need to support multiple HTTP handlers
// so a plugin wrapper will be required for this
resp, err := s.options.HttpPlugin.HandleRequest(requestCopy)
if err != nil {
ctx.Error(fmt.Sprintf("Error handling HTTP Request: %v", err), 500)
return
}
resp.CopyTo(&ctx.Response)
}
}
func (s *LocalGatewayService) handleApiHttpRequest(apiName string) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
if !s.apiServerExists(apiName) {
ctx.Error("Sorry, nitric is listening on this port but is waiting for an API to be available to handle requests, you may have removed an API during development this port will be assigned to an API when one becomes available", 404)
return
}
headerMap := base_http.HttpHeadersToMap(&ctx.Request.Header)
headers := map[string]*apispb.HeaderValue{}
for k, v := range headerMap {
headers[k] = &apispb.HeaderValue{Value: v}
}
query := map[string]*apispb.QueryValue{}
ctx.QueryArgs().VisitAll(func(key []byte, val []byte) {
k := string(key)
if query[k] == nil {
query[k] = &apispb.QueryValue{}
}
query[k].Value = append(query[k].Value, string(val))
})
path := string(ctx.URI().Path())
_, err := url.Parse(path)
if err != nil {
ctx.Error(fmt.Sprintf("Bad Request: %v", err), 400)
return
}
apiEvent := &apispb.ServerMessage{
Content: &apispb.ServerMessage_HttpRequest{
HttpRequest: &apispb.HttpRequest{
Method: string(ctx.Request.Header.Method()),
Path: path,
Headers: headers,
QueryParams: query,
PathParams: map[string]string{},
Body: ctx.Request.Body(),
},
},
}
resp, err := s.options.ApiPlugin.HandleRequest(apiName, apiEvent)
if err != nil {
ctx.Error(fmt.Sprintf("Error handling HTTP Request: %v", err), 500)
return
}
if http := resp.GetHttpResponse(); http != nil {
// Copy headers across
for k, v := range http.Headers {
for _, val := range v.Value {
ctx.Response.Header.Add(k, val)
}
}
// Avoid content length header duplication
ctx.Response.Header.Del("Content-Length")
ctx.Response.SetStatusCode(int(http.Status))
ctx.Response.SetBody(resp.GetHttpResponse().GetBody())
// publish ctx for history
s.apisPlugin.PublishActionState(apis.ApiRequestState{
Api: apiName,
ReqCtx: ctx,
HttpResp: http,
})
return
}
ctx.Error("Response was not a Http response", 500)
}
}
// websocket request handler
// TODO: Add broadcast capability
func (s *LocalGatewayService) handleWebsocketRequest(socketName string) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
upgrader.CheckOrigin = func(ctx *fasthttp.RequestCtx) bool {
return true
}
connectionId := uuid.New().String()
query := map[string]*websocketspb.QueryValue{}
ctx.QueryArgs().VisitAll(func(key []byte, val []byte) {
k := string(key)
if query[k] == nil {
query[k] = &websocketspb.QueryValue{}
}
query[k].Value = append(query[k].Value, string(val))
})
resp, err := s.options.WebsocketListenerPlugin.HandleRequest(&websocketspb.ServerMessage{
Content: &websocketspb.ServerMessage_WebsocketEventRequest{
WebsocketEventRequest: &websocketspb.WebsocketEventRequest{
SocketName: socketName,
WebsocketEvent: &websocketspb.WebsocketEventRequest_Connection{
Connection: &websocketspb.WebsocketConnectionEvent{
QueryParams: query,
},
},
ConnectionId: connectionId,
},
},
})
if err != nil {
return
}
if resp.GetWebsocketEventResponse() == nil || (resp.GetWebsocketEventResponse().GetConnectionResponse() != nil && resp.GetWebsocketEventResponse().GetConnectionResponse().Reject) {
// close the connection
ctx.Error("Connection Refused", 500)
return
}
err = upgrader.Upgrade(ctx, func(ws *websocket.Conn) {
// generate a new connection ID for this client
defer func() {
// close within the websocket plugin will also call ws.Close
_, err = s.websocketPlugin.CloseConnection(ctx, &websocketspb.WebsocketCloseConnectionRequest{
ConnectionId: connectionId,
SocketName: socketName,
})
if err != nil {
system.Logf("Websocket error: %s", err.Error())
return
}
}()
err = s.websocketPlugin.RegisterConnection(socketName, connectionId, ws)
if err != nil {
system.Logf("Websocket error: %s", err.Error())
return
}
// Handshake successful send a registration message with connection ID to the socket worker
for {
// We have successfully connected a new client
// We can read/write messages to/from this client
// Need to create a unique ID for this connection and store in a central location
// This will allow connected clients to message eachother and broadcast to all clients as well
// We'll only read new messages on this connection here, writing will be done by a separate runtime API
// Won't print errors that arise if the socket is closed and are "going away" or "no status" errors
_, message, err := ws.ReadMessage()
if err != nil && websocket.IsCloseError(err, 1001, 1005) {
break
} else if err != nil {
system.Logf("websocket read error: %v", err)
break
}
_, err = s.options.WebsocketListenerPlugin.HandleRequest(&websocketspb.ServerMessage{
Content: &websocketspb.ServerMessage_WebsocketEventRequest{
WebsocketEventRequest: &websocketspb.WebsocketEventRequest{
SocketName: socketName,
ConnectionId: connectionId,
WebsocketEvent: &websocketspb.WebsocketEventRequest_Message{
Message: &websocketspb.WebsocketMessageEvent{
Body: message,
},
},
},
},
})
if err != nil {
system.Logf("Websocket error: %s", err.Error())
return
}
}
_, err = s.options.WebsocketListenerPlugin.HandleRequest(&websocketspb.ServerMessage{
Content: &websocketspb.ServerMessage_WebsocketEventRequest{
WebsocketEventRequest: &websocketspb.WebsocketEventRequest{
SocketName: socketName,
ConnectionId: connectionId,
WebsocketEvent: &websocketspb.WebsocketEventRequest_Disconnection{
Disconnection: &websocketspb.WebsocketDisconnectionEvent{},
},
},
},
})
if err != nil {
system.Logf("Websocket error: %s", err.Error())
return
}
})
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
system.Logf("Websocket error: %s", err.Error())
}
return
}
}
}
func (s *LocalGatewayService) handleTopicRequest(ctx *fasthttp.RequestCtx) {
topicName := ctx.UserValue("name").(string)
// Get the incoming data as JSON
payload := map[string]interface{}{}
err := json.Unmarshal(ctx.Request.Body(), &payload)
if err != nil {
ctx.Error(fmt.Sprintf("Error parsing JSON: %v", err), 400)
return
}
structPayload, err := structpb.NewStruct(payload)
if err != nil {
ctx.Error(fmt.Sprintf("Error serializing topic message from payload: %v", err), 400)
return
}
_, err = s.topicsPlugin.Publish(ctx, &topicspb.TopicPublishRequest{
TopicName: topicName,
Message: &topicspb.TopicMessage{
Content: &topicspb.TopicMessage_StructPayload{
StructPayload: structPayload,
},
},
})
if err != nil {
ctx.Error(fmt.Sprintf("Error handling topic request: %v", err), 500)
return
}
ctx.SuccessString("text/plain", "Successfully delivered message to topic")
}
func (s *LocalGatewayService) handleSchedulesTrigger(ctx *fasthttp.RequestCtx) {
scheduleName := ctx.UserValue("name").(string)
msg := &schedulespb.ServerMessage{
Content: &schedulespb.ServerMessage_IntervalRequest{
IntervalRequest: &schedulespb.IntervalRequest{
ScheduleName: scheduleName,
},
},
}
_, err := s.schedulesPlugin.HandleRequest(msg)
if err != nil {
ctx.Error(fmt.Sprintf("Error handling schedule trigger: %v", err), 500)
return
}
ctx.SuccessString("text/plain", "Successfully triggered schedule")
}
func (s *LocalGatewayService) handleBatchJobTrigger(ctx *fasthttp.RequestCtx) {
jobName := ctx.UserValue("name").(string)
// Get the incoming data as JobData_Struct
payload := map[string]interface{}{}
err := json.Unmarshal(ctx.Request.Body(), &payload)
if err != nil {
ctx.Error(fmt.Sprintf("Error parsing JSON: %v", err), 400)
return
}
st, err := structpb.NewStruct(payload)
if err != nil {
ctx.Error(fmt.Sprintf("Error serializing job message from payload: %v", err), 400)
return
}
jobSubmitRequest := &batchpb.JobSubmitRequest{
JobName: jobName,
Data: &batchpb.JobData{Data: &batchpb.JobData_Struct{Struct: st}},
}
_, err = s.batchPlugin.SubmitJob(context.Background(), jobSubmitRequest)
if err != nil {
ctx.Error(fmt.Sprintf("Error handling batch job trigger: %v", err), 500)
return
}
ctx.SuccessString("text/plain", "Successfully triggered job")
}
func (s *LocalGatewayService) refreshApis(apiState apis.State) {
s.lock.Lock()
defer s.lock.Unlock()
// api has been removed
if len(apiState) < len(s.apiServers) {
// shutdown the apis that have been removed
s.apiServers = lo.Filter(s.apiServers, func(item *apiServer, index int) bool {
_, exists := apiState[item.name]
if !exists {
shutdownServer(item.srv)
}
return exists
})
}
s.apis = make([]string, 0)
uniqApis := lo.Reduce(lo.Keys(apiState), func(agg []string, apiName string, idx int) []string {
if !lo.Contains(agg, apiName) {
agg = append(agg, apiName)
}
return agg
}, []string{})
// sort the APIs by alphabetical order
sort.Strings(uniqApis)
s.apis = append(s.apis, uniqApis...)
err := s.createApiServers()
if err != nil {
system.Log(fmt.Sprintf("error creating api servers: %s", err.Error()))
}
}
func (s *LocalGatewayService) refreshHttpWorkers(state http.State) {
s.lock.Lock()
defer s.lock.Unlock()
s.httpWorkers = make([]string, 0)
// http server has been removed
if len(state) < len(s.httpServers) {
// shutdown the http servers that have been removed
s.httpServers = lo.Filter(s.httpServers, func(item *apiServer, index int) bool {
_, exists := state[item.name]
if !exists {
shutdownServer(item.srv)
}
return exists
})
}
uniqHttpWorkers := lo.Reduce(lo.Keys(state), func(agg []string, host string, idx int) []string {
if !lo.Contains(agg, host) {
agg = append(agg, host)
}
return agg
}, []string{})
// sort the Http Worker Ports lowest to highest
sort.Strings(uniqHttpWorkers)
s.httpWorkers = append(s.httpWorkers, uniqHttpWorkers...)
err := s.createHttpServers()
if err != nil {
system.Log(fmt.Sprintf("error creating http servers: %s", err.Error()))
}
}
func (s *LocalGatewayService) refreshWebsocketWorkers(state websockets.State) {
s.lock.Lock()
s.websocketWorkers = make([]string, 0)
// socket server has been removed
if len(state) < len(s.socketServer) {
// Collect servers to be removed
var toRemove []string
// shutdown the socket servers that have been removed
for socketName, server := range s.socketServer {
_, exists := state[socketName]
if !exists {
shutdownServer(server.srv)
toRemove = append(toRemove, socketName)
}
}
// remove the servers from the collection
for _, socketName := range toRemove {
delete(s.socketServer, socketName)
}
}
websockets := lo.Reduce(lo.Keys(state), func(agg []string, socketName string, idx int) []string {
if !lo.Contains(agg, socketName) {
agg = append(agg, socketName)
}
return agg
}, []string{})
// sort the Http Worker Ports lowest to highest
sort.Strings(websockets)
s.websocketWorkers = append(s.websocketWorkers, websockets...)
// TODO move thread-safe lists/maps to own type so no deadlocks are possible
s.lock.Unlock()
err := s.createWebsocketServers()
if err != nil {
system.Log(fmt.Sprintf("error creating websocket servers: %s", err.Error()))
}
}
func (s *LocalGatewayService) createApiServers() error {
// create an api server for every API worker
for _, apiName := range s.apis {
if s.apiServerExists(apiName) {
continue
}
lis, err := getListener(s.localConfig.Apis, apiName)
if err != nil {
return err
}
fhttp := &fasthttp.Server{
ReadTimeout: time.Second * 1,
IdleTimeout: time.Second * 1,
CloseOnShutdown: true,
ReadBufferSize: 64 * 1024, // Set to 64 KB to handle large headers
Handler: s.handleApiHttpRequest(apiName),
Logger: log.New(s.logWriter, fmt.Sprintf("%s: ", lis.Addr().String()), 0),
}
srv := &apiServer{
lis: lis,
srv: fhttp,
tlsCredentials: s.ApiTlsCredentials,
name: apiName,
}
// get a free port and listen on that for this API
go func(srv *apiServer) {
var err error
if srv.tlsCredentials != nil {
err = srv.srv.ServeTLS(srv.lis, srv.tlsCredentials.CertFile, srv.tlsCredentials.KeyFile)
} else {
err = srv.srv.Serve(srv.lis)
}
if err != nil {
fmt.Println(err)
}
}(srv)
s.apiServers = append(s.apiServers, srv)
}
return nil
}
func (s *LocalGatewayService) apiServerExists(apiName string) bool {
return lo.SomeBy(s.apiServers, func(as *apiServer) bool {
return as.name == apiName
})
}
func getListener(mapping map[string]localconfig.LocalResourceConfiguration, name string) (net.Listener, error) {
if config, exists := mapping[name]; exists {
if config.Port != 0 {
list, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
return nil, fmt.Errorf("error mapping %s to port %d, %s", name, config.Port, err.Error())
}
return list, nil
}
}
return netx.GetNextListener()
}
func (s *LocalGatewayService) createWebsocketServers() error {
if s.socketServer == nil {
s.socketServer = make(map[string]*socketServer)
}
for _, sock := range s.websocketWorkers {
currSocket, ok := s.socketServer[sock]
if !ok {
fhttp := &fasthttp.Server{
ReadTimeout: time.Second * 1,
IdleTimeout: time.Second * 1,
CloseOnShutdown: true,
Handler: s.handleWebsocketRequest(sock),
}
lis, err := getListener(s.localConfig.Websockets, sock)
if err != nil {
return err
}
srv := &socketServer{
lis: lis,
srv: fhttp,
workerCount: 0,
}
go func(srv *socketServer) {
err := srv.srv.Serve(srv.lis)
if err != nil {
fmt.Println(err)
}
}(srv)
currSocket = srv
// append to the server collection
s.socketServer[sock] = currSocket
// this is a brand new server we need to start up
// lets start it and add it to the active list of servers
// we can then filter the servers by their active worker count
currSocket.workerCount = 0
}
currSocket.workerCount = currSocket.workerCount + 1
}
s.websocketPlugin.SetServers(s.GetWebsocketAddresses())
return nil
}
func (s *LocalGatewayService) createHttpServers() error {
// Expand servers to account for apis
lis, err := netx.GetNextListener()
if err != nil {
return err
}
// create an api server for every API worker
for len(s.httpServers) < len(s.httpWorkers) {
fhttp := &fasthttp.Server{
ReadTimeout: time.Second * 1,
IdleTimeout: time.Second * 1,
CloseOnShutdown: true,
ReadBufferSize: 8192,
Handler: s.handleHttpProxyRequest(len(s.httpServers)),
Logger: log.New(s.logWriter, fmt.Sprintf("%s: ", lis.Addr().String()), 0),
}
srv := &apiServer{
lis: lis,
srv: fhttp,
tlsCredentials: s.ApiTlsCredentials,
name: s.httpWorkers[len(s.httpServers)],
}
// get a free port and listen on that for this API
go func(srv *apiServer) {
var err error
if srv.tlsCredentials != nil {
err = srv.srv.ServeTLS(srv.lis, srv.tlsCredentials.CertFile, srv.tlsCredentials.KeyFile)
} else {
err = srv.srv.Serve(srv.lis)
}
if err != nil {
fmt.Println(err)
}
}(srv)
s.httpServers = append(s.httpServers, srv)
}
return nil
}
const nameParam = "{name}"
const (
topicPath = "/topics/" + nameParam
schedulePath = "/schedules/" + nameParam
batchPath = "/jobs/" + nameParam
)
func (s *LocalGatewayService) GetTopicTriggerUrl(topicName string) string {
// TODO: do the path build with the topicPath var
endpoint, _ := url.JoinPath("http://"+s.GetTriggerAddress(), strings.Replace(topicPath, nameParam, topicName, 1))
return endpoint
}
func (s *LocalGatewayService) GetScheduleManualTriggerUrl(scheduleName string) string {
endpoint, _ := url.JoinPath("http://"+s.GetTriggerAddress(), strings.Replace(schedulePath, nameParam, scheduleName, 1))
return endpoint
}
func (s *LocalGatewayService) GetBatchTriggerUrl(jobName string) string {
endpoint, _ := url.JoinPath("http://"+s.GetTriggerAddress(), strings.Replace(batchPath, nameParam, jobName, 1))
return endpoint
}
func (s *LocalGatewayService) Start(opts *gateway.GatewayStartOpts) error {
var err error
// Assign the pool and block
s.options = opts
s.stop = make(chan bool)
// Setup routes
r := router.New()
// Publish to a topic
r.POST(topicPath, s.handleTopicRequest)
r.POST(schedulePath, s.handleSchedulesTrigger)
r.POST(batchPath, s.handleBatchJobTrigger)
s.serviceServer = &fasthttp.Server{
ReadTimeout: time.Second * 1,
IdleTimeout: time.Second * 1,
CloseOnShutdown: true,
ReadBufferSize: 8192,
Handler: r.Handler,
}
s.serviceListener, err = netx.GetNextListener()
if err != nil {
return err
}
if apiPlugin, ok := s.options.ApiPlugin.(*apis.LocalApiGatewayService); ok {
apiPlugin.SubscribeToState(func(state apis.State) {
s.refreshApis(state)
})
s.apisPlugin = apiPlugin
}
if topicsPlugin, ok := s.options.TopicsListenerPlugin.(*topics.LocalTopicsAndSubscribersService); ok {
s.topicsPlugin = topicsPlugin
}
if schedulesPlugin, ok := s.options.SchedulesPlugin.(*schedules.LocalSchedulesService); ok {
s.schedulesPlugin = schedulesPlugin
}
if websocketPlugin, ok := s.options.WebsocketListenerPlugin.(*websockets.LocalWebsocketService); ok {
websocketPlugin.SubscribeToState(func(state map[string]map[string][]websocketspb.WebsocketEventType) {
s.refreshWebsocketWorkers(state)
})
s.websocketPlugin = websocketPlugin
}
if httpProxyPlugin, ok := s.options.HttpPlugin.(*http.LocalHttpProxy); ok {
httpProxyPlugin.SubscribeToState(func(state map[string]*http.HttpProxyService) {
s.refreshHttpWorkers(state)
})
}
return s.serviceServer.Serve(s.serviceListener)
}
func shutdownServer(srv *fasthttp.Server) {
// Shutdown the server
// This will allow Start to exit
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
_ = srv.ShutdownWithContext(ctx)
}
func (s *LocalGatewayService) Stop() error {
// Shutdown all the api servers
for _, as := range s.apiServers {
shutdownServer(as.srv)
}
// Shutdown all the http servers
for _, hs := range s.httpServers {
shutdownServer(hs.srv)
}
// Shutdown all the websocket servers
for _, ss := range s.socketServer {
shutdownServer(ss.srv)
}
if s.serviceServer != nil {
return s.serviceServer.Shutdown()
}
return nil
}
type NewGatewayOpts struct {
TLSCredentials *TLSCredentials
LogWriter io.Writer
LocalConfig localconfig.LocalConfiguration
BatchPlugin *batch.LocalBatchService
Hostname string
}
// Create new HTTP gateway
// XXX: No External Args for function atm (currently the plugin loader does not pass any argument information)
func NewGateway(opts NewGatewayOpts) (*LocalGatewayService, error) {
hostname := opts.Hostname
if hostname == "" {
hostname = "localhost"
}
return &LocalGatewayService{
ApiTlsCredentials: opts.TLSCredentials,
bus: EventBus.New(),
logWriter: opts.LogWriter,
localConfig: opts.LocalConfig,
batchPlugin: opts.BatchPlugin,
hostname: hostname,
}, nil
}