GithubHelp home page GithubHelp logo

amitthanki / aspnetcore.apigateway Goto Github PK

View Code? Open in Web Editor NEW

This project forked from veritassoftware/aspnetcore.apigateway

0.0 1.0 0.0 1.28 MB

Asp Net Core Api Gateway

License: MIT License

C# 100.00%

aspnetcore.apigateway's Introduction

AspNetCore.ApiGateway

Asp Net Core Api Gateway package.

Build Status

The microservices architecture uses an Api Gateway as shown below.

Architecture

The package:

  • Makes creating an Api Gateway a breeze!!
  • Authorization support
  • Swagger support.

In the solution, there are 2 back end APIs : Weather API and Stock API.

Your Gateway API exposes endpoints which are a facade over your backend API endpoints.

  • GET
  • HEAD
  • POST
  • PUT
  • PATCH
  • DELETE

API Gateway Facade

For eg. To make a GET call to the backend API, you would set up an Api and a GET Route in your Gateway API's Api Orchestrator.

Then, the client app would make a GET call to the Gateway API which would make a GET call to the backend API using HttpClient.

In your Backend API project

Let us say you have a GET endpoint like this.

  • HTTP GET - /weatherforecast/forecast

In your Gateway API project

You add a Route for the backend GET call in the Api Orchrestrator.

Add a reference to the package and...

  • Create an Api Orchestration.

    You create an Api (weatherservice) and add a Route (forecast).

    public static class ApiOrchestration
    {
        public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
        {
            var serviceProvider = app.ApplicationServices;

            var weatherService = serviceProvider.GetService<IWeatherService>();

            var weatherApiClientConfig = weatherService.GetClientConfig();

            orchestrator.AddApi("weatherservice", "http://localhost:63969/")
                                //Get
                                .AddRoute("forecast", GatewayVerb.GET, new RouteInfo { Path = "weatherforecast/forecast", ResponseType = typeof(IEnumerable<WeatherForecast>) })
                                //Head
                                .AddRoute("forecasthead", GatewayVerb.HEAD, new RouteInfo { Path = "weatherforecast/forecast" })
                                //Get using custom HttpClient
                                .AddRoute("types", GatewayVerb.GET, new RouteInfo { Path = "weatherforecast/types", ResponseType = typeof(string[]), HttpClientConfig = weatherApiClientConfig })
                                //Get with param using custom HttpClient
                                .AddRoute("type", GatewayVerb.GET, new RouteInfo { Path = "weatherforecast/types/", ResponseType = typeof(WeatherTypeResponse), HttpClientConfig = weatherApiClientConfig })
                                //Get using custom implementation
                                .AddRoute("typescustom", GatewayVerb.GET, weatherService.GetTypes)
                                //Post
                                .AddRoute("add", GatewayVerb.POST, new RouteInfo { Path = "weatherforecast/types/add", RequestType = typeof(AddWeatherTypeRequest), ResponseType = typeof(string[])})
                                //Put
                                .AddRoute("update", GatewayVerb.PUT, new RouteInfo { Path = "weatherforecast/types/update", RequestType = typeof(UpdateWeatherTypeRequest), ResponseType = typeof(string[]) })
                                //Patch
                                .AddRoute("patch", GatewayVerb.PATCH, new RouteInfo { Path = "weatherforecast/forecast/patch", ResponseType = typeof(WeatherForecast) })
                                //Delete
                                .AddRoute("remove", GatewayVerb.DELETE, new RouteInfo { Path = "weatherforecast/types/remove/", ResponseType = typeof(string[]) })
                        .AddApi("stockservice", "http://localhost:63967/")
                                .AddRoute("stocks", GatewayVerb.GET, new RouteInfo { Path = "stock", ResponseType = typeof(IEnumerable<StockQuote>) })
                                .AddRoute("stock", GatewayVerb.GET, new RouteInfo { Path = "stock/", ResponseType = typeof(StockQuote) });
        }
    }
  • Hook up in Startup.cs
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IWeatherService, WeatherService>();

            //Api gateway
            services.AddApiGateway();

            services.AddControllers();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My Api Gateway", Version = "v1" });
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();

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

            //Api gateway
            app.UseApiGateway(orchestrator => ApiOrchestration.Create(orchestrator, app));

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

The Gateway Swagger appears as shown below:

API Gateway Swagger

To call the forecast Route on the weather service Api,

you can enter the Api key and Route key into Swagger as below:

API Gateway Swagger

This will hit the weatherforecast/forecast endpoint on the backend Weather API.

You can check out how the Api Gateway supported Verbs are used below.

You can check out how the Api Gateway's endpoint Authorization support below.

Customizations

  • You can use your own HttpClient to hit the backend Api.
  • You can create your own implementation to hit the backend Api.

Viewing your Gateway's Api Orchestration

Your Gateway's Api Orchestration is published by GET /api/Gateway/orchestration endpoint.

Logging

The Api Gateway uses ILogger<ApiGatewayLog> to create logs.

In your Gateway API project, this can be used to tap into the these logs.

aspnetcore.apigateway's People

Contributors

veritassoftware avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.