示例#1
0
        private RouterEntry ToRouterEntry(PluginMetadata plugin, RouteMetadata metadata)
        {
            RouteTargetType targetType;
            string          target;

            if (RouteHandlerFactory.IsRpc(metadata))
            {
                targetType = RouteTargetType.Lambda;
                target     = _namingConventions.GetLambdaName(plugin);
            }
            else
            {
                targetType = RouteTargetType.Sqs;
                target     = _namingConventions.GetSqsName(plugin);
            }

            return(new RouterEntry(plugin.ServiceKey, metadata.Route, targetType, target));
        }
        private void AddService(YamlMappingNode functions, YamlMappingNode resources, ServiceProject service, PluginMetadata plugin)
        {
            var functionName = _namingConventions.GetLambdaName(plugin);

            var env = new YamlMappingNode
            {
                { "ROUTER_QUEUE_NAME", new YamlMappingNode {
                      { "Fn::GetAtt", new YamlSequenceNode("RouterQueue", "QueueName") }
                  } },
                { "ROUTER_FUNCTION_NAME", new YamlMappingNode {
                      { "Ref", "RouterLambdaFunction" }
                  } },
            };

            switch (plugin.ServiceType)
            {
            case ServiceType.MessageHandler:
                var routes = RouteDiscoverer.Discover(plugin);
                if (routes.Any(x => !RouteHandlerFactory.IsRpc(x)))
                {
                    var queueName    = _namingConventions.GetSqsName(plugin);
                    var queueRefName = ToTitleCase(plugin.ServiceKey.Replace('.', '-'));

                    functions.Add(functionName + "-queue", new YamlMappingNode
                    {
                        { "name", functionName + "-queue" },
                        { "handler", "Toxon.Micro.RabbitBlog.Serverless.ServiceEntry::Toxon.Micro.RabbitBlog.Serverless.ServiceEntry.FunctionImpl::HandleQueueAsync" },
                        { "environment", env },
                        { "memorySize", "512" },
                        { "timeout", "60" },
                        { "events", new YamlSequenceNode(BuildSqsEvent(queueRefName)) },
                        {
                            "package", new YamlMappingNode
                            {
                                { "artifact", $"artifacts/{service.Name}-service.zip" },
                            }
                        },
                    });
                    resources.Add(queueRefName, BuildQueue(queueName));
                }

                if (routes.Any(RouteHandlerFactory.IsRpc))
                {
                    functions.Add(functionName, new YamlMappingNode
                    {
                        { "name", functionName },
                        { "handler", "Toxon.Micro.RabbitBlog.Serverless.ServiceEntry::Toxon.Micro.RabbitBlog.Serverless.ServiceEntry.FunctionImpl::HandleDirectAsync" },
                        { "environment", env },
                        { "memorySize", "512" },
                        { "timeout", "60" },
                        {
                            "package", new YamlMappingNode
                            {
                                { "artifact", $"artifacts/{service.Name}-service.zip" },
                            }
                        },
                    });
                }
                break;

            case ServiceType.Http:
                functions.Add(functionName, new YamlMappingNode
                {
                    { "name", functionName },
                    { "handler", "Toxon.Micro.RabbitBlog.Serverless.HttpEntry::Toxon.Micro.RabbitBlog.Serverless.HttpEntry.FunctionImpl::FunctionHandlerAsync" },
                    { "environment", env },
                    { "memorySize", "512" },
                    { "timeout", "60" },
                    { "events", new YamlSequenceNode(BuildHttpEvent()) },
                    {
                        "package", new YamlMappingNode
                        {
                            { "artifact", $"artifacts/{service.Name}-http.zip" },
                        }
                    },
                });
                break;

            default: throw new ArgumentOutOfRangeException();
            }
        }