From 75aac77310b3a1621f19f18364b45f1b9b9350b7 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sat, 4 Feb 2023 12:48:00 +0100 Subject: [PATCH] :sparkles: Add API Gateway (Ocelot) --- ApiGateway/ApiGateway.csproj | 14 ++++ ApiGateway/Program.cs | 32 ++++++++ ApiGateway/Properties/launchSettings.json | 31 ++++++++ ApiGateway/appsettings.Development.json | 8 ++ ApiGateway/appsettings.json | 9 +++ ApiGateway/ocelot.json | 93 +++++++++++++++++++++++ cat_cafe/cat_cafe.sln | 8 +- 7 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 ApiGateway/ApiGateway.csproj create mode 100644 ApiGateway/Program.cs create mode 100644 ApiGateway/Properties/launchSettings.json create mode 100644 ApiGateway/appsettings.Development.json create mode 100644 ApiGateway/appsettings.json create mode 100644 ApiGateway/ocelot.json diff --git a/ApiGateway/ApiGateway.csproj b/ApiGateway/ApiGateway.csproj new file mode 100644 index 0000000..5302daa --- /dev/null +++ b/ApiGateway/ApiGateway.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/ApiGateway/Program.cs b/ApiGateway/Program.cs new file mode 100644 index 0000000..546b5ae --- /dev/null +++ b/ApiGateway/Program.cs @@ -0,0 +1,32 @@ +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); +builder.Services.AddOcelot(builder.Configuration); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +await app.UseOcelot(); + +app.Run(); diff --git a/ApiGateway/Properties/launchSettings.json b/ApiGateway/Properties/launchSettings.json new file mode 100644 index 0000000..936506d --- /dev/null +++ b/ApiGateway/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:17820", + "sslPort": 44370 + } + }, + "profiles": { + "ApiGateway": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:5003;http://localhost:5197", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ApiGateway/appsettings.Development.json b/ApiGateway/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/ApiGateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ApiGateway/appsettings.json b/ApiGateway/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/ApiGateway/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ApiGateway/ocelot.json b/ApiGateway/ocelot.json new file mode 100644 index 0000000..ce3623b --- /dev/null +++ b/ApiGateway/ocelot.json @@ -0,0 +1,93 @@ +{ + "GlobalConfiguration": { + "BaseUrl": "https://localhost:5003" + }, + "Routes": [ + { + "UpstreamPathTemplate": "/gateway/cats", + "UpstreamHttpMethod": [ "Get" ], + "DownstreamPathTemplate": "/api/cats", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + { + "UpstreamPathTemplate": "/gateway/cats", + "UpstreamHttpMethod": [ "Post" ], + "DownstreamPathTemplate": "/api/cats", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + { + "UpstreamPathTemplate": "/gateway/cats/{id}", + "UpstreamHttpMethod": [ "Get", "Put", "Delete" ], + "DownstreamPathTemplate": "/api/cats/{id}", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + + { + "UpstreamPathTemplate": "/gateway/bars", + "UpstreamHttpMethod": [ "Get", "Post" ], + "DownstreamPathTemplate": "/api/bars", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + { + "UpstreamPathTemplate": "/gateway/bars/{id}", + "UpstreamHttpMethod": [ "Get", "Put", "Delete" ], + "DownstreamPathTemplate": "/api/bars/{id}", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + + { + "UpstreamPathTemplate": "/gateway/customers", + "UpstreamHttpMethod": [ "Get", "Post" ], + "DownstreamPathTemplate": "/api/customers", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + }, + { + "UpstreamPathTemplate": "/gateway/customers/{id}", + "UpstreamHttpMethod": [ "Get", "Put", "Delete" ], + "DownstreamPathTemplate": "/api/customers/{id}", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 7229 + } + ] + } + ] +} \ No newline at end of file diff --git a/cat_cafe/cat_cafe.sln b/cat_cafe/cat_cafe.sln index 86b7799..332759b 100644 --- a/cat_cafe/cat_cafe.sln +++ b/cat_cafe/cat_cafe.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.2.32616.157 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cat_cafe", "cat_cafe.csproj", "{CC02D05A-3817-4D0A-8766-3FEE0C90941B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "..\Tests\Tests.csproj", "{039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiGateway", "..\ApiGateway\ApiGateway.csproj", "{18B6DE0C-D6D7-452F-94A2-83DB98194D6F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Debug|Any CPU.Build.0 = Debug|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.Build.0 = Release|Any CPU + {18B6DE0C-D6D7-452F-94A2-83DB98194D6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18B6DE0C-D6D7-452F-94A2-83DB98194D6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18B6DE0C-D6D7-452F-94A2-83DB98194D6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18B6DE0C-D6D7-452F-94A2-83DB98194D6F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE