4

I am trying to put together an API gateway that also serves the purpose of mashing up calls to multiple APIs. I would also like to have autogenerated Swagger, so I'd like to combine the capability of Ocelot for authorization and rate limiting but would also like to be able to create the edge custom API's in .NET code to handle translations and combinations of API calls.

Is this possible with Ocelot, and are there any examples? Does Ocelot only work with downstream API's as separate applications? I'm trying to avoid an extra hop that would only exist for mashups and composite API calls.

Kyle J V
  • 563
  • 5
  • 21
  • Did you ever find a solution to this? I'm reasoning exactly the same way you are. – killswitch Jul 06 '21 at 09:58
  • 1
    No, I abandoned using Ocelot for this reason. Ended up creating the Mashup as a web api then fronted it using Azure API Management – Kyle J V Jul 06 '21 at 10:53
  • So your APIM connects to both your mashup AND back-end microservices? I'm a bit confused on how to provide a single Swagger interface for both, to give the impression of one unified API. Also, is the mashup api itself a microservice, or does it simply call the back-end microservices and aggregate the results? – killswitch Jul 08 '21 at 07:32
  • 1
    The latter, and I just use Swashbuckle / nswag to generate – Kyle J V Jul 08 '21 at 10:57
  • So basically you still inherited the extra hop for the mashup you tried to avoid in the first place. Apparently there is a way to do mashups directly in APIM, with its own scripts and stuff, but not sure how optimal that would be. – killswitch Jul 08 '21 at 11:26
  • 1
    Yes, extra hop made very little difference in all Azure web apps – Kyle J V Jul 08 '21 at 11:29

2 Answers2

7

This is an example how to use controllers with Ocelot in the same NET6 app.

From Program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json");
builder.Services.AddControllersWithViews();
builder.Services.AddOcelot();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();

app.UseEndpoints(endpoints => {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
});

app.UseOcelot().Wait();
app.Run();

You can also add Swagger for this controllers:

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json");
builder.Services.AddSwaggerGen();
builder.Services.AddControllersWithViews();
builder.Services.AddOcelot();

var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});

app.UseEndpoints(endpoints => {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
});

app.UseOcelot().Wait();
app.Run();
  • Clear and simple, thanks ! For me it was an eye-opener that UseOcelot() must be the last thing in setting up the request pipeline. (It's logic, since Ocelot terminates the request pipeline. It simply does not call the next middleware.) Somehow I missed that until I saw your post – AardVark71 Sep 28 '22 at 06:56
1

There isn't any auto-generated swagger that's supported by Ocelot. But you can hand-roll your own solution.As far as i understand Ocelot only works by providing a ocelot.json configure with your re-reroutes configured to downstream apps/api s.

Ocelot Swagger

Darren
  • 335
  • 2
  • 11