Skip to content

Commit 9749eb7

Browse files
committed
api gateway ocelot added
1 parent 19e7628 commit 9749eb7

File tree

11 files changed

+367
-0
lines changed

11 files changed

+367
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/ApiGateway.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach"
33+
}
34+
]
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/ApiGateway.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/ApiGateway.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/ApiGateway.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>0190539c-e37d-42b9-be1b-de1807da26b8</UserSecretsId>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
<DockerfileContext>..\..</DockerfileContext>
10+
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
15+
<PackageReference Include="Ocelot" Version="18.0.0" />
16+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
17+
</ItemGroup>
18+
</Project>

api-gateway/ApiGateway/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
4+
WORKDIR /app
5+
EXPOSE 5000
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
8+
WORKDIR /src
9+
COPY ["ApiGateways/Web.ApiGateway/Web.ApiGateway.csproj", "ApiGateways/Web.ApiGateway/"]
10+
RUN dotnet restore "ApiGateways/Web.ApiGateway/Web.ApiGateway.csproj"
11+
COPY . .
12+
WORKDIR "/src/ApiGateways/Web.ApiGateway"
13+
RUN dotnet build "Web.ApiGateway.csproj" -c Release -o /app/build
14+
15+
FROM build AS publish
16+
RUN dotnet publish "Web.ApiGateway.csproj" -c Release -o /app/publish
17+
18+
FROM base AS final
19+
WORKDIR /app
20+
COPY --from=publish /app/publish .
21+
ENTRYPOINT ["dotnet", "Web.ApiGateway.dll"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
4+
WORKDIR /app
5+
EXPOSE 5000
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
8+
WORKDIR /src
9+
COPY ["ApiGateways/Web.ApiGateway/Web.ApiGateway.csproj", "ApiGateways/Web.ApiGateway/"]
10+
RUN dotnet restore "ApiGateways/Web.ApiGateway/Web.ApiGateway.csproj"
11+
COPY . .
12+
WORKDIR "/src/ApiGateways/Web.ApiGateway"
13+
RUN dotnet build "Web.ApiGateway.csproj" -c Release -o /app/build
14+
15+
FROM build AS publish
16+
RUN dotnet publish "Web.ApiGateway.csproj" -c Release -o /app/publish
17+
18+
FROM base AS final
19+
WORKDIR /app
20+
COPY --from=publish /app/publish .
21+
ENTRYPOINT ["dotnet", "Web.ApiGateway.dll"]

api-gateway/ApiGateway/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Ocelot.DependencyInjection;
2+
using Ocelot.Middleware;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
8+
builder.Services.AddControllers();
9+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
10+
builder.Services.AddEndpointsApiExplorer();
11+
builder.Services.AddSwaggerGen();
12+
13+
builder.Host
14+
.ConfigureAppConfiguration((hostingContext, config) =>
15+
{
16+
config
17+
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
18+
.AddJsonFile("appsettings.json", true, true)
19+
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
20+
//.AddJsonFile("ocelot.json", false, false)
21+
.AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
22+
.AddEnvironmentVariables();
23+
})
24+
.UseDefaultServiceProvider((context, options) =>
25+
{
26+
options.ValidateOnBuild = false;
27+
options.ValidateScopes = false;
28+
});
29+
30+
builder.Services.AddOcelot();
31+
32+
builder.Services.AddCors(options =>
33+
{
34+
options.AddPolicy("CorsPolicy",
35+
builder => builder.SetIsOriginAllowed((host) => true)
36+
.AllowAnyMethod()
37+
.AllowAnyHeader()
38+
.AllowCredentials());
39+
});
40+
41+
var app = builder.Build();
42+
43+
// Configure the HTTP request pipeline.
44+
if (app.Environment.IsDevelopment())
45+
{
46+
app.UseDeveloperExceptionPage();
47+
app.UseSwagger();
48+
app.UseSwaggerUI();
49+
}
50+
51+
app.MapControllers();
52+
53+
app.UseCors("CorsPolicy");
54+
55+
await app.UseOcelot();
56+
57+
app.Run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:37207",
8+
"sslPort": 44355
9+
}
10+
},
11+
"profiles": {
12+
"Web.ApiGateway": {
13+
"commandName": "Project",
14+
"launchBrowser": false,
15+
"launchUrl": "swagger",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
},
19+
"applicationUrl": "http://localhost:5000",
20+
"dotnetRunMessages": true
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"Docker": {
31+
"commandName": "Docker",
32+
"launchBrowser": false,
33+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
34+
"publishAllPorts": true,
35+
"useSSL": true
36+
}
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
//"Logging": {
3+
// "LogLevel": {
4+
// "Default": "Information",
5+
// "Microsoft": "Warning",
6+
// "Microsoft.Hosting.Lifetime": "Information"
7+
// }
8+
//},
9+
"AllowedHosts": "*"
10+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"Routes": [
3+
{
4+
"DownstreamPathTemplate": "/{everything}",
5+
"DownstreamScheme": "http",
6+
"DownstreamHostAndPorts": [
7+
{
8+
"Host": "localhost",
9+
"Port": 5001
10+
}
11+
],
12+
"UpstreamPathTemplate": "/customer/{everything}",
13+
"UpstreamHttpMethod": [ "Get", "Delete" ],
14+
"UpstreamScheme": "http"
15+
},
16+
{
17+
"DownstreamPathTemplate": "/",
18+
"DownstreamScheme": "http",
19+
"DownstreamHostAndPorts": [
20+
{
21+
"Host": "localhost",
22+
"Port": 5001
23+
}
24+
],
25+
"UpstreamPathTemplate": "/customer",
26+
"UpstreamHttpMethod": [ "Get", "Post", "put" ],
27+
"UpstreamScheme": "http"
28+
},
29+
{
30+
"DownstreamPathTemplate": "/{everything}",
31+
"DownstreamScheme": "http",
32+
"DownstreamHostAndPorts": [
33+
{
34+
"Host": "localhost",
35+
"Port": 5002
36+
}
37+
],
38+
"UpstreamPathTemplate": "/order/{everything}",
39+
"UpstreamHttpMethod": [ "Get", "Delete", "put" ],
40+
"UpstreamScheme": "http"
41+
},
42+
{
43+
"DownstreamPathTemplate": "/",
44+
"DownstreamScheme": "http",
45+
"DownstreamHostAndPorts": [
46+
{
47+
"Host": "localhost",
48+
"Port": 5002
49+
}
50+
],
51+
"UpstreamPathTemplate": "/order",
52+
"UpstreamHttpMethod": [ "Get", "Post", "put" ],
53+
"UpstreamScheme": "http"
54+
}
55+
],
56+
"GlobalConfiguration": {
57+
"BaseUrl": "http://localhost:5000"
58+
}
59+
}

api-gateway/ApiGateway/ocelot.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"Routes": [
3+
{
4+
"DownstreamPathTemplate": "/{everything}",
5+
"DownstreamScheme": "http",
6+
"DownstreamHostAndPorts": [
7+
{
8+
"Host": "localhost",
9+
"Port": 5001
10+
}
11+
],
12+
"UpstreamPathTemplate": "/customer/{everything}",
13+
"UpstreamHttpMethod": [ "Get", "Delete" ],
14+
"UpstreamScheme": "http"
15+
},
16+
{
17+
"DownstreamPathTemplate": "/",
18+
"DownstreamScheme": "http",
19+
"DownstreamHostAndPorts": [
20+
{
21+
"Host": "localhost",
22+
"Port": 5001
23+
}
24+
],
25+
"UpstreamPathTemplate": "/customer",
26+
"UpstreamHttpMethod": [ "Get", "Post", "put" ],
27+
"UpstreamScheme": "http"
28+
},
29+
{
30+
"DownstreamPathTemplate": "/{everything}",
31+
"DownstreamScheme": "http",
32+
"DownstreamHostAndPorts": [
33+
{
34+
"Host": "localhost",
35+
"Port": 5002
36+
}
37+
],
38+
"UpstreamPathTemplate": "/order/{everything}",
39+
"UpstreamHttpMethod": [ "Get", "Delete", "put" ],
40+
"UpstreamScheme": "http"
41+
},
42+
{
43+
"DownstreamPathTemplate": "/",
44+
"DownstreamScheme": "http",
45+
"DownstreamHostAndPorts": [
46+
{
47+
"Host": "localhost",
48+
"Port": 5002
49+
}
50+
],
51+
"UpstreamPathTemplate": "/order",
52+
"UpstreamHttpMethod": [ "Get", "Post", "put" ],
53+
"UpstreamScheme": "http"
54+
}
55+
],
56+
"GlobalConfiguration": {
57+
"BaseUrl": "http://localhost:5000"
58+
}
59+
}

0 commit comments

Comments
 (0)