Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .aspire/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appHostPath": "../src/Aspire.AppHost/Aspire.AppHost.csproj"
}
6 changes: 3 additions & 3 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ dotnet_style_allow_statement_immediately_after_block_experimental = false:error
#### C# Coding Conventions ####

# var preferences
csharp_style_var_elsewhere = false:error
csharp_style_var_for_built_in_types = false:error
csharp_style_var_when_type_is_apparent = false:error
# csharp_style_var_elsewhere = false:error
# csharp_style_var_for_built_in_types = false:error
# csharp_style_var_when_type_is_apparent = false:error

# Modifier preferences
csharp_prefer_static_local_function = true:suggestion
Expand Down
95 changes: 95 additions & 0 deletions src/Aspire.AppHost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Microsoft.Extensions.DependencyInjection;

var builder = DistributedApplication.CreateBuilder(args);

var aspireDB = Environment.GetEnvironmentVariable("ASPIRE_DATABASE_TYPE");

var databaseConnectionString = Environment.GetEnvironmentVariable("ASPIRE_DATABASE_CONNECTION_STRING") ?? "";

switch (aspireDB)
{
case "sql":
var sqlScript = File.ReadAllText("./init-scripts/sql/create-database.sql");

IResourceBuilder<SqlServerDatabaseResource>? sqlDbContainer = null;

if(string.IsNullOrEmpty(databaseConnectionString))
{
Console.WriteLine("No connection string provided, starting a local SQL Server container.");

sqlDbContainer = builder.AddSqlServer("sqlserver")
.WithDataVolume()
.WithLifetime(ContainerLifetime.Persistent)
.AddDatabase("msSqlDb", "Trek")
.WithCreationScript(sqlScript);
}

var mssqlService = builder.AddProject<Projects.Azure_DataApiBuilder_Service>("mssql-service", "Development")
.WithArgs("-f", "net8.0")
.WithEndpoint(endpointName: "https", (e) => e.Port = 1234)
.WithEndpoint(endpointName: "http", (e) => e.Port = 2345)
.WithEnvironment("db-type", "mssql")
.WithUrls((e) =>
{
e.Urls.Clear();
e.Urls.Add(new() { Url = "/swagger", DisplayText = "🔒Swagger", Endpoint = e.GetEndpoint("https") });
e.Urls.Add(new() { Url = "/graphql", DisplayText = "🔒GraphQL", Endpoint = e.GetEndpoint("https") });
})
.WithHttpHealthCheck("/health");

if (sqlDbContainer is null)
{
mssqlService.WithEnvironment("ConnectionStrings__Database", databaseConnectionString);
}
else
{
mssqlService.WithEnvironment("ConnectionStrings__Database", sqlDbContainer)
.WaitFor(sqlDbContainer);
}

break;
case "postgres":
var pgScript = File.ReadAllText("./init-scripts/pg/create-database-pg.sql");

IResourceBuilder<PostgresDatabaseResource>? postgresDB = null;

if (!string.IsNullOrEmpty(databaseConnectionString))
{
Console.WriteLine("No connection string provided, starting a local PostgreSQL container.");

postgresDB = builder.AddPostgres("postgres")
.WithPgAdmin()
.WithLifetime(ContainerLifetime.Persistent)
.AddDatabase("pgDb", "postgres")
.WithCreationScript(pgScript);
}

var pgService = builder.AddProject<Projects.Azure_DataApiBuilder_Service>("pg-service", "Development")
.WithArgs("-f", "net8.0")
.WithEndpoint(endpointName: "https", (e) => e.Port = 1234)
.WithEndpoint(endpointName: "http", (e) => e.Port = 2345)
.WithEnvironment("db-type", "postgresql")
.WithUrls((e) =>
{
e.Urls.Clear();
e.Urls.Add(new() { Url = "/swagger", DisplayText = "🔒Swagger", Endpoint = e.GetEndpoint("https") });
e.Urls.Add(new() { Url = "/graphql", DisplayText = "🔒GraphQL", Endpoint = e.GetEndpoint("https") });
})
.WithHttpHealthCheck("/health");

if (postgresDB is null)
{
pgService.WithEnvironment("ConnectionStrings__Database", databaseConnectionString);
}
else
{
pgService.WithEnvironment("ConnectionStrings__Database", postgresDB)
.WaitFor(postgresDB);
}

break;
default:
throw new Exception("Please set the ASPIRE_DATABASE environment variable to either 'sql' or 'postgre'.");
}

builder.Build().Run();
23 changes: 23 additions & 0 deletions src/Aspire.AppHost/Aspire.AppHost.csproj
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="9.4.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>f08719fd-267f-459e-9980-77b1c52c8755</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
<PackageReference Include="Aspire.Hosting.SqlServer" />
<PackageReference Include="Aspire.Hosting.PostgreSQL" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Service\Azure.DataApiBuilder.Service.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/Aspire.AppHost/DockerStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics;

public static class DockerStatus
{
public static async Task<bool> IsDockerRunningAsync()
{
var psi = new ProcessStartInfo
{
FileName = "docker",
Arguments = "info",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
try
{
using var process = Process.Start(psi)!;
await process.WaitForExitAsync();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
}
57 changes: 57 additions & 0 deletions src/Aspire.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:17047;http://localhost:15161"
},
"http": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19015",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20166"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:15161"
},
"aspire-sql": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145",
"ASPIRE_DATABASE_TYPE": "sql",
"ASPIRE_DATABASE_CONNECTION_STRING": ""
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:17047;http://localhost:15161"
},
"aspire-postgres": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145",
"ASPIRE_DATABASE_TYPE": "postgres",
"ASPIRE_DATABASE_CONNECTION_STRING": ""
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:17047;http://localhost:15161"
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}
17 changes: 17 additions & 0 deletions src/Aspire.AppHost/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Aspire Instructions

This project allows you to run DAB in debug mode using [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/get-started/aspire-overview).

## Prerequisites
- [.NET SDK](https://dotnet.microsoft.com/download) (8.0 or later)
- [Docker](https://www.docker.com/products/docker-desktop) (optional, for containerized development)

## Database Configuration

In the `launchProfile.json` file, you can configure the database connection string. If you don't, Aspire will start for you a local instance in a Docker container.

Simply provide a value for the `ASPIRE_DATABASE_CONNECTION_STRING` environment variable.

You can select to run Aspire with different databases selecting the appropriate launch profile:
- `aspire-sql`
- `aspire-postgres`
9 changes: 9 additions & 0 deletions src/Aspire.AppHost/appsettings.json
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"
}
}
}
Loading