Skip to content

Commit c72d2f9

Browse files
committed
Readded Ocelot apigateway without swagger
1 parent ea3a034 commit c72d2f9

10 files changed

+33
-97
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ msbuild.wrn
4040
API/API.xml
4141
logs/
4242

43-
ocelot.json
43+
ocelot.json
44+
[Oo]celot.[Pp]roduction.json
45+
[Oo]celot.[Ss]taging.json
46+
[Oo]celot.[Dd]evelopment.json

ApiGateway/Ocelot/ApiGateway.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
1212
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
13-
<PackageReference Include="MMLib.SwaggerForOcelot" Version="2.5.0" />
1413
<PackageReference Include="Ocelot" Version="16.0.1" />
1514
<PackageReference Include="Ocelot.Administration" Version="16.0.1" />
1615
<PackageReference Include="Ocelot.Provider.Consul" Version="16.0.1" />

ApiGateway/Ocelot/Configuration/ocelot.global.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
"GlobalConfiguration": {
33
"RequestIdKey": "x-correlation-id",
44
"UseServiceDiscovery": true,
5-
"DownstreamScheme": "http",
6-
"ServiceDiscoveryProvider": {
7-
"Type": "Consul",
8-
"Host": "http://consul",
9-
"Port": 8500,
10-
"ConfigurationKey": "ApiGateway_1",
11-
"PollingInterval": 100
12-
}
5+
"DownstreamScheme": "http"
136
},
147

158
"RateLimitOptions": {

ApiGateway/Ocelot/Configuration/ocelot.movies.json

-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"DownstreamScheme": "http",
66
"UpstreamPathTemplate": "/api/movies/{movieId}/reviews",
77
"ServiceName": "ReviewsService",
8-
"SwaggerKey": "ReviewsService",
98
"FileCacheOptions": { "TtlSeconds": 15 },
109
"LoadBalancerOptions": {
1110
"Type": "LeastConnection"
@@ -16,7 +15,6 @@
1615
"DownstreamScheme": "http",
1716
"UpstreamPathTemplate": "/api/movies/{movieId}/reviews/{everything}",
1817
"ServiceName": "ReviewsService",
19-
"SwaggerKey": "ReviewsService",
2018
"FileCacheOptions": { "TtlSeconds": 15 },
2119
"LoadBalancerOptions": {
2220
"Type": "LeastConnection"
@@ -28,7 +26,6 @@
2826
"DownstreamScheme": "http",
2927
"UpstreamPathTemplate": "/api/movies",
3028
"ServiceName": "MoviesService",
31-
"SwaggerKey": "MoviesService",
3229
"FileCacheOptions": { "TtlSeconds": 15 },
3330
"LoadBalancerOptions": {
3431
"Type": "LeastConnection"
@@ -39,7 +36,6 @@
3936
"DownstreamScheme": "http",
4037
"UpstreamPathTemplate": "/api/movies/{everything}",
4138
"ServiceName": "MoviesService",
42-
"SwaggerKey": "MoviesService",
4339
"FileCacheOptions": { "TtlSeconds": 15 },
4440
"LoadBalancerOptions": {
4541
"Type": "LeastConnection"

ApiGateway/Ocelot/Configuration/ocelot.reviews.json

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"DownstreamScheme": "http",
66
"UpstreamPathTemplate": "/api/reviews",
77
"ServiceName": "ReviewsService",
8-
"SwaggerKey": "ReviewsService",
98
"FileCacheOptions": { "TtlSeconds": 15 },
109
"LoadBalancerOptions": {
1110
"Type": "LeastConnection"
@@ -16,7 +15,6 @@
1615
"DownstreamScheme": "http",
1716
"UpstreamPathTemplate": "/api/reviews/{everything}",
1817
"ServiceName": "ReviewsService",
19-
"SwaggerKey": "ReviewsService",
2018
"FileCacheOptions": { "TtlSeconds": 15 },
2119
"LoadBalancerOptions": {
2220
"Type": "LeastConnection"

ApiGateway/Ocelot/Configuration/ocelot.swaggerEndpoints.json

-43
This file was deleted.

ApiGateway/Ocelot/Configuration/ocelot.users.json

-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"DownstreamScheme": "http",
66
"UpstreamPathTemplate": "/api/users/{userId}/movies",
77
"ServiceName": "MoviesService",
8-
"SwaggerKey": "MoviesService",
98
"FileCacheOptions": { "TtlSeconds": 15 },
109
"LoadBalancerOptions": {
1110
"Type": "LeastConnection"
@@ -16,7 +15,6 @@
1615
"DownstreamScheme": "http",
1716
"UpstreamPathTemplate": "/api/users/{userId}/movies/{everything}",
1817
"ServiceName": "MoviesService",
19-
"SwaggerKey": "MoviesService",
2018
"FileCacheOptions": { "TtlSeconds": 15 },
2119
"LoadBalancerOptions": {
2220
"Type": "LeastConnection"
@@ -28,7 +26,6 @@
2826
"DownstreamScheme": "http",
2927
"UpstreamPathTemplate": "/api/users/{userId}/reviews",
3028
"ServiceName": "ReviewsService",
31-
"SwaggerKey": "ReviewsService",
3229
"FileCacheOptions": { "TtlSeconds": 15 },
3330
"LoadBalancerOptions": {
3431
"Type": "LeastConnection"
@@ -39,7 +36,6 @@
3936
"DownstreamScheme": "http",
4037
"UpstreamPathTemplate": "/api/users/{userId}/reviews/{everything}",
4138
"ServiceName": "ReviewsService",
42-
"SwaggerKey": "ReviewsService",
4339
"FileCacheOptions": { "TtlSeconds": 15 },
4440
"LoadBalancerOptions": {
4541
"Type": "LeastConnection"
@@ -51,8 +47,6 @@
5147
"DownstreamScheme": "http",
5248
"UpstreamPathTemplate": "/api/users",
5349
"ServiceName": "UsersService",
54-
"SwaggerKey": "UsersService",
55-
"UpstreamHttpMethod": [ "Get", "Post" ],
5650
"FileCacheOptions": { "TtlSeconds": 15 },
5751
"LoadBalancerOptions": {
5852
"Type": "LeastConnection"
@@ -63,8 +57,6 @@
6357
"DownstreamScheme": "http",
6458
"UpstreamPathTemplate": "/api/users/{everything}",
6559
"ServiceName": "UsersService",
66-
"SwaggerKey": "UsersService",
67-
"UpstreamHttpMethod": [ "Get", "Post" ],
6860
"FileCacheOptions": { "TtlSeconds": 15 },
6961
"LoadBalancerOptions": {
7062
"Type": "LeastConnection"

ApiGateway/Ocelot/Program.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.Hosting;
5-
using MMLib.SwaggerForOcelot.DependencyInjection;
5+
using Newtonsoft.Json.Linq;
66
using System.IO;
77

88
namespace ApiGateway
@@ -18,27 +18,29 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
1818
Host.CreateDefaultBuilder(args)
1919
.ConfigureAppConfiguration((hostingContext, config) =>
2020
{
21-
//var ocelotJson = new JObject();
22-
//foreach (var jsonFilename in Directory.EnumerateFiles("Configuration", "ocelot.*.json", SearchOption.AllDirectories))
23-
//{
24-
// using (StreamReader fi = File.OpenText(jsonFilename))
25-
// {
26-
// var json = JObject.Parse(fi.ReadToEnd());
27-
// ocelotJson.Merge(json, new JsonMergeSettings
28-
// {
29-
// MergeArrayHandling = MergeArrayHandling.Union
30-
// });
31-
// }
32-
//}
21+
var ocelotJson = new JObject();
22+
foreach (var jsonFilename in Directory.EnumerateFiles("Configuration", "ocelot.*.json", SearchOption.AllDirectories))
23+
{
24+
using (StreamReader fi = File.OpenText(jsonFilename))
25+
{
26+
var json = JObject.Parse(fi.ReadToEnd());
27+
ocelotJson.Merge(json, new JsonMergeSettings
28+
{
29+
MergeArrayHandling = MergeArrayHandling.Merge
30+
});
31+
}
32+
}
3333

34-
//File.WriteAllText("ocelot.json", ocelotJson.ToString());
34+
File.WriteAllText("ocelot.json", ocelotJson.ToString());
3535

3636
config
3737
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
38+
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
39+
.AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json",
40+
optional: true, reloadOnChange: true)
3841
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
3942
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json",
4043
optional: true, reloadOnChange: true)
41-
.AddOcelotWithSwaggerSupport(folder: "Configuration")
4244
.AddEnvironmentVariables();
4345
})
4446
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());

ApiGateway/Ocelot/Startup.cs

-14
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public void ConfigureServices(IServiceCollection services)
2929
.AddOcelot(Configuration)
3030
.AddConsul()
3131
.AddConfigStoredInConsul();
32-
33-
services.AddSwaggerForOcelot(Configuration);
3432
}
3533

3634
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -39,18 +37,6 @@ public void Configure(IApplicationBuilder app)
3937
app.UseStaticFiles();
4038

4139
app
42-
.UseSwaggerForOcelotUI(opt =>
43-
{
44-
opt.ReConfigureUpstreamSwaggerJson = (HttpContext context, string swaggerJson) =>
45-
{
46-
var swagger = JObject.Parse(swaggerJson);
47-
return swagger.ToString(Formatting.Indented);
48-
};
49-
opt.DownstreamSwaggerHeaders = new[]
50-
{
51-
new KeyValuePair<string, string>("x-correlation-id", Guid.NewGuid().ToString()),
52-
};
53-
})
5440
.UseOcelot()
5541
.Wait();
5642
}

ApiGateway/Ocelot/appsettings.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
"Ocelot": "Warning"
88
}
99
},
10-
"AllowedHosts": "*"
10+
"AllowedHosts": "*",
11+
12+
"GlobalConfiguration": {
13+
"ServiceDiscoveryProvider": {
14+
"Type": "PollConsul",
15+
"Host": "http://consul",
16+
"Port": 8500,
17+
"ConfigurationKey": "ApiGateway_1",
18+
"PollingInterval": 100
19+
}
20+
}
1121
}

0 commit comments

Comments
 (0)