Program.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.AddSwaggerGen(swagger =>
  89. {
  90. //This is to apply global headers for all requests
  91. swagger.OperationFilter<HeaderOperationFilter>();
  92. //This is to export enums to front
  93. swagger.SchemaFilter<EnumSchemaFilter>();
  94. //This is to generate the Default UI of Swagger Documentation
  95. swagger.SwaggerDoc("v1", new OpenApiInfo
  96. {
  97. Version = "v1",
  98. Title = "MTWorkHR.API",
  99. Description = "MTWorkHR.APIDesc"
  100. });
  101. swagger.CustomOperationIds(
  102. d => (d.ActionDescriptor as ControllerActionDescriptor)?.ControllerName + (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName
  103. );
  104. // To Enable authorization using Swagger (JWT)
  105. swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
  106. {
  107. Name = "Authorization",
  108. Type = SecuritySchemeType.ApiKey,
  109. Scheme = "Bearer",
  110. BearerFormat = "JWT",
  111. In = ParameterLocation.Header,
  112. Description = "Enter 'Bearer' [space] and then your valid token in the text input below.\r\n\r\nExample: \"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"",
  113. });
  114. swagger.AddSecurityRequirement(new OpenApiSecurityRequirement
  115. {
  116. {
  117. new OpenApiSecurityScheme
  118. {
  119. Reference = new OpenApiReference
  120. {
  121. Type = ReferenceType.SecurityScheme,
  122. Id = "Bearer"
  123. }
  124. },
  125. new string[] {}
  126. }
  127. });
  128. });
  129. //--------------------------
  130. var app = builder.Build();
  131. // Configure the HTTP request pipeline.
  132. // if (app.Environment.IsDevelopment())
  133. // {
  134. app.UseDevExpressControls(); // Required for DevExpress Reporting
  135. app.UseSwagger();
  136. //app.UseSwaggerUI();
  137. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MTWorkHR.API v1"));
  138. // }
  139. app.UseRouting(); // <-- Add UseRouting() here
  140. app.UseCors(x => x
  141. .WithOrigins("https://api.mtwork.com", "https://mtworkhrclient.azurewebsites.net", "http://localhost:4200/") // Allowed origin
  142. .AllowAnyMethod()
  143. .AllowAnyHeader()
  144. .SetIsOriginAllowed(origin =>
  145. true) // allow any origin
  146. .AllowCredentials()); // allow credentials
  147. app.UseHttpsRedirection();
  148. app.UseAuthentication();
  149. app.UseAuthorization();
  150. app.UseEndpoints(endpoints =>
  151. {
  152. endpoints.MapHub<ChatHub>("/chatHub"); // Map your SignalR hub
  153. });
  154. app.UseMiddleware<LoggingMiddleware>();
  155. app.MapControllers();
  156. //app.MapHub<ChatHub>("/chatHub");
  157. app.Run();