153 lines
4.1 KiB
C#
153 lines
4.1 KiB
C#
using ApiGateway;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Ocelot.DependencyInjection;
|
|
using Ocelot.Middleware;
|
|
using MMLib.SwaggerForOcelot.DependencyInjection;
|
|
using Ocelot.Provider.Polly;
|
|
using ApiGateway.Config;
|
|
using ApiGateway.Middleware;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Security.Policy;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
builder.Configuration.AddOcelotWithSwaggerSupport(options =>
|
|
{
|
|
options.Folder = "Routes";
|
|
});
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
|
var logger = loggerFactory.CreateLogger("Gateway");
|
|
|
|
builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
|
|
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
|
|
.AddEnvironmentVariables();
|
|
|
|
var pipeConfig = new OcelotPipelineConfiguration
|
|
{
|
|
|
|
AuthorizationMiddleware = async (downStreamContext, next) =>
|
|
{
|
|
//var _bearer_token = downStreamContext.Request.Headers[HeaderNames.Authorization].ToString();
|
|
|
|
//logger.LogInformation("Bearer :");
|
|
//logger.LogInformation(_bearer_token);
|
|
string date = DateTime.Now.ToString();
|
|
var url = downStreamContext.Items.DownstreamRoute().UpstreamPathTemplate.OriginalValue;
|
|
string path = downStreamContext.Request.Path;
|
|
|
|
var sb = new StringBuilder()
|
|
.Append("Ingreso PipeLine: ").AppendLine(date)
|
|
.Append("calledUrl: ").AppendLine(url)
|
|
.Append("path: ").AppendLine(path);
|
|
//logger.LogInformation(69, sb.ToString());
|
|
|
|
bool isAuthorized = CustomLogic.Authorize(downStreamContext);
|
|
if (isAuthorized)
|
|
{
|
|
var sb2 = new StringBuilder()
|
|
.AppendLine("Is authorized: ").Append(isAuthorized)
|
|
.AppendLine("Date: ").Append(date);
|
|
//logger.LogInformation(69, sb2.ToString());
|
|
await next.Invoke();
|
|
}
|
|
else
|
|
{
|
|
var sb2 = new StringBuilder()
|
|
.Append("Is authorized: ").AppendLine(isAuthorized.ToString())
|
|
.Append("Date: ").AppendLine(date)
|
|
.Append("Errors: ")
|
|
.AppendLine(downStreamContext.Items.Errors().ToString());
|
|
logger.LogWarning(27, sb2.ToString());
|
|
//downStreamContext.Items.SetError(new UnauthenticatedError("Nel wey, no estas autorizado"));
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(name: "MyCors", builder =>
|
|
{
|
|
builder
|
|
.SetIsOriginAllowed((host) => true)
|
|
.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.UseSwagger();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
Environment.SetEnvironmentVariable("Mode", "Dev");
|
|
}
|
|
else
|
|
{
|
|
Environment.SetEnvironmentVariable("Mode", "Prod");
|
|
}
|
|
|
|
|
|
// Cargue de Ocelot
|
|
app.UseMiddleware<OcelotResponseMiddleware>();
|
|
app.UseSwaggerForOcelotUI(options =>
|
|
{
|
|
var sb = new StringBuilder()
|
|
.Append("Inicia ocleot: ");
|
|
//logger.LogInformation(73, sb.ToString());
|
|
|
|
options.PathToSwaggerGenerator = "/swagger/docs";
|
|
options.ReConfigureUpstreamSwaggerJson = AlterUpstream.AlterUpstreamSwaggerJson;
|
|
|
|
}).UseOcelot(pipeConfig).Wait();
|
|
|
|
|
|
app.UseAuthorization();
|
|
app.UseAuthentication();
|
|
|
|
app.MapControllers();
|
|
|
|
app.MapGet("/", () => "Hello World!");
|
|
|
|
|
|
app.Run();
|
|
|
|
|