-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathStartup.cs
255 lines (221 loc) · 8.45 KB
/
Startup.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.ReverseProxy.Abstractions;
using Microsoft.ReverseProxy.Service;
using N8T.Infrastructure.Logging;
using N8T.Infrastructure.OTel;
using N8T.Infrastructure.Tye;
namespace WebApiGateway
{
public class Startup
{
public Startup(IConfiguration config)
{
Config = config;
}
private IConfiguration Config { get; }
private bool IsRunOnTye => Config.IsRunOnTye();
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddCors(options =>
{
options.AddPolicy("api", policy =>
{
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
// inventory
var inventoryUrl = IsRunOnTye
? $"{Config.GetServiceUri("inventoryapp")?.AbsoluteUri}"
: Config.GetValue<string>("Services:inventoryapp");
var invRoute = new ProxyRoute
{
RouteId = "inv",
ClusterId = "inv-svc-cluster",
Match =
{
Path = "/inv/{**catch-all}"
},
Transforms = new List<IDictionary<string, string>>()
};
invRoute.AddTransformXForwarded();
invRoute.AddTransformPathRemovePrefix("/inv");
var invCluster = new Cluster
{
Id = "inv-svc-cluster",
Destinations =
{
{
"inv-svc-cluster/destination1", new Destination
{
Address = inventoryUrl
}
}
}
};
// product catalog
var productCatalogUrl = IsRunOnTye
? $"{Config.GetServiceUri("productcatalogapp")?.AbsoluteUri}"
: Config.GetValue<string>("Services:productcatalogapp");
var prodRoute = new ProxyRoute
{
RouteId = "prod",
ClusterId = "prod-svc-cluster",
Match =
{
Path = "/prod/{**catch-all}"
},
Transforms = new List<IDictionary<string, string>>()
};
prodRoute.AddTransformXForwarded();
prodRoute.AddTransformPathRemovePrefix("/prod");
var prodCluster = new Cluster
{
Id = "prod-svc-cluster",
Destinations =
{
{
"prod-svc-cluster/destination1", new Destination
{
Address = productCatalogUrl
}
}
}
};
// shopping cart
var shoppingCartUrl = IsRunOnTye
? $"{Config.GetServiceUri("shoppingcartapp")?.AbsoluteUri}"
: Config.GetValue<string>("Services:shoppingcartapp");
var cartRoute = new ProxyRoute
{
RouteId = "cart",
ClusterId = "cart-svc-cluster",
Match =
{
Path = "/cart/{**catch-all}"
},
Transforms = new List<IDictionary<string, string>>()
};
cartRoute.AddTransformXForwarded();
cartRoute.AddTransformPathRemovePrefix("/cart");
var cartCluster = new Cluster
{
Id = "cart-svc-cluster",
Destinations =
{
{
"cart-svc-cluster/destination1", new Destination
{
Address = shoppingCartUrl
}
}
}
};
// sale
var saleUrl = IsRunOnTye
? $"{Config.GetServiceUri("saleapp")?.AbsoluteUri}"
: Config.GetValue<string>("Services:saleapp");
var saleRoute = new ProxyRoute
{
RouteId = "sale",
ClusterId = "sale-svc-cluster",
Match =
{
Path = "/sale/{**catch-all}"
},
Transforms = new List<IDictionary<string, string>>()
};
saleRoute.AddTransformXForwarded();
saleRoute.AddTransformPathRemovePrefix("/sale");
var saleCluster = new Cluster
{
Id = "sale-svc-cluster",
Destinations =
{
{
"sale-svc-cluster/destination1", new Destination
{
Address = saleUrl
}
}
}
};
// configure
var routes = new[]
{
invRoute,
prodRoute,
cartRoute,
saleRoute
};
var clusters = new[]
{
invCluster,
prodCluster,
cartCluster,
saleCluster
};
services.AddReverseProxy()
.LoadFromMemory(routes, clusters);
services.AddHealthChecks()
.AddUrlGroup(new Uri(System.IO.Path.Combine(inventoryUrl, "healthz")),
name: "inventoryapp-check", tags: new[] { "inventoryapp" })
.AddUrlGroup(new Uri(System.IO.Path.Combine(productCatalogUrl, "healthz")),
name: "productcatalogapp-check", tags: new[] { "productcatalogapp" })
.AddUrlGroup(new Uri(System.IO.Path.Combine(shoppingCartUrl, "healthz")),
name: "shoppingcartapp-check", tags: new[] { "shoppingcartapp" })
.AddUrlGroup(new Uri(System.IO.Path.Combine(saleUrl, "healthz")),
name: "saleapp-check", tags: new[] { "saleapp" });
services.AddCustomOtelWithZipkin(Config,
o =>
{
o.Endpoint = IsRunOnTye
? new Uri($"http://{Config.GetServiceUri("zipkin")?.DnsSafeHost}:9411/api/v2/spans")
: o.Endpoint;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("api");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/healthz",
new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions {Predicate = _ => true});
endpoints.MapHealthChecks("/liveness",
new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
endpoints.MapGet("/", async context =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync("<h3>WebApiGateway</h3>");
await context.Response.WriteAsync("<br>");
await context.Response.WriteAsync("<a href='/inv/info'>Inventory | </a>");
await context.Response.WriteAsync("<a href='/prod/info'>Product Catalog | </a>");
await context.Response.WriteAsync("<a href='/cart/info'>Shopping Cart | </a>");
await context.Response.WriteAsync("<a href='/sale/info'>Sale </a>");
});
endpoints.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.UseAffinitizedDestinationLookup();
proxyPipeline.UseProxyLoadBalancing();
proxyPipeline.UseRequestAffinitizer();
});
});
app.ApplicationServices.CreateLoggerConfiguration(IsRunOnTye);
}
}
}