Added DevExpress Dashboard to ASP.NET Core API and both Blazor WASM/Server frontends. Configured dashboard storage, sample data source, and API endpoint. Updated Blazor projects with dashboard packages, styles, and a new dashboard page. Navigation and configuration updated to support dashboard integration.
100 lines
3.3 KiB
C#
100 lines
3.3 KiB
C#
using DbFirst.API.Middleware;
|
|
using DbFirst.Application;
|
|
using DbFirst.Application.Repositories;
|
|
using DbFirst.Domain;
|
|
using DbFirst.Infrastructure;
|
|
using DbFirst.Infrastructure.Repositories;
|
|
using DevExpress.AspNetCore;
|
|
using DevExpress.DashboardAspNetCore;
|
|
using DevExpress.DashboardCommon;
|
|
using DevExpress.DashboardWeb;
|
|
using DevExpress.DataAccess.Json;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// TODO: allow listed origins configured in appsettings.json
|
|
// In any case, dont let them to free to use without cors. if there is no origin specified, block all.
|
|
// In development you can keep it easy.
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
if (builder.Environment.IsDevelopment())
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
}
|
|
else
|
|
{
|
|
var origins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
|
|
if (origins.Length > 0)
|
|
{
|
|
policy.WithOrigins(origins)
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
}
|
|
// if no origins configured, deny all by leaving policy without allowances
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.AddInfrastructure(builder.Configuration);
|
|
builder.Services.AddApplication();
|
|
|
|
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>();
|
|
|
|
builder.Services.AddDevExpressControls();
|
|
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
|
|
var dashboardsPath = Path.Combine(builder.Environment.ContentRootPath, "Data", "Dashboards");
|
|
Directory.CreateDirectory(dashboardsPath);
|
|
|
|
var defaultDashboardPath = Path.Combine(dashboardsPath, "DefaultDashboard.xml");
|
|
if (!File.Exists(defaultDashboardPath))
|
|
{
|
|
var defaultDashboard = new Dashboard();
|
|
defaultDashboard.Title.Text = "Default Dashboard";
|
|
defaultDashboard.SaveToXml(defaultDashboardPath);
|
|
}
|
|
|
|
DashboardConfigurator configurator = new DashboardConfigurator();
|
|
// Register Dashboard Storage
|
|
configurator.SetDashboardStorage(new DashboardFileStorage(dashboardsPath));
|
|
// Create a sample JSON data source
|
|
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
|
|
DashboardJsonDataSource jsonDataSourceUrl = new DashboardJsonDataSource("JSON Data Source (URL)");
|
|
jsonDataSourceUrl.JsonSource = new UriJsonSource(
|
|
new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
|
|
jsonDataSourceUrl.RootElement = "Customers";
|
|
dataSourceStorage.RegisterDataSource("jsonDataSourceUrl", jsonDataSourceUrl.SaveToXml());
|
|
configurator.SetDataSourceStorage(dataSourceStorage);
|
|
return configurator;
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
|
|
app.UseDevExpressControls();
|
|
app.UseHttpsRedirection();
|
|
app.UseCors();
|
|
app.UseAuthorization();
|
|
|
|
app.MapDashboardRoute("api/dashboard", "DefaultDashboard");
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|