Apigateway/ApiGateway/Program.cs

146 lines
3.5 KiB
C#
Raw Normal View History

2022-11-02 11:31:23 -05:00
using ApiGateway;
using JwtAuthManager;
using Microsoft.Net.Http.Headers;
using Ocelot.Authorization;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using MMLib.SwaggerForOcelot.DependencyInjection;
using Ocelot.Provider.Polly;
using ApiGateway.Config;
using System.Net;
using Microsoft.AspNetCore.Http;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddOcelotWithSwaggerSupport(options =>
{
options.Folder = "Routes";
});
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger("");
builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
var pipeConfig = new OcelotPipelineConfiguration
{
AuthorizationMiddleware = async (downStreamContext, next) =>
{
//Authorize(downStreamContext);
var _bearer_token = downStreamContext.Request.Headers[HeaderNames.Authorization].ToString();
logger.LogInformation("Bearer 2:");
logger.LogInformation(_bearer_token);
//await next.Invoke();
//downStreamContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
//await downStreamContext.Response.WriteAsync("Nel wey ");
//downStreamContext.Items.SetError(new UnauthenticatedError("Ni modo, wey"));
//return;
//var cifrado = Encipher(_bearer_token, cipherKey);
//var descifrado = Decipher(cifrado, cipherKey);
bool isAuthorized = CustomLogic.Authorize(downStreamContext);
if (isAuthorized)
{
await next.Invoke();
}
else
{
//downStreamContext.Items.SetError(new UnauthenticatedError("Nel wey, no estas autorizado"));
return;
}
}
};
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "MyCors", builder =>
{
builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
builder.Services.AddOcelot(builder.Configuration).AddPolly();
builder.Services.AddSwaggerForOcelot(builder.Configuration);
builder.Services.AddCustomJwtAuthentication();
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddOcelot("Routes", builder.Environment)
.AddEnvironmentVariables();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
// Swagger for ocelot
builder.Services.AddSwaggerGen();
builder.Services.AddAuthorization();
builder.Services.AddAuthentication();
var app = builder.Build();
app.UseCors("MyCors");
//app.UseCors("widthoutCors");
app.UseSwagger();
//Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
//{
//}
if (app.Environment.IsDevelopment())
{
Environment.SetEnvironmentVariable("Mode", "Dev");
}
else
{
Environment.SetEnvironmentVariable("Mode", "Prod");
}
//IApplicationBuilder config2 = new IApplicationBuilder
// Cargue de Ocelot
app.UseMiddleware<OcelotResponseMiddleware>();
app.UseSwaggerForOcelotUI(options =>
{
options.PathToSwaggerGenerator = "/swagger/docs";
options.ReConfigureUpstreamSwaggerJson = AlterUpstream.AlterUpstreamSwaggerJson;
}).UseOcelot(pipeConfig).Wait();
app.UseAuthorization();
app.UseAuthentication();
app.MapControllers();
app.MapGet("/", () => "Hello World!");
app.Run();