Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample for BFF with openapi #232

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/OpenApi.Api1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<OpenApiDocumentsDirectory>.</OpenApiDocumentsDirectory>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenApi.DevServer.ServiceDefaults\OpenApi.DevServer.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/OpenApi.Api1.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@OpenApi.Api1_HostAddress = https://localhost:7289

GET {{OpenApi.Api1_HostAddress}}/weatherforecast/
Accept: application/json

###
70 changes: 70 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/OpenApi.Api1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenApi.Api1 | v1",
"version": "1.0.0"
},
"paths": {
"/weatherforecastFahrenheit": {
"get": {
"tags": [
"OpenApi.Api1"
],
"operationId": "GetWeatherForecast",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"WeatherForecast": {
"required": [
"date",
"temperatureF",
"summary"
],
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date"
},
"temperatureF": {
"type": "integer",
"format": "int32"
},
"summary": {
"type": "string",
"nullable": true
}
}
}
},
"securitySchemes": {
"Bearer": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "Json Web Token"
}
}
},
"tags": [
{
"name": "OpenApi.Api1"
}
]
}
46 changes: 46 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddDefaultAuthentication();
builder.AddDefaultOpenApiConfig();

var app = builder.Build();

app.MapDefaultEndpoints();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecastFahrenheit", (HttpContext c) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-4, 131),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.RequireAuthorization()

.WithName("GetWeatherForecast");

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureF, string? Summary)
{

}
14 changes: 14 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7289;http://localhost:5265",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api1/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
21 changes: 21 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/OpenApi.Api2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<OpenApiDocumentsDirectory>.</OpenApiDocumentsDirectory>
</PropertyGroup>

<ItemGroup>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenApi.DevServer.ServiceDefaults\OpenApi.DevServer.ServiceDefaults.csproj" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/OpenApi.Api2.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@OpenApi.Api2_HostAddress = https://localhost:7289

GET {{OpenApi.Api2_HostAddress}}/weatherforecastfahrenheit/
Accept: application/json

###
70 changes: 70 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/OpenApi.Api2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenApi.Api2 | v1",
"version": "1.0.0"
},
"paths": {
"/WeatherforecastCelcius": {
"get": {
"tags": [
"OpenApi.Api2"
],
"operationId": "GetWeatherForecast",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"WeatherForecast": {
"required": [
"date",
"temperatureC",
"summary"
],
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date"
},
"temperatureC": {
"type": "integer",
"format": "int32"
},
"summary": {
"type": "string",
"nullable": true
}
}
}
},
"securitySchemes": {
"Bearer": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "Json Web Token"
}
}
},
"tags": [
{
"name": "OpenApi.Api2"
}
]
}
45 changes: 45 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddDefaultAuthentication();

builder.AddDefaultOpenApiConfig();

var app = builder.Build();

app.MapDefaultEndpoints();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/WeatherforecastCelcius", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast").RequireAuthorization();
;

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
}
14 changes: 14 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7297;http://localhost:5084",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Api2/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
29 changes: 29 additions & 0 deletions BFF/v3/OpenApi/OpenApi.Bff/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Duende.Bff;

namespace OpenApi.Bff;

/// <summary>
/// Configuration section
/// </summary>
public class Configuration
{
public string? Authority { get; set; }

public string? ClientId { get; set; }

/// <summary>
/// should be supplied as a command line argument or environment variable, e.g.
/// ./GenericBFF --BFF:ClientSecret=secret
/// </summary>
public string? ClientSecret { get; set; }

public List<string> Scopes { get; set; } = new();
public List<Api> Apis { get; set; } = new();
}

public class Api
{
public string? LocalPath { get; set; }
public string? RemoteUrl { get; set; }
public TokenType RequiredToken { get; set; }
}
Loading
Loading