-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #240 add papercut smtp hosting * #240 Deleted api tag from papercut * #240 set endpointname for papercut unittest * #240 try to implement health * #240 changed unittest on papercut to call papercut directly * #240 changed README.md's * #240 revert changes in non-papercut places * #240 revert non-papercut changes * #240: update Papercut SMTP integration and tests * #240 changed README.md to reflect the new connectionstring convention --------- Co-authored-by: Aaron Powell <[email protected]>
- Loading branch information
1 parent
0c1260d
commit 776688a
Showing
21 changed files
with
572 additions
and
0 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
23 changes: 23 additions & 0 deletions
23
....Hosting.PapercutSmtp.AppHost/CommunityToolkit.Aspire.Hosting.PapercutSmtp.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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireAppHostSdkVersion)" /> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>f60c6ce9-5628-467c-a6fc-2fc7938b38ad</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting" /> | ||
<PackageReference Include="Aspire.Hosting.AppHost" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.PapercutSmtp\CommunityToolkit.Aspire.Hosting.PapercutSmtp.csproj" IsAspireProjectResource="false" /> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.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,12 @@ | ||
using Aspire.Hosting; | ||
using Projects; | ||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var papercut = builder.AddPapercutSmtp("papercut"); | ||
|
||
var sendmail = builder.AddProject<CommunityToolkit_Aspire_Hosting_PapercutSmtp_SendMailApi>("sendmail") | ||
.WithReference(papercut) | ||
.WaitFor(papercut); | ||
|
||
builder.Build().Run(); |
9 changes: 9 additions & 0 deletions
9
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.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" | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
....PapercutSmtp.SendMailApi/CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi.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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
13 changes: 13 additions & 0 deletions
13
...ng.PapercutSmtp.SendMailApi/CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi.http
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,13 @@ | ||
@CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi_HostAddress = http://localhost:5075 | ||
|
||
POST http://localhost:5075/send | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"From": "[email protected]", | ||
"To": "[email protected]", | ||
"Body": "Hello World", | ||
"Subject": "Test" | ||
} | ||
### |
15 changes: 15 additions & 0 deletions
15
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi/MailData.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,15 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi; | ||
|
||
public class MailData | ||
{ | ||
[Required] | ||
public required string From { get; set; } | ||
[Required] | ||
public required string To { get; set; } | ||
[Required] | ||
public required string Body { get; set; } | ||
[Required] | ||
public required string Subject { get; set; } | ||
} |
44 changes: 44 additions & 0 deletions
44
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi/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,44 @@ | ||
using CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Data.Common; | ||
using System.Net.Mail; | ||
|
||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
||
string? papercutConnectionString = builder.Configuration.GetConnectionString("papercut"); | ||
DbConnectionStringBuilder connectionBuilder = new() | ||
{ | ||
ConnectionString = papercutConnectionString | ||
}; | ||
|
||
Uri endpoint = new(connectionBuilder["Endpoint"].ToString()!, UriKind.Absolute); | ||
builder.Services.AddScoped(_ => new SmtpClient(endpoint.Host, endpoint.Port)); | ||
builder.AddServiceDefaults(); | ||
WebApplication app = builder.Build(); | ||
|
||
app.MapPost("/send", ([FromBody]MailData mailData, [FromServices] SmtpClient smtpClient) => | ||
{ | ||
MailMessage myMail = CreateMailMessage(mailData); | ||
smtpClient.Send(myMail); | ||
}) | ||
.WithName("SendMail"); | ||
|
||
app.MapGet("/health", () => "OK"); | ||
|
||
app.MapDefaultEndpoints(); | ||
app.Run(); | ||
return; | ||
|
||
MailMessage CreateMailMessage(MailData mailData) | ||
{ | ||
MailAddress from = new(mailData.From, "TestFromName"); | ||
MailAddress to = new(mailData.To, "TestToName"); | ||
MailMessage mailMessage = new(from, to); | ||
MailAddress replyTo = new(mailData.From); | ||
mailMessage.ReplyToList.Add(replyTo); | ||
mailMessage.Subject = mailData.Subject; | ||
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8; | ||
mailMessage.Body = mailData.Body; | ||
mailMessage.BodyEncoding = System.Text.Encoding.UTF8; | ||
return mailMessage; | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi/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": "*" | ||
} |
21 changes: 21 additions & 0 deletions
21
...tSmtp.ServiceDefaults/CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults.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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireSharedProject>true</IsAspireSharedProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
|
||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" /> | ||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" /> | ||
</ItemGroup> | ||
|
||
</Project> |
118 changes: 118 additions & 0 deletions
118
examples/papercut/CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults/Extensions.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,118 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. | ||
// This project should be referenced by each service project in your solution. | ||
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults | ||
public static class Extensions | ||
{ | ||
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.ConfigureOpenTelemetry(); | ||
|
||
builder.AddDefaultHealthChecks(); | ||
|
||
builder.Services.AddServiceDiscovery(); | ||
|
||
builder.Services.ConfigureHttpClientDefaults(http => | ||
{ | ||
// Turn on resilience by default | ||
http.AddStandardResilienceHandler(); | ||
|
||
// Turn on service discovery by default | ||
http.AddServiceDiscovery(); | ||
}); | ||
|
||
// Uncomment the following to restrict the allowed schemes for service discovery. | ||
// builder.Services.Configure<ServiceDiscoveryOptions>(options => | ||
// { | ||
// options.AllowedSchemes = ["https"]; | ||
// }); | ||
|
||
return builder; | ||
} | ||
|
||
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Logging.AddOpenTelemetry(logging => | ||
{ | ||
logging.IncludeFormattedMessage = true; | ||
logging.IncludeScopes = true; | ||
}); | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddRuntimeInstrumentation(); | ||
}) | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddSource(builder.Environment.ApplicationName) | ||
.AddAspNetCoreInstrumentation() | ||
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) | ||
//.AddGrpcClientInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
}); | ||
|
||
builder.AddOpenTelemetryExporters(); | ||
|
||
return builder; | ||
} | ||
|
||
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); | ||
|
||
if (useOtlpExporter) | ||
{ | ||
builder.Services.AddOpenTelemetry().UseOtlpExporter(); | ||
} | ||
|
||
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) | ||
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) | ||
//{ | ||
// builder.Services.AddOpenTelemetry() | ||
// .UseAzureMonitor(); | ||
//} | ||
|
||
return builder; | ||
} | ||
|
||
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Services.AddHealthChecks() | ||
// Add a default liveness check to ensure app is responsive | ||
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); | ||
|
||
return builder; | ||
} | ||
|
||
public static WebApplication MapDefaultEndpoints(this WebApplication app) | ||
{ | ||
// Adding health checks endpoints to applications in non-development environments has security implications. | ||
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
// All health checks must pass for app to be considered ready to accept traffic after starting | ||
app.MapHealthChecks("/health"); | ||
|
||
// Only health checks tagged with the "live" tag must pass for app to be considered alive | ||
app.MapHealthChecks("/alive", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Tags.Contains("live") | ||
}); | ||
} | ||
|
||
return app; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...tyToolkit.Aspire.Hosting.PapercutSmtp/CommunityToolkit.Aspire.Hosting.PapercutSmtp.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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>An Aspire component leveraging Papercut SMTP container.</Description> | ||
<AdditionalPackageTags>papercut smtp hosting</AdditionalPackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="CommunityToolkit.Aspire.Hosting.PapercutSmtp.Tests" /> | ||
</ItemGroup> | ||
</Project> |
8 changes: 8 additions & 0 deletions
8
src/CommunityToolkit.Aspire.Hosting.PapercutSmtp/PapercutSmtpContainerImageTags.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 @@ | ||
namespace CommunityToolkit.Aspire.Hosting.PapercutSmtp; | ||
|
||
internal static class PapercutSmtpContainerImageTags | ||
{ | ||
public const string Registry = "docker.io"; | ||
public const string Image = "changemakerstudiosus/papercut-smtp"; | ||
public const string Tag = "7.0.0-rc1"; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/CommunityToolkit.Aspire.Hosting.PapercutSmtp/PapercutSmtpContainerResource.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,21 @@ | ||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// Resource for the Papercut SMTP server. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
public class PapercutSmtpContainerResource(string name) : ContainerResource(name), IResourceWithConnectionString | ||
{ | ||
internal const int HttpEndpointPort = 80; | ||
internal const int SmtpEndpointPort = 25; | ||
internal const string HttpEndpointName = "http"; | ||
internal const string SmtpEndpointName = "smtp"; | ||
private EndpointReference? _smtpEndpoint; | ||
private EndpointReference SmtpEndpoint => _smtpEndpoint ??= new EndpointReference(this, SmtpEndpointName); | ||
|
||
/// <summary> | ||
/// ConnectionString for the Papercut SMTP server in the form of smtp://host:port. | ||
/// </summary> | ||
public ReferenceExpression ConnectionStringExpression => ReferenceExpression.Create( | ||
$"Endpoint={SmtpEndpoint.Scheme}://{SmtpEndpoint.Property(EndpointProperty.Host)}:{SmtpEndpoint.Property(EndpointProperty.Port)}"); | ||
} |
Oops, something went wrong.