Skip to content

Commit

Permalink
Merge pull request #159 from DuendeSoftware/joe/angular-net8
Browse files Browse the repository at this point in the history
Rebuild Angular sample with new template and .NET 8
  • Loading branch information
brockallen authored Jan 23, 2024
2 parents 252363a + f6cf367 commit fc21241
Show file tree
Hide file tree
Showing 92 changed files with 14,026 additions and 22,353 deletions.
41 changes: 41 additions & 0 deletions IdentityServer/v7/BFF/Angular/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "0.2.0",
"compounds": [
{
"name": "All",
"configurations": ["API", "BFF"],
"presentation": {
"hidden": false,
"group": "compunds",
}
},
],
"configurations": [
{
"name": "API",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-api",
"program": "${workspaceFolder}/Angular.Api/bin/Debug/net8.0/Angular.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/Angular.Api",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"console": "externalTerminal",
},
{
"name": "BFF",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-bff",
"program": "${workspaceFolder}/Angular.Bff/bin/Debug/net8.0/Angular.Bff.dll",
"args": [],
"cwd": "${workspaceFolder}/Angular.Bff",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"console": "externalTerminal",
},
]
}
42 changes: 42 additions & 0 deletions IdentityServer/v7/BFF/Angular/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/Angular.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-api",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/Angular.Api/Angular.Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-bff",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/Angular.Bff/Angular.Bff.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
]

}
14 changes: 14 additions & 0 deletions IdentityServer/v7/BFF/Angular/Angular.Api/Angular.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions IdentityServer/v7/BFF/Angular/Angular.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Angular.Api;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication("token")
.AddJwtBearer("token", options =>
{
options.Authority = "https://demo.duendesoftware.com";
options.Audience = "api";

options.MapInboundClaims = false;
});

builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ApiCaller", policy =>
{
policy.RequireClaim("scope", "api");
});

options.AddPolicy("InteractiveUser", policy =>
{
policy.RequireClaim("sub");
});
});

var app = builder.Build();

app.UseHttpsRedirection();

app.MapGroup("/todos")
.ToDoGroup()
.RequireAuthorization("ApiCaller", "InteractiveUser");


app.Run();

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
76 changes: 76 additions & 0 deletions IdentityServer/v7/BFF/Angular/Angular.Api/ToDoEndpointGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Http.Extensions;
using System.Security.Claims;

namespace Angular.Api;

public static class TodoEndpointGroup
{
private static readonly List<ToDo> data = new List<ToDo>()
{
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(4), Name = "Have Dinner", User = "1 (Alice Smith)" },
};

public static RouteGroupBuilder ToDoGroup(this RouteGroupBuilder group)
{
// GET
group.MapGet("/", () => data);
group.MapGet("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
});

// POST
group.MapPost("/", (ToDo model, ClaimsPrincipal user, HttpContext context) =>
{
model.Id = ToDo.NewId();
model.User = $"{user.FindFirst("sub")?.Value} ({user.FindFirst("name")?.Value})";

data.Add(model);

var url = new Uri($"{context.Request.GetEncodedUrl()}/{model.Id}");

return Results.Created(url, model);
});

// PUT
group.MapPut("/{id}", (int id, ToDo model, ClaimsPrincipal User) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

item.Date = model.Date;
item.Name = model.Name;

return Results.NoContent();
});

// DELETE
group.MapDelete("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

data.Remove(item);

return Results.NoContent();
});

return group;
}
}

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

public int Id { get; set; }
public DateTimeOffset Date { get; set; }
public string? Name { get; set; }
public string? User { get; set; }
}
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 IdentityServer/v7/BFF/Angular/Angular.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
27 changes: 27 additions & 0 deletions IdentityServer/v7/BFF/Angular/Angular.Bff/Angular.Bff.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<SpaRoot>..\angular.client</SpaRoot>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<SpaProxyServerUrl>https://localhost:4200</SpaProxyServerUrl>
<RootNamespace>Angular.Bff</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\angular.client\angular.client.esproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Duende.Bff.Yarp" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
<Version>8.*-*</Version>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Angular.Bff;
using Duende.Bff.Yarp;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddBff()
.AddRemoteApis();

Expand Down Expand Up @@ -40,22 +40,32 @@
};
});

builder.Services.AddAuthorization();

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseBff();
app.UseAuthorization();
app.MapBffManagementEndpoints();

app.MapControllers()
// Comment this out to use the external api
app.MapGroup("/todos")
.ToDoGroup()
.RequireAuthorization()
.AsBffApiEndpoint();

// app.MapRemoteBffApiEndpoint("/todos", "https://localhost:5020/todos")
// .RequireAccessToken(Duende.Bff.TokenType.User);
// Comment this in to use the external api
//app.MapRemoteBffApiEndpoint("/todos", "https://localhost:7001/todos")
// .RequireAccessToken(Duende.Bff.TokenType.User);

app.MapFallbackToFile("index.html"); ;
app.MapFallbackToFile("/index.html");

app.Run();

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:6001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
}
}

Loading

0 comments on commit fc21241

Please sign in to comment.