Skip to content

Commit 2d12abc

Browse files
maddymontaquilatommasodotNETDamianEdwardscaptainsafiaCopilot
authored
Adding Aspire!!!!!!!!!!!!!!!!!!!!!!!!!! (#2797)
## Why make this change? BECAUSE I AM COOL ## What is this change? Aspirification ## How was this tested? Blood, sweat, and tears ## Sample Request(s) `aspire run` baby --------- Co-authored-by: Tommaso Stocchi <[email protected]> Co-authored-by: Damian Edwards <[email protected]> Co-authored-by: Safia Abdalla <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Aniruddh Munde <[email protected]>
1 parent 7b31e9a commit 2d12abc

18 files changed

+1007
-38
lines changed

.aspire/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"appHostPath": "../src/Aspire.AppHost/Aspire.AppHost.csproj"
3+
}

src/.editorconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ dotnet_style_allow_statement_immediately_after_block_experimental = false:error
7575
#### C# Coding Conventions ####
7676

7777
# var preferences
78-
csharp_style_var_elsewhere = false:error
79-
csharp_style_var_for_built_in_types = false:error
80-
csharp_style_var_when_type_is_apparent = false:error
78+
# csharp_style_var_elsewhere = false:error
79+
# csharp_style_var_for_built_in_types = false:error
80+
# csharp_style_var_when_type_is_apparent = false:error
8181

8282
# Modifier preferences
8383
csharp_prefer_static_local_function = true:suggestion

src/Aspire.AppHost/AppHost.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
var builder = DistributedApplication.CreateBuilder(args);
4+
5+
var aspireDB = Environment.GetEnvironmentVariable("ASPIRE_DATABASE_TYPE");
6+
7+
var databaseConnectionString = Environment.GetEnvironmentVariable("ASPIRE_DATABASE_CONNECTION_STRING") ?? "";
8+
9+
switch (aspireDB)
10+
{
11+
case "mssql":
12+
var sqlScript = File.ReadAllText("./init-scripts/sql/create-database.sql");
13+
14+
IResourceBuilder<SqlServerDatabaseResource>? sqlDbContainer = null;
15+
16+
if (string.IsNullOrEmpty(databaseConnectionString))
17+
{
18+
Console.WriteLine("No connection string provided, starting a local SQL Server container.");
19+
20+
sqlDbContainer = builder.AddSqlServer("sqlserver")
21+
.WithDataVolume()
22+
.WithLifetime(ContainerLifetime.Persistent)
23+
.AddDatabase("msSqlDb", "Trek")
24+
.WithCreationScript(sqlScript);
25+
}
26+
27+
var mssqlService = builder.AddProject<Projects.Azure_DataApiBuilder_Service>("mssql-service", "Development")
28+
.WithArgs("-f", "net8.0")
29+
.WithEndpoint(endpointName: "https", (e) => e.Port = 1234)
30+
.WithEndpoint(endpointName: "http", (e) => e.Port = 2345)
31+
.WithEnvironment("db-type", "mssql")
32+
.WithUrls((e) =>
33+
{
34+
e.Urls.Clear();
35+
e.Urls.Add(new() { Url = "/swagger", DisplayText = "🔒Swagger", Endpoint = e.GetEndpoint("https") });
36+
e.Urls.Add(new() { Url = "/graphql", DisplayText = "🔒GraphQL", Endpoint = e.GetEndpoint("https") });
37+
})
38+
.WithHttpHealthCheck("/health");
39+
40+
if (sqlDbContainer is null)
41+
{
42+
mssqlService.WithEnvironment("ConnectionStrings__Database", databaseConnectionString);
43+
}
44+
else
45+
{
46+
mssqlService.WithEnvironment("ConnectionStrings__Database", sqlDbContainer)
47+
.WaitFor(sqlDbContainer);
48+
}
49+
50+
break;
51+
case "postgresql":
52+
var pgScript = File.ReadAllText("./init-scripts/pg/create-database-pg.sql");
53+
54+
IResourceBuilder<PostgresDatabaseResource>? postgresDB = null;
55+
56+
if (!string.IsNullOrEmpty(databaseConnectionString))
57+
{
58+
Console.WriteLine("No connection string provided, starting a local PostgreSQL container.");
59+
60+
postgresDB = builder.AddPostgres("postgres")
61+
.WithPgAdmin()
62+
.WithLifetime(ContainerLifetime.Persistent)
63+
.AddDatabase("pgDb", "postgres")
64+
.WithCreationScript(pgScript);
65+
}
66+
67+
var pgService = builder.AddProject<Projects.Azure_DataApiBuilder_Service>("pg-service", "Development")
68+
.WithArgs("-f", "net8.0")
69+
.WithEndpoint(endpointName: "https", (e) => e.Port = 1234)
70+
.WithEndpoint(endpointName: "http", (e) => e.Port = 2345)
71+
.WithEnvironment("db-type", "postgresql")
72+
.WithUrls((e) =>
73+
{
74+
e.Urls.Clear();
75+
e.Urls.Add(new() { Url = "/swagger", DisplayText = "🔒Swagger", Endpoint = e.GetEndpoint("https") });
76+
e.Urls.Add(new() { Url = "/graphql", DisplayText = "🔒GraphQL", Endpoint = e.GetEndpoint("https") });
77+
})
78+
.WithHttpHealthCheck("/health");
79+
80+
if (postgresDB is null)
81+
{
82+
pgService.WithEnvironment("ConnectionStrings__Database", databaseConnectionString);
83+
}
84+
else
85+
{
86+
pgService.WithEnvironment("ConnectionStrings__Database", postgresDB)
87+
.WaitFor(postgresDB);
88+
}
89+
90+
break;
91+
default:
92+
throw new Exception("Please set the ASPIRE_DATABASE environment variable to either 'mssql' or 'postgresql'.");
93+
}
94+
95+
builder.Build().Run();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" />
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<TargetFramework>net8.0</TargetFramework>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<NoWarn>NU1603;NU1605</NoWarn>
10+
<Nullable>enable</Nullable>
11+
<UserSecretsId>f08719fd-267f-459e-9980-77b1c52c8755</UserSecretsId>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Aspire.Hosting.AppHost" />
16+
<PackageReference Include="Aspire.Hosting.SqlServer" />
17+
<PackageReference Include="Aspire.Hosting.PostgreSQL" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\Service\Azure.DataApiBuilder.Service.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

src/Aspire.AppHost/DockerStatus.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Diagnostics;
2+
3+
public static class DockerStatus
4+
{
5+
public static async Task<bool> IsDockerRunningAsync()
6+
{
7+
var psi = new ProcessStartInfo
8+
{
9+
FileName = "docker",
10+
Arguments = "info",
11+
RedirectStandardOutput = true,
12+
RedirectStandardError = true,
13+
UseShellExecute = false,
14+
};
15+
try
16+
{
17+
using var process = Process.Start(psi)!;
18+
await process.WaitForExitAsync();
19+
return process.ExitCode == 0;
20+
}
21+
catch
22+
{
23+
return false;
24+
}
25+
}
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"profiles": {
3+
"https": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"environmentVariables": {
7+
"ASPNETCORE_ENVIRONMENT": "Development",
8+
"DOTNET_ENVIRONMENT": "Development",
9+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
10+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145"
11+
},
12+
"dotnetRunMessages": true,
13+
"applicationUrl": "https://localhost:17047;http://localhost:15161"
14+
},
15+
"http": {
16+
"commandName": "Project",
17+
"launchBrowser": true,
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development",
20+
"DOTNET_ENVIRONMENT": "Development",
21+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19015",
22+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20166"
23+
},
24+
"dotnetRunMessages": true,
25+
"applicationUrl": "http://localhost:15161"
26+
},
27+
"aspire-mssql": {
28+
"commandName": "Project",
29+
"launchBrowser": true,
30+
"environmentVariables": {
31+
"ASPNETCORE_ENVIRONMENT": "Development",
32+
"DOTNET_ENVIRONMENT": "Development",
33+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
34+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145",
35+
"ASPIRE_DATABASE_TYPE": "mssql",
36+
"ASPIRE_DATABASE_CONNECTION_STRING": ""
37+
},
38+
"dotnetRunMessages": true,
39+
"applicationUrl": "https://localhost:17047;http://localhost:15161"
40+
},
41+
"aspire-postgresql": {
42+
"commandName": "Project",
43+
"launchBrowser": true,
44+
"environmentVariables": {
45+
"ASPNETCORE_ENVIRONMENT": "Development",
46+
"DOTNET_ENVIRONMENT": "Development",
47+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21213",
48+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22145",
49+
"ASPIRE_DATABASE_TYPE": "postgresql",
50+
"ASPIRE_DATABASE_CONNECTION_STRING": ""
51+
},
52+
"dotnetRunMessages": true,
53+
"applicationUrl": "https://localhost:17047;http://localhost:15161"
54+
}
55+
},
56+
"$schema": "https://json.schemastore.org/launchsettings.json"
57+
}

src/Aspire.AppHost/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Aspire Instructions
2+
3+
This project allows you to run DAB in debug mode using [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/get-started/aspire-overview).
4+
5+
## Prerequisites
6+
- [.NET SDK](https://dotnet.microsoft.com/download) (8.0 or later)
7+
- [Docker](https://www.docker.com/products/docker-desktop) (optional, for containerized development)
8+
9+
## Database Configuration
10+
11+
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.
12+
13+
Simply provide a value for the `ASPIRE_DATABASE_CONNECTION_STRING` environment variable.
14+
15+
You can select to run Aspire with different databases selecting the appropriate launch profile:
16+
- `aspire-sql`
17+
- `aspire-postgres`
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)