Program.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. using MTWorkHR.API.Chat;
  24. using Microsoft.AspNetCore.SignalR;
  25. var builder = WebApplication.CreateBuilder(args);
  26. if (OperatingSystem.IsLinux())
  27. {
  28. builder.Configuration.AddJsonFile("appsettings.Linux.json", optional: true, reloadOnChange: true);
  29. }
  30. var config = new AppSettingsConfiguration();
  31. // Add services to the container.
  32. builder.Services.AddDbContext<HRDataContext>(options =>
  33. {
  34. options.UseSqlServer(config.ConnectionStrings.MTWorkHRConnectionString);
  35. // options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
  36. });
  37. builder.Configuration.Bind(config);
  38. builder.Services.AddApplicationServices(config);
  39. builder.Services.AddInfrastructureIdentityServices(config);
  40. //builder.Services.AddPersistenceServices(builder.Configuration);
  41. //builder.Services.AddIdentityServices(config);
  42. builder.Services.AddHostedService<DbMigrationService>();
  43. if (builder.Environment.IsDevelopment())
  44. {
  45. var mockBlobServiceClient = new Mock<BlobServiceClient>();
  46. // Mock BlobContainerClient since this is what BlobServiceClient interacts with.
  47. var mockBlobContainerClient = new Mock<BlobContainerClient>();
  48. // Set up the mock to return a mock BlobContainerClient when requested.
  49. mockBlobServiceClient
  50. .Setup(x => x.GetBlobContainerClient(It.IsAny<string>()))
  51. .Returns(mockBlobContainerClient.Object);
  52. builder.Services.AddSingleton(mockBlobServiceClient.Object);
  53. }
  54. else
  55. {
  56. // Use the actual connection string for production or other environments.
  57. builder.Services.AddSingleton(x => new BlobServiceClient(config.ConnectionStrings.BlobConnectionString));
  58. }
  59. //builder.Services.AddControllers();
  60. builder.Services.AddControllers(options =>
  61. {
  62. //add filter by instance
  63. options.Filters.Add(new InputValidationActionFilter());
  64. //add filter By the type
  65. options.Filters.Add(typeof(InputValidationActionFilter));
  66. });
  67. //disable default model validation, because we handle this in InputValidationActionFilter and LoggingMiddleware.
  68. builder.Services.Configure<ApiBehaviorOptions>(options =>
  69. {
  70. options.SuppressModelStateInvalidFilter = true;
  71. });
  72. builder.Services.AddDevExpressControls(); // Add DevExpress Reporting controls
  73. //Reporting
  74. builder.Services.AddScoped<ReportStorageWebExtension, CustomReportStorageWebExtension>();
  75. builder.Services.ConfigureReportingServices(configurator => {
  76. configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
  77. viewerConfigurator.UseCachedReportSourceBuilder();
  78. });
  79. });
  80. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  81. builder.Services.AddEndpointsApiExplorer();
  82. //builder.Services.AddSwaggerGen();
  83. //--------------------------
  84. builder.Services.AddSignalR(options =>
  85. {
  86. options.EnableDetailedErrors = true;
  87. });
  88. builder.Services.AddCors(options => {
  89. options.AddPolicy("CORSPolicy", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().SetIsOriginAllowed((hosts) => true));
  90. });
  91. builder.Services.AddSwaggerGen(swagger =>
  92. {
  93. //This is to apply global headers for all requests
  94. swagger.OperationFilter<HeaderOperationFilter>();
  95. //This is to export enums to front
  96. swagger.SchemaFilter<EnumSchemaFilter>();
  97. //This is to generate the Default UI of Swagger Documentation
  98. swagger.SwaggerDoc("v1", new OpenApiInfo
  99. {
  100. Version = "v1",
  101. Title = "MTWorkHR.API",
  102. Description = "MTWorkHR.APIDesc"
  103. });
  104. swagger.CustomOperationIds(
  105. d => (d.ActionDescriptor as ControllerActionDescriptor)?.ControllerName + (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName
  106. );
  107. // To Enable authorization using Swagger (JWT)
  108. swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
  109. {
  110. Name = "Authorization",
  111. Type = SecuritySchemeType.ApiKey,
  112. Scheme = "Bearer",
  113. BearerFormat = "JWT",
  114. In = ParameterLocation.Header,
  115. Description = "Enter 'Bearer' [space] and then your valid token in the text input below.\r\n\r\nExample: \"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"",
  116. });
  117. swagger.AddSecurityRequirement(new OpenApiSecurityRequirement
  118. {
  119. {
  120. new OpenApiSecurityScheme
  121. {
  122. Reference = new OpenApiReference
  123. {
  124. Type = ReferenceType.SecurityScheme,
  125. Id = "Bearer"
  126. }
  127. },
  128. new string[] {}
  129. }
  130. });
  131. });
  132. //--------------------------
  133. var app = builder.Build();
  134. // Configure the HTTP request pipeline.
  135. // if (app.Environment.IsDevelopment())
  136. // {
  137. app.UseDevExpressControls(); // Required for DevExpress Reporting
  138. app.UseSwagger();
  139. //app.UseSwaggerUI();
  140. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MTWorkHR.API v1"));
  141. // }
  142. app.UseRouting(); // <-- Add UseRouting() here
  143. app.UseMiddleware<LoggingMiddleware>();
  144. //app.UseCors(x => x
  145. // .WithOrigins("https://api.mtwork.com", "https://mtworkhrclient.azurewebsites.net", "http://localhost:4200", "https://www.mtwork.com") // Allowed origins without trailing slash
  146. // .AllowAnyMethod()
  147. // .AllowAnyHeader()
  148. // .AllowCredentials() // Allows credentials
  149. //);
  150. app.UseCors("CORSPolicy");
  151. app.UseHttpsRedirection();
  152. app.UseAuthentication();
  153. app.UseAuthorization();
  154. app.UseEndpoints(endpoints =>
  155. {
  156. endpoints.MapHub<ChatHub>("/chatHub"); // Map your SignalR hub
  157. });
  158. app.MapControllers();
  159. //app.MapHub<ChatHub>("/chatHub");
  160. app.Run();