Skip to content

Commit

Permalink
Init aspire
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Jan 18, 2025
1 parent b2076b8 commit 4faf947
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 155 deletions.
9 changes: 9 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,14 @@
<PackageVersion Include="MSTest.TestFramework" Version="3.6.4" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="NUnit" Version="4.2.2" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.MongoDB" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions GrandNode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grand.Module.Api", "src\Mod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grand.Module.ScheduledTasks", "src\Modules\Grand.Module.ScheduledTasks\Grand.Module.ScheduledTasks.csproj", "{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "src\Aspire\Aspire.AppHost\Aspire.AppHost.csproj", "{FFA85947-690E-496B-B21E-DB1800F0DCA9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "src\Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{591950E4-6377-4ECA-A386-8BD3FFE302F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -363,6 +367,14 @@ Global
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Release|Any CPU.Build.0 = Release|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Release|Any CPU.Build.0 = Release|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 23 additions & 0 deletions src/Aspire/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.0.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>bdc1e5b4-4475-44fc-851c-dd576ac2c123</UserSecretsId>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\..\Web\Grand.Web\Grand.Web.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Aspire/Aspire.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

var builder = DistributedApplication.CreateBuilder(args);


var mongo = builder.AddMongoDB("mongo")
.WithLifetime(ContainerLifetime.Persistent);

var mongodb = mongo.AddDatabase("Mongodb");

builder
.AddProject<Projects.Grand_Web>("grand-web")
.WithHttpEndpoint(80)
.WithEnvironment("", "")
.WithReference(mongodb)
.WaitFor(mongodb);

builder.Build().Run();
32 changes: 32 additions & 0 deletions src/Aspire/Aspire.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17196;http://localhost:15129",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21225",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22257",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15129",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19032",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20163",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"

}
}
}
}
8 changes: 8 additions & 0 deletions src/Aspire/Aspire.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/Aspire/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"
}
}
}
22 changes: 22 additions & 0 deletions src/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
89 changes: 89 additions & 0 deletions src/Aspire/Aspire.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}
}
7 changes: 0 additions & 7 deletions src/Core/Grand.Infrastructure/Configuration/DatabaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,4 @@ public class DatabaseConfig
/// Gets or sets a value indicating whether use LiteDB database (only for installation process)
/// </summary>
public string LiteDbConnectionString { get; set; }

/// <summary>
/// Gets or sets a value indicating whether use connection string for database (only for installed databases)
/// </summary>
public string ConnectionString { get; set; }

public int DbProvider { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public class PerformanceConfig
/// </summary>
public bool UseResponseCompression { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ignore DbVersionCheckMiddleware
/// </summary>
public bool IgnoreDbVersionCheckMiddleware { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ignore IgnoreUsePoweredByMiddleware
/// </summary>
Expand Down
15 changes: 11 additions & 4 deletions src/Core/Grand.Infrastructure/StartupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ public static class StartupBase
/// </summary>
private static void InitDatabase(IServiceCollection services, IConfiguration configuration)
{
var dbConfig = services.StartupConfig<DatabaseConfig>(configuration.GetSection("Database"));
if (!string.IsNullOrEmpty(dbConfig.ConnectionString))
var connectionString = configuration["ConnectionStrings:Mongodb"];
var providerString = configuration["ConnectionStrings:Provider"];
var providerInt = 0;
if (!string.IsNullOrEmpty(providerString))
{
_ = int.TryParse(providerString, out providerInt);
}

if (!string.IsNullOrEmpty(connectionString))
DataSettingsManager.Instance.LoadDataSettings(new DataSettings {
ConnectionString = dbConfig.ConnectionString,
DbProvider = (DbProvider)dbConfig.DbProvider
ConnectionString = connectionString,
DbProvider = (DbProvider)providerInt,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Plugins;
using Grand.Module.Installer.Filters;
using Grand.Module.Installer.Interfaces;
using Grand.Module.Installer.Models;
using Grand.SharedKernel.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;

namespace Grand.Module.Installer.Controllers;

[DatabaseConfigured]
public class InstallController : Controller
{

Expand All @@ -25,6 +24,7 @@ public class InstallController : Controller
private readonly ICacheBase _cacheBase;
private readonly IHostApplicationLifetime _applicationLifetime;
private readonly DatabaseConfig _dbConfig;
private readonly IConfiguration _configuration;
private readonly ILogger<InstallController> _logger;

/// <summary>
Expand All @@ -40,11 +40,13 @@ public InstallController(
ICacheBase cacheBase,
IHostApplicationLifetime applicationLifetime,
DatabaseConfig dbConfig,
IConfiguration configuration,
ILogger<InstallController> logger)
{
_cacheBase = cacheBase;
_applicationLifetime = applicationLifetime;
_dbConfig = dbConfig;
_configuration = configuration;
_logger = logger;
}

Expand All @@ -70,9 +72,10 @@ private InstallModel PrepareModel(InstallModel? model)
model ??= new InstallModel {
AdminEmail = "[email protected]",
InstallSampleData = false,
DatabaseConnectionString = "",
AdminPassword = "",
ConfirmPassword = ""
ConfirmPassword = "",
DatabaseConnectionString = _configuration["ConnectionStrings:Mongodb"],
ConnectionInfo = !string.IsNullOrEmpty(_configuration["ConnectionStrings:Mongodb"]),
};

model.AvailableProviders = Enum.GetValues(typeof(DbProvider)).Cast<DbProvider>().Select(v => new SelectListItem {
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions src/Modules/Grand.Module.Installer/Startup/EndpointProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Grand.Infrastructure.Endpoints;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;

namespace Grand.Module.Installer.Startup;

public class EndpointProvider : IEndpointProvider
{
public int Priority => int.MinValue;

public void RegisterEndpoint(IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapControllerRoute("Install", "install", new { controller = "Install", action = "Index" });
}
}
Loading

0 comments on commit 4faf947

Please sign in to comment.