|
| 1 | +// Copyright Quesma, licensed under the Elastic License 2.0. |
| 2 | +// SPDX-License-Identifier: Elastic-2.0 |
| 3 | +package quesma_api |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | +) |
| 9 | + |
| 10 | +type Quesma struct { |
| 11 | + pipelines []PipelineBuilder |
| 12 | +} |
| 13 | + |
| 14 | +func NewQuesma() *Quesma { |
| 15 | + return &Quesma{ |
| 16 | + pipelines: make([]PipelineBuilder, 0), |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func (quesma *Quesma) AddPipeline(pipeline PipelineBuilder) { |
| 21 | + quesma.pipelines = append(quesma.pipelines, pipeline) |
| 22 | +} |
| 23 | + |
| 24 | +func (quesma *Quesma) GetPipelines() []PipelineBuilder { |
| 25 | + return quesma.pipelines |
| 26 | +} |
| 27 | + |
| 28 | +func (quesma *Quesma) Start() { |
| 29 | + for _, pipeline := range quesma.pipelines { |
| 30 | + pipeline.Start() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (quesma *Quesma) Stop(ctx context.Context) { |
| 35 | + for _, pipeline := range quesma.pipelines { |
| 36 | + for _, conn := range pipeline.GetFrontendConnectors() { |
| 37 | + conn.Stop(ctx) |
| 38 | + } |
| 39 | + } |
| 40 | + for _, pipeline := range quesma.pipelines { |
| 41 | + for _, conn := range pipeline.GetBackendConnectors() { |
| 42 | + conn.Close() |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (quesma *Quesma) Build() (QuesmaBuilder, error) { |
| 48 | + endpoints := make(map[string]struct{}) |
| 49 | + handlers := make(map[string]HandlersPipe) |
| 50 | + |
| 51 | + for _, pipeline := range quesma.pipelines { |
| 52 | + for _, conn := range pipeline.GetFrontendConnectors() { |
| 53 | + if httpConn, ok := conn.(HTTPFrontendConnector); ok { |
| 54 | + endpoints[conn.GetEndpoint()] = struct{}{} |
| 55 | + router := httpConn.GetRouter() |
| 56 | + for path, handlerWrapper := range router.GetHandlers() { |
| 57 | + handlerWrapper.Processors = append(handlerWrapper.Processors, pipeline.GetProcessors()...) |
| 58 | + handlers[path] = handlerWrapper |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + if len(endpoints) == 1 { |
| 64 | + for _, pipeline := range quesma.pipelines { |
| 65 | + for _, conn := range pipeline.GetFrontendConnectors() { |
| 66 | + if httpConn, ok := conn.(HTTPFrontendConnector); ok { |
| 67 | + router := httpConn.GetRouter().Clone().(Router) |
| 68 | + if len(endpoints) == 1 { |
| 69 | + router.SetHandlers(handlers) |
| 70 | + } |
| 71 | + httpConn.AddRouter(router) |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for _, pipeline := range quesma.pipelines { |
| 79 | + backendConnectorTypesPerPipeline := make(map[BackendConnectorType]struct{}) |
| 80 | + for _, conn := range pipeline.GetFrontendConnectors() { |
| 81 | + if tcpConn, ok := conn.(TCPFrontendConnector); ok { |
| 82 | + if len(pipeline.GetProcessors()) > 0 { |
| 83 | + tcpConn.GetConnectionHandler().SetHandlers(pipeline.GetProcessors()) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + backendConnectors := pipeline.GetBackendConnectors() |
| 88 | + for _, backendConnector := range backendConnectors { |
| 89 | + backendConnectorTypesPerPipeline[backendConnector.GetId()] = struct{}{} |
| 90 | + } |
| 91 | + for _, proc := range pipeline.GetProcessors() { |
| 92 | + supportedBackendConnectorsByProc := proc.GetSupportedBackendConnectors() |
| 93 | + for _, backendConnectorType := range supportedBackendConnectorsByProc { |
| 94 | + if _, ok := backendConnectorTypesPerPipeline[backendConnectorType]; !ok { |
| 95 | + return nil, fmt.Errorf("processor %v requires backend connector %v which is not provided", proc.GetId(), GetBackendConnectorNameFromType(backendConnectorType)) |
| 96 | + } |
| 97 | + } |
| 98 | + proc.SetBackendConnectors(backendConnectors) |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + return quesma, nil |
| 104 | +} |
0 commit comments