|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Routing.Template; |
| 11 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 12 | +using Microsoft.Azure.WebJobs.Script.Workers.Rpc; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | +using Microsoft.Extensions.Options; |
| 15 | +using Newtonsoft.Json.Linq; |
| 16 | + |
| 17 | +namespace Microsoft.Azure.WebJobs.Script.Workers.Http |
| 18 | +{ |
| 19 | + internal sealed class HttpWorkerFunctionProvider : IFunctionProvider |
| 20 | + { |
| 21 | + private readonly HttpWorkerOptions _httpWorkerOptions; |
| 22 | + private readonly IHostFunctionMetadataProvider _hostFunctionMetadataProvider; |
| 23 | + private readonly IOptionsMonitor<LanguageWorkerOptions> _languageWorkerOptions; |
| 24 | + private readonly ILogger _logger; |
| 25 | + private ImmutableDictionary<string, ImmutableArray<string>> _errors = ImmutableDictionary<string, ImmutableArray<string>>.Empty; |
| 26 | + private static readonly JArray AllHttpMethods = BuildAllHttpMethods(); |
| 27 | + |
| 28 | + public HttpWorkerFunctionProvider(IOptions<HttpWorkerOptions> httpWorkerOptions, IOptionsMonitor<LanguageWorkerOptions> languageWorkerOptions, IHostFunctionMetadataProvider hostFunctionMetadataProvider, ILogger<HttpWorkerFunctionProvider> logger) |
| 29 | + { |
| 30 | + ArgumentNullException.ThrowIfNull(logger); |
| 31 | + ArgumentNullException.ThrowIfNull(httpWorkerOptions?.Value); |
| 32 | + ArgumentNullException.ThrowIfNull(languageWorkerOptions); |
| 33 | + ArgumentNullException.ThrowIfNull(hostFunctionMetadataProvider); |
| 34 | + _httpWorkerOptions = httpWorkerOptions?.Value; |
| 35 | + _hostFunctionMetadataProvider = hostFunctionMetadataProvider; |
| 36 | + _languageWorkerOptions = languageWorkerOptions; |
| 37 | + _logger = logger; |
| 38 | + } |
| 39 | + |
| 40 | + public ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors => _errors; |
| 41 | + |
| 42 | + private static JArray BuildAllHttpMethods() |
| 43 | + { |
| 44 | + return new JArray( |
| 45 | + HttpMethods.Get, |
| 46 | + HttpMethods.Post, |
| 47 | + HttpMethods.Put, |
| 48 | + HttpMethods.Delete, |
| 49 | + HttpMethods.Patch, |
| 50 | + HttpMethods.Head, |
| 51 | + HttpMethods.Options, |
| 52 | + HttpMethods.Trace, |
| 53 | + HttpMethods.Connect); |
| 54 | + } |
| 55 | + |
| 56 | + public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync() |
| 57 | + { |
| 58 | + var routes = _httpWorkerOptions.Http?.Routes; |
| 59 | + |
| 60 | + if (routes is null || !routes.Any()) |
| 61 | + { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + if (!_httpWorkerOptions.CustomRoutesEnabled) |
| 66 | + { |
| 67 | + throw new InvalidOperationException($"Routes configuration is only allowed for worker runtime: custom"); |
| 68 | + } |
| 69 | + |
| 70 | + var hostFunctionMetadata = await _hostFunctionMetadataProvider.GetFunctionMetadataAsync(_languageWorkerOptions.CurrentValue.WorkerConfigs, forceRefresh: false); |
| 71 | + |
| 72 | + // We already know custom handler http routes are configured, if function.json files are also present we cannot proceed. |
| 73 | + if (hostFunctionMetadata.Any()) |
| 74 | + { |
| 75 | + throw new InvalidOperationException( |
| 76 | + "Detected both function.json files and custom handler HTTP route configuration definition in host.json" + |
| 77 | + "Only one configuration source is supported. Remove either the function.json files or the HTTP routes entries in host.json."); |
| 78 | + } |
| 79 | + |
| 80 | + return CreateFunctionsFromRoutes(routes); |
| 81 | + } |
| 82 | + |
| 83 | + private ImmutableArray<FunctionMetadata> CreateFunctionsFromRoutes(IEnumerable<HttpWorkerRoute> routes) |
| 84 | + { |
| 85 | + var metadataBuilder = ImmutableArray.CreateBuilder<FunctionMetadata>(); |
| 86 | + var errorsBuilder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<string>>(); |
| 87 | + |
| 88 | + int index = 0; |
| 89 | + |
| 90 | + foreach (var route in routes) |
| 91 | + { |
| 92 | + var functionName = $"http-handler{++index}"; |
| 93 | + var routeTemplate = route.Route; |
| 94 | + |
| 95 | + if (string.IsNullOrWhiteSpace(routeTemplate)) |
| 96 | + { |
| 97 | + AddError("Route cannot be null, empty or whitespace."); |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (!TryParseRoute(routeTemplate, out var parseError)) |
| 102 | + { |
| 103 | + AddError(parseError!); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + metadataBuilder.Add(CreateHttpFunctionMetadata(route, functionName)); |
| 108 | + |
| 109 | + _logger.LogInformation( |
| 110 | + "Created function {FunctionName} for route {RouteTemplate} (authLevel={AuthLevel}).", |
| 111 | + functionName, |
| 112 | + routeTemplate, |
| 113 | + route.AuthorizationLevel); |
| 114 | + |
| 115 | + void AddError(string reason) |
| 116 | + { |
| 117 | + errorsBuilder.Add(functionName, [reason]); |
| 118 | + _logger.LogError( |
| 119 | + "Unable to create function {FunctionName} for route {Route} due to invalid route: {Reason}", |
| 120 | + functionName, |
| 121 | + routeTemplate ?? "<null>", |
| 122 | + reason); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + _errors = errorsBuilder.ToImmutable(); |
| 127 | + |
| 128 | + return metadataBuilder.ToImmutable(); |
| 129 | + |
| 130 | + bool TryParseRoute(string template, out string error) |
| 131 | + { |
| 132 | + try |
| 133 | + { |
| 134 | + _ = TemplateParser.Parse(template); |
| 135 | + error = null; |
| 136 | + return true; |
| 137 | + } |
| 138 | + catch (ArgumentException ex) |
| 139 | + { |
| 140 | + error = ex.Message; |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static FunctionMetadata CreateHttpFunctionMetadata(HttpWorkerRoute route, string functionName) |
| 147 | + { |
| 148 | + var trigger = new JObject |
| 149 | + { |
| 150 | + ["type"] = "httpTrigger", |
| 151 | + ["authLevel"] = route.AuthorizationLevel.ToString(), |
| 152 | + ["direction"] = "in", |
| 153 | + ["name"] = "req", |
| 154 | + ["methods"] = AllHttpMethods, |
| 155 | + ["route"] = route.Route |
| 156 | + }; |
| 157 | + |
| 158 | + var output = new JObject |
| 159 | + { |
| 160 | + ["type"] = "http", |
| 161 | + ["direction"] = "out", |
| 162 | + ["name"] = "res" |
| 163 | + }; |
| 164 | + |
| 165 | + var metadata = new FunctionMetadata |
| 166 | + { |
| 167 | + Name = functionName |
| 168 | + }; |
| 169 | + |
| 170 | + metadata.Bindings.Add(BindingMetadata.Create(trigger)); |
| 171 | + metadata.Bindings.Add(BindingMetadata.Create(output)); |
| 172 | + |
| 173 | + return metadata; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments