-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for acting as a .NET Aspire client integration.
- Loading branch information
1 parent
11c6ede
commit 272cc6b
Showing
94 changed files
with
62,831 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...crosoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Microsoft.Azure.CosmosRepository\Microsoft.Azure.CosmosRepository.csproj" /> | ||
<ProjectReference Include="..\AspireApp.ServiceDefaults\AspireApp.ServiceDefaults.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/GlobalUsings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
global using AspireApp.ApiService; | ||
|
||
global using Microsoft.AspNetCore.HttpLogging; | ||
global using Microsoft.AspNetCore.Mvc; | ||
global using Microsoft.Azure.CosmosRepository; |
33 changes: 33 additions & 0 deletions
33
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add service defaults & Aspire client integrations. | ||
builder.AddServiceDefaults(); | ||
|
||
builder.AddCosmosRepositoryClient("cosmos-repository"); | ||
|
||
// Add services to the container. | ||
builder.Services.AddProblemDetails(); | ||
|
||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi | ||
builder.Services.AddOpenApi(); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
app.UseExceptionHandler(); | ||
|
||
app.UseSwagger(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwaggerUI(); | ||
app.MapOpenApi(); | ||
} | ||
|
||
app.MapTodoApi(); | ||
app.MapDefaultEndpoints(); | ||
|
||
app.Run(); |
52 changes: 52 additions & 0 deletions
52
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/Todo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace AspireApp.ApiService; | ||
|
||
/// <summary> | ||
/// Represents a todo item. | ||
/// </summary> | ||
/// <remarks> | ||
/// <example> | ||
/// Consider the following example: | ||
/// <code lang="csharp"> | ||
/// Todo todo = "🐕 Walk the dog."; | ||
/// | ||
/// // Imagine time passes and you actually walk the dog. | ||
/// // To update the todo, mark it as complete: | ||
/// | ||
/// todo.IsCompleted = true; | ||
/// </code> | ||
/// </example> | ||
/// </remarks> | ||
public sealed class Todo : Item | ||
{ | ||
/// <summary> | ||
/// Gets or sets the title of the todo. | ||
/// </summary> | ||
[JsonProperty("title")] | ||
public string Title { get; set; } = ""; | ||
|
||
/// <summary> | ||
/// Gets or sets whether the todo is considered complete. | ||
/// </summary> | ||
[JsonProperty("isCompleted")] | ||
public bool IsCompleted { get; set; } | ||
|
||
/// <summary> | ||
/// Implicitly converts the given <see cref="string"/> | ||
/// <paramref name="title"/> to a <see cref="Todo"/>. | ||
/// </summary> | ||
/// <param name="title">The title of the todo.</param> | ||
/// <returns> | ||
/// A new <see cref="Todo"/> instance with the | ||
/// given <paramref name="title"/> as the <see cref="Title"/>. | ||
/// </returns> | ||
public static implicit operator Todo(string title) => new() | ||
{ | ||
Title = title, | ||
IsCompleted = false | ||
}; | ||
} |
78 changes: 78 additions & 0 deletions
78
...Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/TodoEndpointExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace AspireApp.ApiService; | ||
|
||
public static class TodoEndpointExtensions | ||
{ | ||
public static IEndpointRouteBuilder MapTodoApi(this IEndpointRouteBuilder app) | ||
{ | ||
var todos = app.MapGroup("/api/todos"); | ||
|
||
todos.MapGet("", GetTodosAsync) | ||
.WithOpenApi() | ||
.Produces<Todo[]>(200) | ||
.WithHttpLogging(HttpLoggingFields.All); | ||
|
||
todos.MapGet("/{id}", GetTodoAsync) | ||
.WithOpenApi() | ||
.Produces<Todo>(200) | ||
.Produces(404) | ||
.WithHttpLogging(HttpLoggingFields.All); | ||
|
||
todos.MapPost("", CreateTodoAsync) | ||
.WithOpenApi() | ||
.Produces(201) | ||
.Produces(400) | ||
.WithHttpLogging(HttpLoggingFields.All); | ||
|
||
todos.MapPut("", UpdateTodoAsync) | ||
.WithOpenApi() | ||
.Produces(204) | ||
.Produces(404) | ||
.WithHttpLogging(HttpLoggingFields.All); | ||
|
||
todos.MapDelete("/{id}", DeleteTodoAsync) | ||
.WithOpenApi() | ||
.Produces(204) | ||
.Produces(404) | ||
.WithHttpLogging(HttpLoggingFields.All); | ||
|
||
return app; | ||
} | ||
|
||
static IAsyncEnumerable<Todo> GetTodosAsync([FromServices] IReadOnlyRepository<Todo> repository) | ||
=> repository.PageAsync(predicate: todo => todo != null, limit: 1_000, pageSize: 25); | ||
|
||
static async Task<IResult> GetTodoAsync([FromRoute] string id, [FromServices] IRepository<Todo> repository) | ||
{ | ||
var todo = await repository.GetAsync(id); | ||
|
||
return todo is null | ||
? Results.NotFound() | ||
: Results.Json(todo); | ||
} | ||
|
||
static async Task<IResult> CreateTodoAsync([FromBody] Todo todo, [FromServices] IRepository<Todo> repository) | ||
{ | ||
var result = await repository.CreateAsync(todo); | ||
|
||
return result is null | ||
? Results.BadRequest() | ||
: Results.Created($"/api/todos/{result.Id}", result); | ||
} | ||
|
||
static async Task<IResult> UpdateTodoAsync([FromBody] Todo inputTodo, [FromServices] IRepository<Todo> repository) | ||
{ | ||
await repository.UpdateAsync(inputTodo); | ||
|
||
return Results.NoContent(); | ||
} | ||
|
||
static async Task<IResult> DeleteTodoAsync([FromRoute] string id, [FromServices] IRepository<Todo> repository) | ||
{ | ||
await repository.DeleteAsync(id); | ||
|
||
return Results.NoContent(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...rosoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.ApiService/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
24 changes: 24 additions & 0 deletions
24
...les/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" /> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>e8bdcee5-71ce-4928-a339-f8dbed60e7db</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AspireApp.ApiService\AspireApp.ApiService.csproj" /> | ||
<ProjectReference Include="..\AspireApp.Web\AspireApp.Web.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.AppHost" /> | ||
<PackageReference Include="Aspire.Hosting.Azure.CosmosDB" /> | ||
</ItemGroup> | ||
|
||
</Project> |
20 changes: 20 additions & 0 deletions
20
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.AppHost/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (cosmosContainer) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var db = builder.AddAzureCosmosDB("cosmos-repository") | ||
.RunAsEmulator(static cosmosContainer => | ||
cosmosContainer.WithLifetime(ContainerLifetime.Persistent)); | ||
|
||
var apiService = builder.AddProject<Projects.AspireApp_ApiService>("apiservice") | ||
.WithExternalHttpEndpoints() | ||
.WithReference(db) | ||
.WaitFor(db); | ||
|
||
builder.AddProject<Projects.AspireApp_Web>("webfrontend") | ||
.WithExternalHttpEndpoints() | ||
.WithReference(apiService) | ||
.WaitFor(apiService); | ||
|
||
builder.Build().Run(); |
8 changes: 8 additions & 0 deletions
8
...Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.AppHost/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
samples/Microsoft.Azure.CosmosRepository/AspireApp/AspireApp.AppHost/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Aspire.Hosting.Dcp": "Warning" | ||
} | ||
} | ||
} |
Oops, something went wrong.