/
OcelotExtensions.cs
198 lines (167 loc) · 8.93 KB
/
OcelotExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Ocelot.Configuration;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;
using Ocelot.Configuration.Setter;
using Ocelot.DependencyInjection;
using Ocelot.DownstreamRouteFinder.Finder;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Middleware.Pipeline;
using Ocelot.Responses;
namespace TestGateway
{
public static class OcelotExtensions
{
public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app,
Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction)
=> UseOcelotWhenRouteMatch(app, builderAction, new OcelotPipelineConfiguration());
public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app,
Action<OcelotPipelineConfiguration> pipelineConfigurationAction,
Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction)
{
var pipelineConfiguration = new OcelotPipelineConfiguration();
pipelineConfigurationAction?.Invoke(pipelineConfiguration);
return UseOcelotWhenRouteMatch(app, builderAction, pipelineConfiguration);
}
public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app, Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction, OcelotPipelineConfiguration configuration)
{
app.MapWhen(context =>
{
var internalConfigurationResponse =
context.RequestServices.GetRequiredService<IInternalConfigurationRepository>().Get();
if (internalConfigurationResponse.IsError || internalConfigurationResponse.Data.ReRoutes.Count == 0)
{
return false;
}
var internalConfiguration = internalConfigurationResponse.Data;
var downstreamRouteFinder = context.RequestServices
.GetRequiredService<IDownstreamRouteProviderFactory>()
.Get(internalConfiguration);
var response = downstreamRouteFinder.Get(context.Request.Path, context.Request.QueryString.ToString(),
context.Request.Method, internalConfiguration, context.Request.Host.ToString());
return !response.IsError
&& !string.IsNullOrEmpty(response.Data?.ReRoute?.DownstreamReRoute?.FirstOrDefault()
?.DownstreamScheme);
}, appBuilder => appBuilder.UseOcelot(builderAction, configuration).Wait());
return app;
}
public static Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder app,
Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction)
=> UseOcelot(app, builderAction, new OcelotPipelineConfiguration());
public static Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder app,
Action<OcelotPipelineConfiguration> pipelineConfigurationAction,
Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction)
{
var pipelineConfiguration = new OcelotPipelineConfiguration();
pipelineConfigurationAction?.Invoke(pipelineConfiguration);
return UseOcelot(app, builderAction, pipelineConfiguration);
}
public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder app, Action<IOcelotPipelineBuilder, OcelotPipelineConfiguration> builderAction, OcelotPipelineConfiguration configuration)
{
var internalConfiguration = await CreateConfiguration(app); // initConfiguration
ConfigureDiagnosticListener(app);
var ocelotPipelineBuilder = new OcelotPipelineBuilder(app.ApplicationServices);
builderAction?.Invoke(ocelotPipelineBuilder, configuration ?? new OcelotPipelineConfiguration());
var ocelotDelegate = ocelotPipelineBuilder.Build();
app.Properties["analysis.NextMiddlewareName"] = "TransitionToOcelotMiddleware";
app.Use(async (context, task) =>
{
var downstreamContext = new DownstreamContext(context);
await ocelotDelegate.Invoke(downstreamContext);
});
return app;
}
private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder)
{
var fileConfigurationRepository = builder.ApplicationServices.GetService<IFileConfigurationRepository>();
var fileConfiguration = await fileConfigurationRepository.Get();
if (fileConfiguration.IsError)
{
ThrowToStopOcelotStarting(fileConfiguration);
}
// now create the config
var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
var internalConfig = await internalConfigCreator.Create(fileConfiguration.Data);
// Configuration error, throw error message
if (internalConfig.IsError)
{
ThrowToStopOcelotStarting(internalConfig);
}
// now save it in memory
var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
internalConfigRepo.AddOrReplace(internalConfig.Data);
// if configuration from file system
if (typeof(DiskFileConfigurationRepository) == fileConfigurationRepository.GetType())
{
// earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this
var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>();
fileConfig.OnChange(async (config) =>
{
var newInternalConfig = await internalConfigCreator.Create(config);
internalConfigRepo.AddOrReplace(newInternalConfig.Data);
});
}
var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>();
var configurations = builder.ApplicationServices.GetServices<OcelotMiddlewareConfigurationDelegate>();
// Todo - this has just been added for consul so far...will there be an ordering problem in the future? Should refactor all config into this pattern?
foreach (var configuration in configurations)
{
await configuration(builder);
}
if (AdministrationApiInUse(adminPath))
{
//We have to make sure the file config is set for the ocelot.env.json and ocelot.json so that if we pull it from the
//admin api it works...boy this is getting a spit spags boll.
var fileConfigSetter = builder.ApplicationServices.GetService<IFileConfigurationSetter>();
await SetFileConfig(fileConfigSetter, fileConfiguration.Data);
}
return GetOcelotConfigAndReturn(internalConfigRepo);
}
private static bool AdministrationApiInUse(IAdministrationPath adminPath)
{
return adminPath != null;
}
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, FileConfiguration fileConfig)
{
var response = await fileConfigSetter.Set(fileConfig);
if (IsError(response))
{
ThrowToStopOcelotStarting(response);
}
}
private static bool IsError(Response response)
{
return response == null || response.IsError;
}
private static IInternalConfiguration GetOcelotConfigAndReturn(IInternalConfigurationRepository provider)
{
var ocelotConfiguration = provider.Get();
if (ocelotConfiguration?.Data == null || ocelotConfiguration.IsError)
{
ThrowToStopOcelotStarting(ocelotConfiguration);
return null;
}
return ocelotConfiguration.Data;
}
private static void ThrowToStopOcelotStarting(Response config)
{
throw new Exception($"Unable to start Ocelot, errors are: {string.Join(",", config.Errors.Select(x => x.ToString()))}");
}
private static void ConfigureDiagnosticListener(IApplicationBuilder builder)
{
var env = builder.ApplicationServices.GetService<IHostingEnvironment>();
var listener = builder.ApplicationServices.GetService<OcelotDiagnosticListener>();
var diagnosticListener = builder.ApplicationServices.GetService<DiagnosticListener>();
diagnosticListener.SubscribeWithAdapter(listener);
}
}
}