Skip to content

Commit

Permalink
Adds Mongo Express (#2091)
Browse files Browse the repository at this point in the history
* Adds Mongo Express to MongoDB builder extensions

* Move tests due to test refactor

* Simplify ctor

PR feedback

Co-authored-by: David Fowler <[email protected]>

---------

Co-authored-by: David Fowler <[email protected]>
  • Loading branch information
timheuer and davidfowl authored Feb 8, 2024
1 parent 22e10aa commit 3a80354
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
5 changes: 3 additions & 2 deletions playground/mongo/Mongo.ApiService/Mongo.ApiService.http
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@CosmosEndToEnd.ApiService_HostAddress = http://localhost:5301
@ApiService_HostAddress = http://localhost:5301

GET {{CosmosEndToEnd.ApiService_HostAddress}}/weatherforecast/
GET {{ApiService_HostAddress}}/
Accept: application/json

###

3 changes: 2 additions & 1 deletion playground/mongo/Mongo.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

var builder = DistributedApplication.CreateBuilder(args);

var db = builder.AddMongoDB("mongo");
var db = builder.AddMongoDBContainer("mongo")
.WithMongoExpress();

builder.AddProject<Projects.Mongo_ApiService>("api")
.WithReference(db);
Expand Down
40 changes: 40 additions & 0 deletions src/Aspire.Hosting/MongoDB/MongoDBBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Net.Sockets;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.MongoDB;
using Aspire.Hosting.Publishing;

namespace Aspire.Hosting;
Expand Down Expand Up @@ -69,6 +70,45 @@ public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResour
.WithManifestPublishingCallback(context => context.WriteMongoDBDatabaseToManifest(mongoDBDatabase));
}

/// <summary>
/// Adds a MongoExpress administration and development platform for MongoDB to the application model.
/// </summary>
/// <param name="builder">The MongoDB server resource builder.</param>
/// <param name="hostPort">The host port for the application ui.</param>
/// <param name="containerName">The name of the container (Optional).</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithMongoExpress<T>(this IResourceBuilder<T> builder, int? hostPort = null, string? containerName = null) where T : IMongoDBParentResource
{
containerName ??= $"{builder.Resource.Name}-mongoexpress";

var mongoExpressContainer = new MongoExpressContainerResource(containerName);
builder.ApplicationBuilder.AddResource(mongoExpressContainer)
.WithAnnotation(new ContainerImageAnnotation { Image = "mongo-express", Tag = "latest" })
.WithEnvironment(context => ConfigureMongoExpressContainer(context, builder.Resource))
.WithHttpEndpoint(containerPort: 8081, hostPort: hostPort, name: containerName)
.ExcludeFromManifest();

return builder;
}

private static void ConfigureMongoExpressContainer(EnvironmentCallbackContext context, IResource resource)
{
var hostPort = GetResourcePort(resource);

context.EnvironmentVariables.Add("ME_CONFIG_MONGODB_URL", $"mongodb://host.docker.internal:{hostPort}/?directConnection=true");

static int GetResourcePort(IResource resource)
{
if (!resource.TryGetAllocatedEndPoints(out var allocatedEndpoints))
{
throw new DistributedApplicationException(
$"MongoDB resource \"{resource.Name}\" does not have endpoint annotation.");
}

return allocatedEndpoints.Single().Port;
}
}

private static void WriteMongoDBContainerToManifest(this ManifestPublishingContext context, MongoDBContainerResource resource)
{
context.WriteContainer(resource);
Expand Down
9 changes: 9 additions & 0 deletions src/Aspire.Hosting/MongoDB/MongoExpressContainerResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.MongoDB;
internal sealed class MongoExpressContainerResource(string name) : ContainerResource(name)
{
}
20 changes: 20 additions & 0 deletions tests/Aspire.Hosting.Tests/MongoDB/AddMongoDBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Sockets;
using Aspire.Hosting.MongoDB;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

Expand Down Expand Up @@ -96,4 +97,23 @@ public void MongoDBCreatesConnectionString()

Assert.Equal("mongodb://localhost:27017/mydatabase", connectionString);
}

[Fact]
public void WithMongoExpressAddsContainer()
{
var builder = DistributedApplication.CreateBuilder();
builder.AddMongoDB("mongo").WithMongoExpress();

Assert.Single(builder.Resources.OfType<MongoExpressContainerResource>());
}

[Fact]
public void WithMongoExpressOnMultipleResources()
{
var builder = DistributedApplication.CreateBuilder();
builder.AddMongoDB("mongo").WithMongoExpress();
builder.AddMongoDB("mongo2").WithMongoExpress();

Assert.Equal(2, builder.Resources.OfType<MongoExpressContainerResource>().Count());
}
}

0 comments on commit 3a80354

Please sign in to comment.