CommunityToolkit / Aspire

A community project with additional components and extensions for .NET Aspire
MIT License
218 stars 27 forks source link

AddDataAPIBuilder() doesn't support multiple configuration files. #187

Closed JerryNixon closed 1 week ago

JerryNixon commented 3 weeks ago

The current syntax:

var builder = DistributedApplication.CreateBuilder(args);

// Add Data API Builder using dab-config.json 
var dab = builder.AddDataAPIBuilder("dab")
    .WithReference(sqlDatabase)
    .WaitFor(sqlServer);

builder.Build().Run();

This does not make it clear how to include configuration files.

I suggest two important methods:

Method Intent
WithConfigurationFile(string) Mounts the file in the container
WithConfigurationFiles(string[]) Mounts multiple files in the container
var dab = builder.AddDataAPIBuilder("dab")
    .WithConfigurationFile("./dab-config.json") // new
    .WithReference(sqlDatabase)
    .WaitFor(sqlServer);

and

var dab = builder.AddDataAPIBuilder("dab")
    .WithConfigurationFiles("./dab-config.json", "./dab-config-2.json") // new
    .WithReference(sqlDatabase)
    .WaitFor(sqlServer);

This is important because:

  1. Multiple data source support in DAB requires multiple configuration files.
  2. Azure Cosmos DB support requires multiple configuration files (schema files).

Workaround:

I realize this can be done today with Mount() but this undermines the simplicity of the integration.

var dab = builder.AddDataAPIBuilder("dab")
    .WithReference(sqlDatabase)
    .WaitFor(sqlServer);

// configuration
var config = "path";
var internalPath = $"/App/{System.IO.Path.GetFileName(config)}"
dab.WithBindMount(config, internalPath) 

and (multiple configuration files)

var dab = builder.AddDataAPIBuilder("dab")
    .WithReference(sqlDatabase)
    .WaitFor(sqlServer);

// configurations
var configs = new[] { "path", "path", "path" };
foreach (var config in configs)
{
    var internalPath = $"/App/{System.IO.Path.GetFileName(config)}"
    dab.WithBindMount(config, internalPath) 
}
tommasodotNET commented 1 week ago

At the moment config-file can be added manually as a second parameter of AddDataAPIBuilder() has stated in the docs.

Looking at the signature method:

 public static IResourceBuilder<DataApiBuilderContainerResource> AddDataAPIBuilder(this IDistributedApplicationBuilder builder,
        [ResourceName] string name,
        string configFilePath = "./dab-config.json",
        int? port = null)

we simply made the default value as dab-config to make it easier to config DAB when the config file has the standard name dab-config and is placed in the AppHost folder.

Following down this path, though, we can create another signature for AddDataAPIBuilder accepting multiple configFiles:

 public static IResourceBuilder<DataApiBuilderContainerResource> AddDataAPIBuilder(this IDistributedApplicationBuilder builder,
        [ResourceName] string name,
        string[] configFilesPath,
        int? port = null)