Re-factored and formatted OcelotApiGw project

This commit is contained in:
Rafsanul Hasan 2018-09-09 04:07:24 +06:00
parent 25cc0cf562
commit 4e774612ae
No known key found for this signature in database
GPG Key ID: FC57FD2D87BE60DD
2 changed files with 98 additions and 94 deletions

View File

@ -1,31 +1,29 @@
using System; using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using static Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions;
using static Microsoft.AspNetCore.Hosting.WebHostExtensions;
using IWebHost = Microsoft.AspNetCore.Hosting.IWebHost;
using Path = System.IO.Path;
using WebHost = Microsoft.AspNetCore.WebHost;
namespace OcelotApiGw namespace OcelotApiGw
{ {
public class Program public class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
BuildWebHost(args).Run(); BuildWebHost(args).Run();
} }
public static IWebHost BuildWebHost(string[] args) public static IWebHost BuildWebHost(string[] args)
{ {
IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args); var builder = WebHost.CreateDefaultBuilder(args);
builder.ConfigureServices(s => s.AddSingleton(builder)) builder
.ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json"))) .ConfigureServices(s => s.AddSingleton(builder))
.UseStartup<Startup>(); .ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json")))
IWebHost host = builder.Build(); .UseStartup<Startup>();
return host; var host = builder.Build();
} return host;
} }
}
} }

View File

@ -1,11 +1,5 @@
using System; using Microsoft.AspNetCore.Builder;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CacheManager.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -14,78 +8,90 @@ using Ocelot.Middleware;
namespace OcelotApiGw namespace OcelotApiGw
{ {
public class Startup public class Startup
{ {
private readonly IConfiguration _cfg; private readonly IConfiguration _cfg;
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
_cfg = configuration; _cfg = configuration;
} }
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
var identityUrl = _cfg.GetValue<string>("IdentityUrl"); var identityUrl = _cfg.GetValue<string>("IdentityUrl");
var authenticationProviderKey = "IdentityApiKey"; var authenticationProviderKey = "IdentityApiKey";
services.AddCors(options => services
{ .AddCors(options =>
options.AddPolicy("CorsPolicy", {
builder => builder.AllowAnyOrigin() options
.AllowAnyMethod() .AddPolicy(
.AllowAnyHeader() "CorsPolicy",
.AllowCredentials()); builder =>
}); builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
});
services.AddAuthentication() services
.AddJwtBearer(authenticationProviderKey, x => .AddAuthentication()
{ .AddJwtBearer(
x.Authority = identityUrl; authenticationProviderKey,
x.RequireHttpsMetadata = false; x =>
x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() {
{ x.Authority = identityUrl;
ValidAudiences = new[] { "orders", "basket", "locations", "marketing", "mobileshoppingagg", "webshoppingagg" } x.RequireHttpsMetadata = false;
}; x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
x.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents() {
{ ValidAudiences = new[] { "orders", "basket", "locations", "marketing", "mobileshoppingagg", "webshoppingagg" }
OnAuthenticationFailed = async ctx => };
{ x.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents()
int i = 0; {
}, #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
OnTokenValidated = async ctx => OnAuthenticationFailed = async ctx =>
{ #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
int i = 0; {
}, },
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
OnTokenValidated = async ctx =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
},
OnMessageReceived = async ctx => #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
{ OnMessageReceived = async ctx =>
int i = 0; #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
} {
}; }
}); };
});
services.AddOcelot(_cfg); services.AddOcelot(_cfg);
} }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ {
var pathBase = _cfg["PATH_BASE"]; var pathBase = _cfg["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase)) if (!string.IsNullOrEmpty(pathBase))
{ {
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
loggerFactory.AddConsole(_cfg.GetSection("Logging")); loggerFactory.AddConsole(_cfg.GetSection("Logging"));
app.UseCors("CorsPolicy"); app.UseCors("CorsPolicy");
app.UseOcelot().Wait(); app.UseOcelot().Wait();
} }
} }
} }