1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
- using System . Collections . Generic ;
5
- using System . Threading ;
6
- using System . Threading . Tasks ;
4
+ using System ;
5
+ using System . Text ;
7
6
using Microsoft . AspNetCore . Routing ;
8
7
using Microsoft . AspNetCore . Routing . Constraints ;
9
8
using Microsoft . Azure . WebJobs . Extensions . Http ;
10
- using Microsoft . Azure . WebJobs . Script . Description ;
11
- using Microsoft . Extensions . Hosting ;
12
9
using Microsoft . Extensions . Logging ;
13
10
using Microsoft . Extensions . Options ;
14
11
15
12
namespace Microsoft . Azure . WebJobs . Script . WebHost
16
13
{
17
- /// <summary>
18
- /// An <see cref="IHostedService"/> responsible for HTTP function initialization.
19
- /// </summary>
20
- public class HttpInitializationService : IHostedService
14
+ internal sealed partial class WebScriptHostHttpRoutesManager : IHttpRoutesManager
21
15
{
16
+ private static readonly string [ ] _allMethods = new string [ ] { "all" } ;
22
17
private readonly IOptions < HttpOptions > _httpOptions ;
23
18
private readonly IWebJobsRouter _router ;
24
19
private readonly ILoggerFactory _loggerFactory ;
25
- private readonly IScriptJobHost _host ;
26
20
private readonly IEnvironment _environment ;
27
21
28
- public HttpInitializationService ( IOptions < HttpOptions > httpOptions , IWebJobsRouter router , ILoggerFactory loggerFactory , IScriptJobHost host , IEnvironment environment )
22
+ public WebScriptHostHttpRoutesManager ( IOptions < HttpOptions > httpOptions , IWebJobsRouter router , ILoggerFactory loggerFactory , IEnvironment environment )
29
23
{
30
24
_httpOptions = httpOptions ;
31
25
_router = router ;
32
26
_loggerFactory = loggerFactory ;
33
- _host = host ;
34
27
_environment = environment ;
35
28
}
36
29
37
- public Task StartAsync ( CancellationToken cancellationToken )
30
+ public void InitializeHttpFunctionRoutes ( IScriptJobHost host )
38
31
{
39
- InitializeHttpFunctions ( _host . Functions , _httpOptions . Value ) ;
32
+ var routesLogBuilder = new StringBuilder ( ) ;
33
+ routesLogBuilder . AppendLine ( "Initializing function HTTP routes" ) ;
40
34
41
- return Task . CompletedTask ;
42
- }
43
-
44
- public Task StopAsync ( CancellationToken cancellationToken )
45
- {
46
- return Task . CompletedTask ;
47
- }
48
-
49
- private void InitializeHttpFunctions ( IEnumerable < FunctionDescriptor > functions , HttpOptions httpOptions )
50
- {
51
35
_router . ClearRoutes ( ) ;
52
36
53
37
// TODO: FACAVAL Instantiation of the ScriptRouteHandler should be cleaned up
54
- WebJobsRouteBuilder routesBuilder = _router . CreateBuilder ( new ScriptRouteHandler ( _loggerFactory , _host , _environment , false ) , httpOptions . RoutePrefix ) ;
38
+ WebJobsRouteBuilder routesBuilder = _router . CreateBuilder ( new ScriptRouteHandler ( _loggerFactory , host , _environment , false ) , _httpOptions . Value . RoutePrefix ) ;
55
39
56
40
// Proxies do not honor the route prefix defined in host.json
57
- WebJobsRouteBuilder proxiesRoutesBuilder = _router . CreateBuilder ( new ScriptRouteHandler ( _loggerFactory , _host , _environment , true ) , routePrefix : null ) ;
41
+ WebJobsRouteBuilder proxiesRoutesBuilder = _router . CreateBuilder ( new ScriptRouteHandler ( _loggerFactory , host , _environment , true ) , routePrefix : null ) ;
58
42
59
- foreach ( var function in functions )
43
+ foreach ( var function in host . Functions )
60
44
{
61
45
var httpTrigger = function . GetTriggerAttributeOrNull < HttpTriggerAttribute > ( ) ;
62
46
if ( httpTrigger != null )
@@ -76,23 +60,50 @@ private void InitializeHttpFunctions(IEnumerable<FunctionDescriptor> functions,
76
60
77
61
WebJobsRouteBuilder builder = function . Metadata . IsProxy ? proxiesRoutesBuilder : routesBuilder ;
78
62
builder . MapFunctionRoute ( function . Metadata . Name , route , constraints , function . Metadata . Name ) ;
63
+
64
+ LogRouteMap ( routesLogBuilder , function . Metadata . Name , route , httpTrigger . Methods , function . Metadata . IsProxy , _httpOptions . Value . RoutePrefix ) ;
79
65
}
80
66
}
81
67
82
68
IRouter proxyRouter = null ;
83
69
IRouter functionRouter = null ;
84
70
85
- if ( proxiesRoutesBuilder . Count > 0 )
71
+ if ( routesBuilder . Count == 0 && proxiesRoutesBuilder . Count == 0 )
86
72
{
87
- proxyRouter = proxiesRoutesBuilder . Build ( ) ;
73
+ routesLogBuilder . AppendLine ( "No HTTP routes mapped" ) ;
88
74
}
89
-
90
- if ( routesBuilder . Count > 0 )
75
+ else
91
76
{
92
- functionRouter = routesBuilder . Build ( ) ;
77
+ if ( proxiesRoutesBuilder . Count > 0 )
78
+ {
79
+ proxyRouter = proxiesRoutesBuilder . Build ( ) ;
80
+ }
81
+
82
+ if ( routesBuilder . Count > 0 )
83
+ {
84
+ functionRouter = routesBuilder . Build ( ) ;
85
+ }
93
86
}
94
87
95
88
_router . AddFunctionRoutes ( functionRouter , proxyRouter ) ;
89
+
90
+ ILogger logger = _loggerFactory . CreateLogger ( "Host.HttpRoutes" ) ;
91
+ logger . LogInformation ( routesLogBuilder . ToString ( ) ) ;
92
+ }
93
+
94
+ private void LogRouteMap ( StringBuilder builder , string functionName , string route , string [ ] methods , bool isProxy , string prefix )
95
+ {
96
+ methods = methods ?? _allMethods ;
97
+ string methodList = string . Join ( ',' , methods ) ;
98
+
99
+ if ( isProxy )
100
+ {
101
+ builder . AppendLine ( $ "Mapped proxy route '{ route } ' [{ methodList } ] to '{ functionName } '") ;
102
+ }
103
+ else
104
+ {
105
+ builder . AppendLine ( $ "Mapped function route '{ prefix } /{ route } ' [{ methodList } ] to '{ functionName } '") ;
106
+ }
96
107
}
97
108
}
98
109
}
0 commit comments