refactor: restructure Program.cs and enhance Telemetry response format

This commit is contained in:
2026-06-01 18:16:30 +03:30
parent 97a946ebea
commit 4df537816f
2 changed files with 155 additions and 112 deletions

View File

@@ -2,92 +2,104 @@ using FluentValidation;
using GreenHome.AI.DeepSeek;
using GreenHome.Application;
using GreenHome.Infrastructure;
using GreenHome.Infrastructure.Data;
using GreenHome.Sms.Ippanel;
using GreenHome.VoiceCall.Avanak;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
// ============================================================
// 1. Service Registration
// ============================================================
// Application/Infrastructure DI
builder.Services.AddAutoMapper(typeof(GreenHome.Application.MappingProfile));
builder.Services.AddValidatorsFromAssemblyContaining<GreenHome.Application.DeviceDtoValidator>();
// CORS Configuration
const string CorsPolicy = "DefaultCors";
builder.Services.AddCors(options =>
{
options.AddPolicy(CorsPolicy, policy =>
{
if (builder.Environment.IsDevelopment())
{
// در محیط Development همه origin ها مجاز هستند
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
else
{
// در محیط Production فقط دامنه‌های مشخص
policy
.WithOrigins(
"http://green.nabaksoft.ir",
"https://green.nabaksoft.ir",
"http://gh1.nabaksoft.ir",
"https://gh1.nabaksoft.ir"
)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}
});
});
builder.Services.AddDbContext<GreenHome.Infrastructure.GreenHomeDbContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("Default"));
});
builder.Services.AddScoped<GreenHome.Application.IDeviceService, GreenHome.Infrastructure.DeviceService>();
builder.Services.AddScoped<GreenHome.Application.ITelemetryService, GreenHome.Infrastructure.TelemetryService>();
builder.Services.AddScoped<GreenHome.Application.IDeviceSettingsService, GreenHome.Infrastructure.DeviceSettingsService>();
builder.Services.AddScoped<GreenHome.Application.IAuthService, GreenHome.Infrastructure.AuthService>();
builder.Services.AddScoped<GreenHome.Application.IAlertService, GreenHome.Infrastructure.AlertService>();
builder.Services.AddScoped<GreenHome.Application.IAlertConditionService, GreenHome.Infrastructure.AlertConditionService>();
builder.Services.AddScoped<GreenHome.Application.ISunCalculatorService, GreenHome.Infrastructure.SunCalculatorService>();
builder.Services.AddScoped<GreenHome.Application.IAIQueryService, GreenHome.Infrastructure.AIQueryService>();
builder.Services.AddScoped<GreenHome.Application.IDailyReportService, GreenHome.Infrastructure.DailyReportService>();
builder.Services.AddScoped<GreenHome.Application.IAlertLogService, GreenHome.Infrastructure.AlertLogService>();
builder.Services.AddScoped<GreenHome.Application.IUserDailyReportService, GreenHome.Infrastructure.UserDailyReportService>();
builder.Services.AddScoped<GreenHome.Application.IChecklistService, GreenHome.Infrastructure.ChecklistService>();
builder.Services.AddScoped<GreenHome.Application.IMonthlyReportService, GreenHome.Infrastructure.MonthlyReportService>();
builder.Services.AddScoped<GreenHome.Application.IDevicePostService, GreenHome.Infrastructure.DevicePostService>();
builder.Services.AddScoped<GreenHome.Application.IDeviceTokenService, GreenHome.Infrastructure.DeviceTokenService>();
// SMS Service Configuration
builder.Services.AddIppanelSms(builder.Configuration);
// Voice Call Service Configuration
builder.Services.AddAvanakVoiceCall(builder.Configuration);
// AI Service Configuration
builder.Services.AddDeepSeek(builder.Configuration);
ConfigureCoreServices(builder);
ConfigureCors(builder);
ConfigureExternalServices(builder);
var app = builder.Build();
// Apply pending migrations automatically
using (var scope = app.Services.CreateScope())
// ============================================================
// 2. Pipeline Configuration
// ============================================================
ApplyMigrations(app);
ConfigureMiddleware(app);
ConfigureStaticFiles(app);
app.MapControllers();
app.Run();
// ============================================================
// Service Registration Methods
// ============================================================
void ConfigureCoreServices(WebApplicationBuilder builder)
{
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
builder.Services.AddAutoMapper(typeof(GreenHome.Application.MappingProfile));
builder.Services.AddValidatorsFromAssemblyContaining<GreenHome.Application.DeviceDtoValidator>();
builder.Services.AddInfrastructure(builder.Configuration);
}
void ConfigureCors(WebApplicationBuilder builder)
{
const string CorsPolicy = "DefaultCors";
builder.Services.AddCors(options =>
{
options.AddPolicy(CorsPolicy, policy =>
{
if (builder.Environment.IsDevelopment())
{
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
else
{
policy
.WithOrigins(
"http://green.nabaksoft.ir",
"https://green.nabaksoft.ir",
"http://gh1.nabaksoft.ir",
"https://gh1.nabaksoft.ir"
)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}
});
});
}
void ConfigureExternalServices(WebApplicationBuilder builder)
{
builder.Services.AddIppanelSms(builder.Configuration);
builder.Services.AddAvanakVoiceCall(builder.Configuration);
builder.Services.AddDeepSeek(builder.Configuration);
}
// ============================================================
// Middleware & Pipeline Methods
// ============================================================
void ApplyMigrations(WebApplication app)
{
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<GreenHome.Infrastructure.GreenHomeDbContext>();
var context = services.GetRequiredService<GreenHomeDbContext>();
context.Database.Migrate();
}
catch (Exception ex)
@@ -97,28 +109,30 @@ using (var scope = app.Services.CreateScope())
}
}
// Configure the HTTP request pipeline.
app.MapOpenApi();
app.MapScalarApiReference();
if (!app.Environment.IsDevelopment())
void ConfigureMiddleware(WebApplication app)
{
app.UseHttpsRedirection();
app.MapOpenApi();
app.MapScalarApiReference();
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseCors("DefaultCors");
app.UseAuthorization();
}
app.UseCors(CorsPolicy);
app.UseAuthorization();
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".amr"] = "audio/amr";
app.UseStaticFiles(new StaticFileOptions
void ConfigureStaticFiles(WebApplication app)
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "My_StaticFiles")),
RequestPath = "/My_StaticFiles",
ContentTypeProvider = provider
});
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".amr"] = "audio/amr";
app.MapControllers();
app.Run();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(app.Environment.ContentRootPath, "My_StaticFiles")),
RequestPath = "/My_StaticFiles",
ContentTypeProvider = provider
});
}