Program.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using MTWorkHR.Application;
  2. using MTWorkHR.Infrastructure;
  3. using Microsoft.EntityFrameworkCore;
  4. using MTWorkHR.Core;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Application.Middlewares;
  7. using MTWorkHR.Application.Services.Interfaces;
  8. using MTWorkHR.Application.Filters;
  9. using MTWorkHR.Application.StartupService;
  10. using Microsoft.AspNetCore.Mvc;
  11. using MTWorkHR.Infrastructure.DBContext;
  12. using Microsoft.AspNetCore.Mvc.Controllers;
  13. using Microsoft.OpenApi.Models;
  14. using MTWorkHR.API.Swagger;
  15. using Azure.Storage.Blobs;
  16. using Moq;
  17. using Microsoft.Extensions.DependencyInjection;
  18. using DevExpress.AspNetCore;
  19. using DevExpress.XtraCharts;
  20. using DevExpress.XtraReports.Web.Extensions;
  21. using DevExpress.AspNetCore.Reporting;
  22. using MTWorkHR.Infrastructure.Reports;
  23. var builder = WebApplication.CreateBuilder(args);
  24. if (OperatingSystem.IsLinux())
  25. {
  26. builder.Configuration.AddJsonFile("appsettings.Linux.json", optional: true, reloadOnChange: true);
  27. }
  28. var config = new AppSettingsConfiguration();
  29. // Add services to the container.
  30. builder.Services.AddDbContext<HRDataContext>(options =>
  31. {
  32. options.UseSqlServer(config.ConnectionStrings.LocalConnectionString);
  33. // options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
  34. });
  35. builder.Configuration.Bind(config);
  36. builder.Services.AddApplicationServices(config);
  37. builder.Services.AddInfrastructureIdentityServices(config);
  38. //builder.Services.AddPersistenceServices(builder.Configuration);
  39. //builder.Services.AddIdentityServices(config);
  40. builder.Services.AddHostedService<DbMigrationService>();
  41. if (builder.Environment.IsDevelopment())
  42. {
  43. var mockBlobServiceClient = new Mock<BlobServiceClient>();
  44. // Mock BlobContainerClient since this is what BlobServiceClient interacts with.
  45. var mockBlobContainerClient = new Mock<BlobContainerClient>();
  46. // Set up the mock to return a mock BlobContainerClient when requested.
  47. mockBlobServiceClient
  48. .Setup(x => x.GetBlobContainerClient(It.IsAny<string>()))
  49. .Returns(mockBlobContainerClient.Object);
  50. builder.Services.AddSingleton(mockBlobServiceClient.Object);
  51. }
  52. else
  53. {
  54. // Use the actual connection string for production or other environments.
  55. builder.Services.AddSingleton(x => new BlobServiceClient(config.ConnectionStrings.BlobConnectionString));
  56. }
  57. //builder.Services.AddControllers();
  58. builder.Services.AddControllers(options =>
  59. {
  60. //add filter by instance
  61. options.Filters.Add(new InputValidationActionFilter());
  62. //add filter By the type
  63. options.Filters.Add(typeof(InputValidationActionFilter));
  64. });
  65. //disable default model validation, because we handle this in InputValidationActionFilter and LoggingMiddleware.
  66. builder.Services.Configure<ApiBehaviorOptions>(options =>
  67. {
  68. options.SuppressModelStateInvalidFilter = true;
  69. });
  70. builder.Services.AddDevExpressControls(); // Add DevExpress Reporting controls
  71. //Reporting
  72. builder.Services.AddScoped<ReportStorageWebExtension, CustomReportStorageWebExtension>();
  73. builder.Services.ConfigureReportingServices(configurator => {
  74. configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
  75. viewerConfigurator.UseCachedReportSourceBuilder();
  76. });
  77. });
  78. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  79. builder.Services.AddEndpointsApiExplorer();
  80. //builder.Services.AddSwaggerGen();
  81. //--------------------------
  82. builder.Services.AddSwaggerGen(swagger =>
  83. {
  84. //This is to apply global headers for all requests
  85. swagger.OperationFilter<HeaderOperationFilter>();
  86. //This is to export enums to front
  87. swagger.SchemaFilter<EnumSchemaFilter>();
  88. //This is to generate the Default UI of Swagger Documentation
  89. swagger.SwaggerDoc("v1", new OpenApiInfo
  90. {
  91. Version = "v1",
  92. Title = "MTWorkHR.API",
  93. Description = "MTWorkHR.APIDesc"
  94. });
  95. swagger.CustomOperationIds(
  96. d => (d.ActionDescriptor as ControllerActionDescriptor)?.ControllerName + (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName
  97. );
  98. // To Enable authorization using Swagger (JWT)
  99. swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
  100. {
  101. Name = "Authorization",
  102. Type = SecuritySchemeType.ApiKey,
  103. Scheme = "Bearer",
  104. BearerFormat = "JWT",
  105. In = ParameterLocation.Header,
  106. Description = "Enter 'Bearer' [space] and then your valid token in the text input below.\r\n\r\nExample: \"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"",
  107. });
  108. swagger.AddSecurityRequirement(new OpenApiSecurityRequirement
  109. {
  110. {
  111. new OpenApiSecurityScheme
  112. {
  113. Reference = new OpenApiReference
  114. {
  115. Type = ReferenceType.SecurityScheme,
  116. Id = "Bearer"
  117. }
  118. },
  119. new string[] {}
  120. }
  121. });
  122. });
  123. //--------------------------
  124. var app = builder.Build();
  125. // Configure the HTTP request pipeline.
  126. // if (app.Environment.IsDevelopment())
  127. // {
  128. app.UseDevExpressControls(); // Required for DevExpress Reporting
  129. app.UseSwagger();
  130. //app.UseSwaggerUI();
  131. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MTWorkHR.API v1"));
  132. // }
  133. app.UseCors(x => x
  134. .AllowAnyMethod()
  135. .AllowAnyHeader()
  136. .SetIsOriginAllowed(origin =>
  137. true) // allow any origin
  138. .AllowCredentials()); // allow credentials
  139. app.UseHttpsRedirection();
  140. app.UseAuthentication();
  141. app.UseAuthorization();
  142. app.UseMiddleware<LoggingMiddleware>();
  143. app.MapControllers();
  144. app.Run();