Sample implementation of microservice with MongoDB database, Ocelot API gateway and a simple Redis Caching at the reporting service
- Empty project or minimal API
- Install-Package Ocelot
- add ocelot.json configuration
- add Logging
- Ensure CORS is enabled on the services
- Ensure there is no 403 redirects to https when testing on http
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Cache.CacheManager;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile($"ocelot.{builder.Environment.EnvironmentName}.json", true, true);
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Services.AddOcelot();
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("API Gateway!");
});
});
await app.UseOcelot();
app.Run();