Apigateway/ApiGateway/Program.cs

153 lines
4.1 KiB
C#
Raw Normal View History

2022-11-02 11:31:23 -05:00
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;
2023-01-10 10:35:15 -05:00
using System.Text;
using System.IO;
using System.Security.Policy;
2022-11-02 11:31:23 -05:00
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddOcelotWithSwaggerSupport(options =>
{
options.Folder = "Routes";
});
2023-01-10 10:35:15 -05:00
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
2024-02-09 17:57:46 -05:00
var logger = loggerFactory.CreateLogger("Gateway");
2022-11-02 11:31:23 -05:00
builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
var pipeConfig = new OcelotPipelineConfiguration
{
AuthorizationMiddleware = async (downStreamContext, next) =>
{
2023-01-10 10:35:15 -05:00
//var _bearer_token = downStreamContext.Request.Headers[HeaderNames.Authorization].ToString();
2022-11-02 11:31:23 -05:00
//logger.LogInformation("Bearer :");
//logger.LogInformation(_bearer_token);
2023-01-10 10:35:15 -05:00
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());
2022-11-02 11:31:23 -05:00
bool isAuthorized = CustomLogic.Authorize(downStreamContext);
if (isAuthorized)
{
2023-01-10 10:35:15 -05:00
var sb2 = new StringBuilder()
.AppendLine("Is authorized: ").Append(isAuthorized)
.AppendLine("Date: ").Append(date);
//logger.LogInformation(69, sb2.ToString());
2022-11-02 11:31:23 -05:00
await next.Invoke();
}
else
{
2023-01-10 10:35:15 -05:00
var sb2 = new StringBuilder()
.Append("Is authorized: ").AppendLine(isAuthorized.ToString())
.Append("Date: ").AppendLine(date)
.Append("Errors: ")
.AppendLine(downStreamContext.Items.Errors().ToString());
2024-02-09 17:57:46 -05:00
logger.LogWarning(27, sb2.ToString());
2022-11-02 11:31:23 -05:00
//downStreamContext.Items.SetError(new UnauthenticatedError("Nel wey, no estas autorizado"));
return;
}
}
};
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "MyCors", builder =>
{
2023-01-10 10:35:15 -05:00
builder
.SetIsOriginAllowed((host) => true)
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
2022-11-02 11:31:23 -05:00
});
});
builder.Services.AddOcelot(builder.Configuration).AddPolly();
builder.Services.AddSwaggerForOcelot(builder.Configuration);
//builder.Services.AddCustomJwtAuthentication();
2022-11-02 11:31:23 -05:00
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();
2022-11-02 11:31:23 -05:00
if (app.Environment.IsDevelopment())
{
Environment.SetEnvironmentVariable("Mode", "Dev");
}
else
{
Environment.SetEnvironmentVariable("Mode", "Prod");
}
// Cargue de Ocelot
app.UseMiddleware<OcelotResponseMiddleware>();
app.UseSwaggerForOcelotUI(options =>
{
2023-01-10 10:35:15 -05:00
var sb = new StringBuilder()
.Append("Inicia ocleot: ");
//logger.LogInformation(73, sb.ToString());
2023-01-10 10:35:15 -05:00
2022-11-02 11:31:23 -05:00
options.PathToSwaggerGenerator = "/swagger/docs";
options.ReConfigureUpstreamSwaggerJson = AlterUpstream.AlterUpstreamSwaggerJson;
}).UseOcelot(pipeConfig).Wait();
app.UseAuthorization();
app.UseAuthentication();
app.MapControllers();
app.MapGet("/", () => "Hello World!");
app.Run();