| 239 | } |
| 240 | |
| 241 | func NewServer(opts ...ServerOption) (*Server, error) { |
| 242 | options := &serverOptions{} |
| 243 | options.tubeFactoryBuilders = make(map[string]func(configMap common.ConfigMap) (contube.TubeFactory, error)) |
| 244 | options.tubeConfig = make(map[string]common.ConfigMap) |
| 245 | options.runtimeFactoryBuilders = make(map[string]func(configMap common.ConfigMap) (api.FunctionRuntimeFactory, error)) |
| 246 | options.runtimeConfig = make(map[string]common.ConfigMap) |
| 247 | options.stateStoreLoader = DefaultStateStoreLoader |
| 248 | for _, o := range opts { |
| 249 | if o == nil { |
| 250 | continue |
| 251 | } |
| 252 | _, err := o.apply(options) |
| 253 | if err != nil { |
| 254 | return nil, err |
| 255 | } |
| 256 | } |
| 257 | var log *common.Logger |
| 258 | if options.log == nil { |
| 259 | log = common.NewDefaultLogger() |
| 260 | } else { |
| 261 | log = common.NewLogger(options.log) |
| 262 | } |
| 263 | if options.httpTubeFact == nil { |
| 264 | options.httpTubeFact = contube.NewHttpTubeFactory(context.Background()) |
| 265 | log.Info("Using the default HTTP tube factory") |
| 266 | } |
| 267 | options.managerOpts = []fs.ManagerOption{ |
| 268 | fs.WithTubeFactory("http", options.httpTubeFact), |
| 269 | } |
| 270 | |
| 271 | options.managerOpts = append(options.managerOpts, fs.WithLogger(log.Logger)) |
| 272 | |
| 273 | // Config Tube Factory |
| 274 | if tubeFactories, err := setupFactories(options.tubeFactoryBuilders, options.tubeConfig); err == nil { |
| 275 | for name, f := range tubeFactories { |
| 276 | options.managerOpts = append(options.managerOpts, fs.WithTubeFactory(name, f)) |
| 277 | } |
| 278 | } else { |
| 279 | return nil, err |
| 280 | } |
| 281 | |
| 282 | // Config Runtime Factory |
| 283 | if runtimeFactories, err := setupFactories(options.runtimeFactoryBuilders, options.runtimeConfig); err == nil { |
| 284 | for name, f := range runtimeFactories { |
| 285 | options.managerOpts = append(options.managerOpts, fs.WithRuntimeFactory(name, f)) |
| 286 | } |
| 287 | } else { |
| 288 | return nil, err |
| 289 | } |
| 290 | |
| 291 | // Config Queue Factory |
| 292 | if options.queueConfig.Type != "" { |
| 293 | queueFactoryBuilder, ok := options.tubeFactoryBuilders[options.queueConfig.Type] |
| 294 | if !ok { |
| 295 | return nil, fmt.Errorf("%w, queueType: %s", ErrUnsupportedQueueType, options.queueConfig.Type) |
| 296 | } |
| 297 | queueFactory, err := queueFactoryBuilder(options.queueConfig.Config) |
| 298 | if err != nil { |