fancyDevelopment / Fancy.ResourceLinker

A set of libraries to easily create API Gateways, Backend for Frontends (BFF) and truly RESTful Web APIs based on ASP.NET Core
Apache License 2.0
19 stars 5 forks source link

Fancy.ResourceLinker

A library to easily create API Gateways and Backend for Frontends (BFF) based on ASP.NET Core.

Why an api Gateway/Backend for Frontend

If you need to access multiple backend systems from a single frontend, then it is often better the frontend has a single system it communicates with and which is also located closer to the backend systems instead of letting the frontend communicate with each single backend directly. The following picture depicts this connstellation.

architecture

The advantages you get if you use the API Gateway or Backend for Frontend pattern are:

Features

With Fancy Resource Linker you can easily add the following features to your API Gateway:

Sample Application

There is a sample application Fancy.ResourceLinker-Sample which demonstrates all the features of this library. Have a look to it to see how the individual fetures can be used.

Getting Started

First add the Fancy.ResourceLinker.Gateway nuget package to your project.

To get started building an api gateway with Fancy.ResourceLinker in your ASP.NET Core application add the required services to the service collection and load a configuration by providing a configuration section.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGateway()
                .LoadConfiguration(builder.Configuration.GetSection("Gateway"))
                .AddRouting();

In your application add a configuration section with the name "Gateway" and create the default structure within it as shown in the following sample snippet:

"Gateway": {
  "Routing": {
    "Routes": {
      "Microservice1": {
        "BaseUrl": "http://localhost:5000",
      }
    }
  }
}

Finally you can ask for a type called GatewayRouter via dependency injection and use it to make calls to your microservices/backends using the name of your route in the configuration and a relative path to the data you would like to retrieve.

[ApiController]
public class HomeController : HypermediaController
{
    private readonly GatewayRouter _router;

    public HomeController(GatewayRouter router)
    {
        _router = router;
    }

    [HttpGet]
    [Route("api/views/home")]
    public async Task<IActionResult> GetHomeViewModel()
    {
        var dataFromMicroservice = _router.GetAsync<TypeToDeserializeData>("Microservice1", "api/data/123");
        [...]
    }
}

With this basic set up you can make calls to your microservices easily and change the base address in the configuration. Read through the advanced features docs to extend your api gateway with more capabilities.

Realize Advanced Features in your Gateway

To learn how each single feature can be realized have a look to the following individual guidelines.