|
| 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(); |
0 commit comments