-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved a few things around and started adding support for .NET Aspire.
- Loading branch information
1 parent
e24a40e
commit 33e7437
Showing
57 changed files
with
1,133 additions
and
61 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
4 changes: 4 additions & 0 deletions
4
playground/ProfanityFilter.AppHost/CustomData/CustomWords.txt
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,4 @@ | ||
IsPostBack | ||
WebForms | ||
SilverLight | ||
SOAP |
26 changes: 26 additions & 0 deletions
26
playground/ProfanityFilter.AppHost/ProfanityFilter.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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" /> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>e5321ea8-a659-4588-a117-d53dc2c6a5cc</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.AppHost" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ProfanityFilter.Hosting\ProfanityFilter.Hosting.csproj" IsAspireProjectResource="false" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="CustomData\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
_ = builder.AddProfanityFilter("profanity-filter") | ||
.WithDataBindMount("./CustomData"); | ||
|
||
builder.Build().Run(); |
8 changes: 8 additions & 0 deletions
8
playground/ProfanityFilter.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" | ||
} | ||
} | ||
} |
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" | ||
} | ||
} | ||
} |
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
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,88 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace ProfanityFilter.Client; | ||
|
||
internal sealed class DefaultRealtimeClient( | ||
IOptions<ProfanityFilterOptions> options, | ||
ILogger<DefaultRealtimeClient> logger) : IRealtimeClient | ||
{ | ||
private readonly HubConnection _connection = new HubConnectionBuilder() | ||
.WithUrl(options.Value.RealtimeUri) | ||
.WithAutomaticReconnect() | ||
.WithStatefulReconnect() | ||
.Build(); | ||
|
||
/// <inheritdoc /> | ||
async ValueTask IRealtimeClient.StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_connection.Closed += OnHubConnectionClosedAsync; | ||
_connection.Reconnected += OnHubConnectionReconnectedAsync; | ||
_connection.Reconnecting += OnHubConnectionReconnectingAsync; | ||
|
||
await _connection.StartAsync(cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
async ValueTask IRealtimeClient.StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_connection.Closed -= OnHubConnectionClosedAsync; | ||
_connection.Reconnected -= OnHubConnectionReconnectedAsync; | ||
_connection.Reconnecting -= OnHubConnectionReconnectingAsync; | ||
|
||
await _connection.StopAsync(cancellationToken); | ||
} | ||
|
||
private Task OnHubConnectionClosedAsync(Exception? exception) | ||
{ | ||
logger.HubConnectionClosed(exception); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private Task OnHubConnectionReconnectingAsync(Exception? exception) | ||
{ | ||
logger.HubReconnecting(exception); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private Task OnHubConnectionReconnectedAsync(string? arg) | ||
{ | ||
logger.HubReconnected(arg); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc /> | ||
async IAsyncEnumerable<ProfanityFilterResponse> IRealtimeClient.LiveStreamAsync( | ||
IAsyncEnumerable<ProfanityFilterRequest> liveRequests, | ||
[EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
var channel = Channel.CreateUnbounded<ProfanityFilterResponse>(); | ||
|
||
_connection.On<ProfanityFilterResponse>( | ||
"live", response => channel.Writer.TryWrite(response)); | ||
|
||
var sendTask = Task.Run(async () => | ||
{ | ||
await foreach (var request in liveRequests.WithCancellation(cancellationToken)) | ||
{ | ||
await _connection.SendAsync("LiveStream", request, cancellationToken); | ||
} | ||
|
||
channel.Writer.Complete(); | ||
}, | ||
cancellationToken); | ||
|
||
while (await channel.Reader.WaitToReadAsync(cancellationToken)) | ||
{ | ||
while (channel.Reader.TryRead(out var response)) | ||
{ | ||
yield return response; | ||
} | ||
} | ||
|
||
await sendTask; | ||
} | ||
} |
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,54 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace ProfanityFilter.Client; | ||
|
||
/// <inheritdoc cref="IRestClient" /> | ||
internal sealed class DefaultRestClient(HttpClient client) : IRestClient | ||
{ | ||
/// <inheritdoc /> | ||
async Task<IMaybe<ProfanityFilterResponse>> IRestClient.ApplyFilterAsync(ProfanityFilterRequest request) | ||
{ | ||
using var response = await client.PostAsJsonAsync( | ||
"filter", request, JsonSerializationContext.Default.ProfanityFilterRequest); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
var result = await response.Content.ReadFromJsonAsync( | ||
JsonSerializationContext.Default.ProfanityFilterResponse); | ||
|
||
return result.AsMaybe(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
Task<IMaybe<string[]>> IRestClient.GetDataByNameAsync(string name) | ||
{ | ||
return GetMaybeAsync($"data/{name}", JsonSerializationContext.Default.StringArray); | ||
} | ||
|
||
/// <inheritdoc /> | ||
Task<IMaybe<string[]>> IRestClient.GetDataNamesAsync() | ||
{ | ||
return GetMaybeAsync("data", JsonSerializationContext.Default.StringArray); | ||
} | ||
|
||
/// <inheritdoc /> | ||
Task<IMaybe<StrategyResponse[]>> IRestClient.GetStrategiesAsync() | ||
{ | ||
return GetMaybeAsync("strategies", JsonSerializationContext.Default.StrategyResponseArray); | ||
} | ||
|
||
/// <inheritdoc /> | ||
Task<IMaybe<FilterTargetResponse[]>> IRestClient.GetTargetsAsync() | ||
{ | ||
return GetMaybeAsync("targets", JsonSerializationContext.Default.FilterTargetResponseArray); | ||
} | ||
|
||
/// <inheritdoc /> | ||
async Task<IMaybe<T>> GetMaybeAsync<T>(string uri, JsonTypeInfo<T> typeInfo) | ||
{ | ||
var data = await client.GetFromJsonAsync(uri, typeInfo); | ||
|
||
return data.AsMaybe(); | ||
} | ||
} |
Oops, something went wrong.