|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/soluto/dqd/handlers" |
| 7 | + "github.com/soluto/dqd/listeners" |
| 8 | + "github.com/soluto/dqd/pipe" |
| 9 | + "github.com/soluto/dqd/providers/azure" |
| 10 | + "github.com/soluto/dqd/providers/servicebus" |
| 11 | + "github.com/soluto/dqd/providers/sqs" |
| 12 | + "github.com/soluto/dqd/utils" |
| 13 | + v1 "github.com/soluto/dqd/v1" |
| 14 | + "github.com/spf13/viper" |
| 15 | +) |
| 16 | + |
| 17 | +type App struct { |
| 18 | + Sources map[string]*v1.Source |
| 19 | + Listeners []listeners.Listener |
| 20 | + Workers []*pipe.Worker |
| 21 | +} |
| 22 | + |
| 23 | +var sourceProviders = map[string]struct { |
| 24 | + v1.ConsumerFactory |
| 25 | + v1.ProducerFactory |
| 26 | +}{ |
| 27 | + "azure-queue": { |
| 28 | + &azure.AzureQueueClientFactory{}, |
| 29 | + &azure.AzureQueueClientFactory{}, |
| 30 | + }, |
| 31 | + "sqs": { |
| 32 | + &sqs.SQSClientFactory{}, |
| 33 | + &sqs.SQSClientFactory{}, |
| 34 | + }, |
| 35 | + "service-bus": { |
| 36 | + &servicebus.ServiceBusClientFactory{}, |
| 37 | + &servicebus.ServiceBusClientFactory{}, |
| 38 | + }, |
| 39 | + "io": { |
| 40 | + &utils.IoSourceFactory{}, |
| 41 | + &utils.IoSourceFactory{}, |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +func createSources(v *viper.Viper) map[string]*v1.Source { |
| 46 | + sources := map[string]*v1.Source{} |
| 47 | + for sourceName, subSource := range utils.ViperSubMap(v, "sources") { |
| 48 | + sourceType := subSource.GetString("type") |
| 49 | + factory, exist := sourceProviders[sourceType] |
| 50 | + if !exist { |
| 51 | + panic(fmt.Errorf("FATAL - Unkown source provider:%v", sourceType)) |
| 52 | + } |
| 53 | + sources[sourceName] = v1.NewSource(factory, factory, subSource, sourceName) |
| 54 | + } |
| 55 | + return sources |
| 56 | +} |
| 57 | + |
| 58 | +func getSource(sources map[string]*v1.Source, sourceName string) *v1.Source { |
| 59 | + source, exists := sources[sourceName] |
| 60 | + if !exists { |
| 61 | + panic(fmt.Sprintf("Missing source definition: %v", sourceName)) |
| 62 | + } |
| 63 | + return source |
| 64 | +} |
| 65 | + |
| 66 | +func getPipeSources(sources map[string]*v1.Source, v *viper.Viper) (pipeSources []*v1.Source) { |
| 67 | + sourcesConfig := v.GetStringSlice("sources") |
| 68 | + for _, s := range sourcesConfig { |
| 69 | + pipeSources = append(pipeSources, getSource(sources, s)) |
| 70 | + } |
| 71 | + if len(pipeSources) == 0 { |
| 72 | + pipeSources = []*v1.Source{getSource(sources, v.GetString("source"))} |
| 73 | + } |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +func createHandler(v *viper.Viper) handlers.Handler { |
| 78 | + if v == nil { |
| 79 | + panic("no handler define for pipe, use 'none' handler if it's the desired behavior") |
| 80 | + } |
| 81 | + if v.Get("none") != nil { |
| 82 | + return handlers.None |
| 83 | + } |
| 84 | + v.SetDefault("http.path", "/") |
| 85 | + v.SetDefault("http.host", "localhost") |
| 86 | + v.SetDefault("http.port", 80) |
| 87 | + |
| 88 | + httpEndpoint := v.GetString("http.endpoint") |
| 89 | + if httpEndpoint == "" { |
| 90 | + httpEndpoint = fmt.Sprintf("http://%v:%v%v", v.GetString("http.host"), v.GetString("http.port"), v.GetString("http.path")) |
| 91 | + } |
| 92 | + return handlers.NewHttpHandler(httpEndpoint) |
| 93 | +} |
| 94 | + |
| 95 | +func createWorkers(v *viper.Viper, sources map[string]*v1.Source) []*pipe.Worker { |
| 96 | + var wList []*pipe.Worker |
| 97 | + pipesConfig := utils.ViperSubMap(v, "pipes") |
| 98 | + for name, pipeConfig := range pipesConfig { |
| 99 | + pipeConfig.SetDefault("rate.init", 10) |
| 100 | + pipeConfig.SetDefault("rate.min", 1) |
| 101 | + pipeConfig.SetDefault("rate.window", "30s") |
| 102 | + pipeConfig.SetDefault("source", "default") |
| 103 | + handler := createHandler(pipeConfig.Sub("handler")) |
| 104 | + |
| 105 | + pipeSources := getPipeSources(sources, pipeConfig) |
| 106 | + |
| 107 | + var opts = []pipe.WorkerOption{} |
| 108 | + writeToSource := pipeConfig.GetString("onError.writeTo.source") |
| 109 | + if writeToSource != "" { |
| 110 | + opts = append(opts, pipe.WithErrorSource(getSource(sources, writeToSource))) |
| 111 | + } |
| 112 | + |
| 113 | + if pipeConfig.IsSet("rate.fixed") { |
| 114 | + opts = append(opts, pipe.WithFixedRate(pipeConfig.GetInt("rate.fixed"))) |
| 115 | + } else { |
| 116 | + opts = append(opts, pipe.WithDynamicRate(pipeConfig.GetInt("rate.init"), |
| 117 | + pipeConfig.GetInt("rate.min"), |
| 118 | + pipeConfig.GetDuration("rate.window"))) |
| 119 | + } |
| 120 | + output := pipeConfig.GetString("output") |
| 121 | + if output != "" { |
| 122 | + opts = append(opts, pipe.WithOutput(getSource(sources, output))) |
| 123 | + } else { |
| 124 | + opts = append(opts, pipe.WithDynamicRate(pipeConfig.GetInt("rate.init"), |
| 125 | + pipeConfig.GetInt("rate.min"), |
| 126 | + pipeConfig.GetDuration("rate.window"))) |
| 127 | + } |
| 128 | + |
| 129 | + wList = append(wList, pipe.NewWorker( |
| 130 | + name, |
| 131 | + pipeSources, |
| 132 | + handler, |
| 133 | + opts..., |
| 134 | + )) |
| 135 | + } |
| 136 | + return wList |
| 137 | +} |
| 138 | + |
| 139 | +func createListeners(v *viper.Viper, sources map[string]*v1.Source) []listeners.Listener { |
| 140 | + v.SetDefault("listeners.http.host", "0.0.0.0:9999") |
| 141 | + host := v.GetString("listeners.http.host") |
| 142 | + listener := listeners.Http(host) |
| 143 | + for _, s := range sources { |
| 144 | + listener.Add(s, viper.New()) |
| 145 | + } |
| 146 | + return []listeners.Listener{listener} |
| 147 | +} |
| 148 | + |
| 149 | +func CreateApp(v *viper.Viper) (_ *App, err error) { |
| 150 | + defer func() { |
| 151 | + if r := recover(); r != nil { |
| 152 | + err = fmt.Errorf("%v", r) |
| 153 | + } |
| 154 | + }() |
| 155 | + |
| 156 | + err = utils.NormalizeEntityConfig(v, "pipe", "pipes") |
| 157 | + if err != nil { |
| 158 | + return |
| 159 | + } |
| 160 | + err = utils.NormalizeEntityConfig(v, "source", "sources") |
| 161 | + if err != nil { |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + sources := createSources(v) |
| 166 | + listeners := createListeners(v, sources) |
| 167 | + workers := createWorkers(v, sources) |
| 168 | + return &App{ |
| 169 | + sources, |
| 170 | + listeners, |
| 171 | + workers, |
| 172 | + }, nil |
| 173 | +} |
0 commit comments