Skip to content

Commit

Permalink
Merge pull request #1729 from Erwinvandervalk/bff-aspire-cleanup
Browse files Browse the repository at this point in the history
Cleanup for the BFF project
  • Loading branch information
Erwinvandervalk authored Jan 27, 2025
2 parents 2fc9d4f + 71e33a3 commit 0bd85e8
Show file tree
Hide file tree
Showing 77 changed files with 2,625 additions and 796 deletions.
1,984 changes: 1,984 additions & 0 deletions bff/.editorconfig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bff/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ keys
Duende.BFF.db
*.db-shm
*.db-wal
.vs/
15 changes: 10 additions & 5 deletions bff/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Update="FluentAssertions" Version="6.7.0" />
<PackageReference Update="AngleSharp" Version="1.2.0" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
<PackageReference Update="Aspire.Hosting.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Update="Microsoft.Extensions.TimeProvider.Testing" Version="8.8.0" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Update="Serilog" Version="4.2.0" />
<PackageReference Update="Serilog.Sinks.TextWriter" Version="3.0.0" />
<PackageReference Update="Serilog.Sinks.XUnit" Version="3.0.19" />
<PackageReference Update="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Update="Shouldly" Version="4.2.1" />
<PackageReference Update="xunit" Version="2.9.2" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Update="xunit.core" Version="2.9.2" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageReference Update="Xunit.SkippableFact" Version="1.5.23" />
</ItemGroup>

<Target Name="SetAssemblyVersion" AfterTargets="MinVer">
Expand Down
2 changes: 1 addition & 1 deletion bff/Duende.Bff.sln
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Global
{2A04808A-A06C-4F10-87B9-2D12E065F729} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
{8B943A54-F50C-4946-8D0E-DA1B886F13D6} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DAD5980-4688-4794-9CF0-6F3CB67194E7}
Expand Down
4 changes: 3 additions & 1 deletion bff/Duende.Bff.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dpop/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
20 changes: 20 additions & 0 deletions bff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ If you're ready to dive into development, check out our [Quickstart Tutorial](ht

For more in-depth documentation, visit [our documentation portal](https://docs.duendesoftware.com).

## Running the Hosts.AppHost project

The Hosts.AppHost project is an Aspnet Aspire project that launches all dependencies. For example, it starts an identity server and various ways
that the BFF can be configured. Use this to test if the functionality is still working.

There's also an integration test project covering this. This project can run in 3 modes:

1. **Directly**. Then the test fixture will launch an aspire test host. It will run all tests against the aspire test host.
2. **With manually run aspire host.** The advantage of this is that you can keep your aspire host running and only iterate on your tests. This is more efficient for writing the tests.
It also leaves the door open to re-using these tests to run them against a deployed in stance somewhere in the future. Downside is that you cannot debug both your tests and host at the same time because visual studio compiles them in the same location.
3. **With NCrunch**. It turns out that NCrunch doesn't support building aspire projects. Iterating over the tests using ncrunch is the fastest way to get feedback. However, to make this work, conditional compilation is used.


Starting the host can be done via the UI (set as startup project using 'HTTPS' as the launch profile). It can also be started from the command line (which makes iterating over the tests faster)
Running it with configuration release means you can compile / modify the tests while keeping the dev server running.

``` powershell
dotnet run -p samples/Hosts.AppHost --Configuration Release
```

## Licensing
Duende.BFF is source-available, but requires a paid [license](https://duendesoftware.com/products/bff) for production use.

Expand Down
29 changes: 13 additions & 16 deletions bff/build/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
 using System;
using System;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

using static Bullseye.Targets;
Expand All @@ -12,8 +10,8 @@ namespace build
{
internal static class Program
{
private const string packOutput = "./artifacts";
private const string envVarMissing = " environment variable is missing. Aborting.";
private const string PackOutput = "./artifacts";
private const string EnvVarMissing = " environment variable is missing. Aborting.";

private static class Targets
{
Expand All @@ -23,7 +21,6 @@ private static class Targets
public const string Build = "build";
public const string Test = "test";
public const string Pack = "pack";
public const string SignBinary = "sign-binary";
public const string SignPackage = "sign-package";
}

Expand Down Expand Up @@ -60,19 +57,19 @@ internal static async Task Main(string[] args)

Target(Targets.CleanPackOutput, () =>
{
if (Directory.Exists(packOutput))
if (Directory.Exists(PackOutput))
{
Directory.Delete(packOutput, true);
Directory.Delete(PackOutput, true);
}
});

Target(Targets.Pack, DependsOn(Targets.Build, Targets.CleanPackOutput), () =>
{
Run("dotnet", $"pack ./src/Duende.Bff/Duende.Bff.csproj -c Release -o {Directory.CreateDirectory(packOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.EntityFramework/Duende.Bff.EntityFramework.csproj -c Release -o {Directory.CreateDirectory(packOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Yarp/Duende.Bff.Yarp.csproj -c Release -o {Directory.CreateDirectory(packOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Blazor/Duende.Bff.Blazor.csproj -c Release -o {Directory.CreateDirectory(packOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Blazor.Client/Duende.Bff.Blazor.Client.csproj -c Release -o {Directory.CreateDirectory(packOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff/Duende.Bff.csproj -c Release -o {Directory.CreateDirectory(PackOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.EntityFramework/Duende.Bff.EntityFramework.csproj -c Release -o {Directory.CreateDirectory(PackOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Yarp/Duende.Bff.Yarp.csproj -c Release -o {Directory.CreateDirectory(PackOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Blazor/Duende.Bff.Blazor.csproj -c Release -o {Directory.CreateDirectory(PackOutput).FullName} --no-build --nologo");
Run("dotnet", $"pack ./src/Duende.Bff.Blazor.Client/Duende.Bff.Blazor.Client.csproj -c Release -o {Directory.CreateDirectory(PackOutput).FullName} --no-build --nologo");
});

Target(Targets.SignPackage, DependsOn(Targets.Pack, Targets.RestoreTools), () =>
Expand All @@ -84,7 +81,7 @@ internal static async Task Main(string[] args)

Target("sign", DependsOn(Targets.Test, Targets.SignPackage));

await RunTargetsAndExitAsync(args, ex => ex is SimpleExec.ExitCodeException || ex.Message.EndsWith(envVarMissing));
await RunTargetsAndExitAsync(args, ex => ex is SimpleExec.ExitCodeException || ex.Message.EndsWith(EnvVarMissing));
}

private static void SignNuGet()
Expand All @@ -93,10 +90,10 @@ private static void SignNuGet()

if (string.IsNullOrWhiteSpace(signClientSecret))
{
throw new Exception($"SignClientSecret{envVarMissing}");
throw new Exception($"SignClientSecret{EnvVarMissing}");
}

foreach (var file in Directory.GetFiles(packOutput, "*.nupkg", SearchOption.AllDirectories))
foreach (var file in Directory.GetFiles(PackOutput, "*.nupkg", SearchOption.AllDirectories))
{
Console.WriteLine($" Signing {file}");

Expand Down
12 changes: 6 additions & 6 deletions bff/migrations/UserSessionDb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public static void Main(string[] args)
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
3 changes: 0 additions & 3 deletions bff/samples/Apis/Api.DPoP/DPoP/DPoPProofValidatonContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Collections.Generic;
using System.Security.Claims;

namespace Api.DPoP;

public class DPoPProofValidatonContext
Expand Down
5 changes: 3 additions & 2 deletions bff/samples/Apis/Api.DPoP/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Duende.AspNetCore.Authentication.JwtBearer.DPoP;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;

namespace Api.DPoP;

internal static class Extensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
Expand Down Expand Up @@ -83,4 +84,4 @@ public static WebApplication ConfigurePipeline(this WebApplication app)

return app;
}
}
}
1 change: 1 addition & 0 deletions bff/samples/Apis/Api.DPoP/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Api.DPoP;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
Expand Down
20 changes: 10 additions & 10 deletions bff/samples/Apis/Api.DPoP/ToDoController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Duende Software. All rights reserved.
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Microsoft.AspNetCore.Mvc;
Expand All @@ -15,7 +15,7 @@ public class ToDoController : ControllerBase
{
private readonly ILogger<ToDoController> _logger;

private static readonly List<ToDo> __data = new List<ToDo>()
private static readonly List<ToDo> Data = new List<ToDo>()
{
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "bob" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "bob" },
Expand All @@ -32,13 +32,13 @@ public IActionResult GetAll()
{
_logger.LogInformation("GetAll");

return Ok(__data.AsEnumerable());
return Ok(Data.AsEnumerable());
}

[HttpGet("todos/{id}")]
public IActionResult Get(int id)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

_logger.LogInformation("Get {id}", id);
Expand All @@ -51,7 +51,7 @@ public IActionResult Post([FromBody] ToDo model)
model.Id = ToDo.NewId();
model.User = $"{User.FindFirst("sub").Value} ({User.FindFirst("name").Value})";

__data.Add(model);
Data.Add(model);
_logger.LogInformation("Add {name}", model.Name);

return Created(Url.Action(nameof(Get), new { id = model.Id }), model);
Expand All @@ -60,7 +60,7 @@ public IActionResult Post([FromBody] ToDo model)
[HttpPut("todos/{id}")]
public IActionResult Put(int id, [FromBody] ToDo model)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

item.Date = model.Date;
Expand All @@ -74,10 +74,10 @@ public IActionResult Put(int id, [FromBody] ToDo model)
[HttpDelete("todos/{id}")]
public IActionResult Delete(int id)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

__data.Remove(item);
Data.Remove(item);
_logger.LogInformation("Delete {id}", id);

return NoContent();
Expand All @@ -86,10 +86,10 @@ public IActionResult Delete(int id)

public class ToDo
{
static int _nextId = 1;
static int NextId = 1;
public static int NewId()
{
return _nextId++;
return NextId++;
}

public int Id { get; set; }
Expand Down
18 changes: 9 additions & 9 deletions bff/samples/Apis/Api.Isolated/ToDoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ToDoController : ControllerBase
{
private readonly ILogger<ToDoController> _logger;

private static readonly List<ToDo> __data = new List<ToDo>()
private static readonly List<ToDo> Data = new List<ToDo>()
{
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "bob" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "bob" },
Expand All @@ -32,13 +32,13 @@ public IActionResult GetAll()
{
_logger.LogInformation("GetAll");

return Ok(__data.AsEnumerable());
return Ok(Data.AsEnumerable());
}

[HttpGet("todos/{id}")]
public IActionResult Get(int id)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

_logger.LogInformation("Get {id}", id);
Expand All @@ -51,7 +51,7 @@ public IActionResult Post([FromBody] ToDo model)
model.Id = ToDo.NewId();
model.User = $"{User.FindFirst("sub").Value} ({User.FindFirst("name").Value})";

__data.Add(model);
Data.Add(model);
_logger.LogInformation("Add {name}", model.Name);

return Created(Url.Action(nameof(Get), new { id = model.Id }), model);
Expand All @@ -60,7 +60,7 @@ public IActionResult Post([FromBody] ToDo model)
[HttpPut("todos/{id}")]
public IActionResult Put(int id, [FromBody] ToDo model)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

item.Date = model.Date;
Expand All @@ -74,10 +74,10 @@ public IActionResult Put(int id, [FromBody] ToDo model)
[HttpDelete("todos/{id}")]
public IActionResult Delete(int id)
{
var item = __data.FirstOrDefault(x => x.Id == id);
var item = Data.FirstOrDefault(x => x.Id == id);
if (item == null) return NotFound();

__data.Remove(item);
Data.Remove(item);
_logger.LogInformation("Delete {id}", id);

return NoContent();
Expand All @@ -86,10 +86,10 @@ public IActionResult Delete(int id)

public class ToDo
{
static int _nextId = 1;
static int NextId = 1;
public static int NewId()
{
return _nextId++;
return NextId++;
}

public int Id { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions bff/samples/Apis/Api/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.IdentityModel.Tokens;
using Serilog;

namespace Api;

internal static class Extensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
Expand Down
1 change: 1 addition & 0 deletions bff/samples/Apis/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Api;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
Expand Down
4 changes: 2 additions & 2 deletions bff/samples/Apis/Api/ToDoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public IActionResult Delete(int id)

public class ToDo
{
static int _nextId = 1;
static int NextId = 1;
public static int NewId()
{
return _nextId++;
return NextId++;
}

public int Id { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Bff.DPoP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,6 @@ private static void MapRemoteUrls(IEndpointRouteBuilder app)

// On this path, we require the user token
app.MapRemoteBffApiEndpoint("/api/user-token", "https://localhost:5011")
.RequireAccessToken(TokenType.User);
.RequireAccessToken();
}
}
2 changes: 1 addition & 1 deletion bff/samples/Bff.EF/Bff.EF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Bff.DPoP</RootNamespace>
<RootNamespace>Bff.EF</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 0bd85e8

Please sign in to comment.