Files
2025-10-27 17:39:18 -04:00

71 lines
2.5 KiB
C#

using Microsoft.Extensions.Configuration;
var builder = DistributedApplication.CreateBuilder(args);
#region Sql Server
var sqlConfig = builder.Configuration.GetSection("SqlServer");
ArgumentNullException.ThrowIfNull(sqlConfig);
var sqlPassword = builder.AddParameter("prefab-sql-password", secret: true);
var sqlContainerName = sqlConfig["ContainerName"];
ArgumentException.ThrowIfNullOrWhiteSpace(sqlContainerName);
IResourceBuilder<SqlServerServerResource> sqlServer;
var sqlServerHostPort = sqlConfig["HostPort"];
if (!string.IsNullOrWhiteSpace(sqlServerHostPort) && int.TryParse(sqlServerHostPort, out var hostPort))
{
sqlServer = builder.AddSqlServer(sqlContainerName, sqlPassword);
sqlServer.WithEndpoint("tcp", e =>
{
e.Port = hostPort; // host port (what clients connect to)
e.TargetPort = 1433; // container port (SQL Server listens on 1433)
e.IsProxied = false; // disable Aspire's reverse proxy for this endpoint
});
}
else
{
sqlServer = builder.AddSqlServer(sqlContainerName, sqlPassword);
}
var sqlServerDataVolume = sqlConfig.GetValue<bool?>("UseDataVolume");
if (sqlServerDataVolume.GetValueOrDefault(true))
{
sqlServer = sqlServer
.WithDataVolume()
.WithLifetime(ContainerLifetime.Persistent);;
}
var database = sqlServer.AddDatabase("prefab-db");
#endregion
var catalog = builder.AddProject<Projects.Prefab_Catalog_Api>("prefab-catalog")
.WithReference(database)
.WithEnvironment("ConnectionStrings__PrefabDb", database.Resource.ConnectionStringExpression)
.WithEnvironment("ConnectionStrings__PrefabDbReadOnly", database.Resource.ConnectionStringExpression)
.WaitFor(sqlServer);
var catalogClientTransport = builder.Configuration["Prefab__Catalog__Client__Transport"] ?? "InProcess";
var catalogClientBaseAddress = builder.Configuration["Prefab__Catalog__Client__BaseAddress"];
var prefabWeb = builder.AddProject<Projects.Prefab_Web>("prefab-web")
.WithReference(database)
.WithEnvironment("ConnectionStrings__PrefabDb", database.Resource.ConnectionStringExpression)
.WithEnvironment("ConnectionStrings__PrefabDbReadOnly", database.Resource.ConnectionStringExpression)
.WaitFor(sqlServer)
.WithReference(catalog)
.WaitFor(catalog);
prefabWeb.WithEnvironment("Prefab__Catalog__Client__Transport", catalogClientTransport);
if (!string.IsNullOrWhiteSpace(catalogClientBaseAddress))
{
prefabWeb.WithEnvironment("Prefab__Catalog__Client__BaseAddress", catalogClientBaseAddress);
}
builder.Build().Run();