Program.cs 6.5 KB

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