Program.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.LocalConnectionString);
  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.AddSwaggerGen(swagger =>
  85. {
  86. //This is to apply global headers for all requests
  87. swagger.OperationFilter<HeaderOperationFilter>();
  88. //This is to export enums to front
  89. swagger.SchemaFilter<EnumSchemaFilter>();
  90. //This is to generate the Default UI of Swagger Documentation
  91. swagger.SwaggerDoc("v1", new OpenApiInfo
  92. {
  93. Version = "v1",
  94. Title = "MTWorkHR.API",
  95. Description = "MTWorkHR.APIDesc"
  96. });
  97. swagger.CustomOperationIds(
  98. d => (d.ActionDescriptor as ControllerActionDescriptor)?.ControllerName + (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName
  99. );
  100. // To Enable authorization using Swagger (JWT)
  101. swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
  102. {
  103. Name = "Authorization",
  104. Type = SecuritySchemeType.ApiKey,
  105. Scheme = "Bearer",
  106. BearerFormat = "JWT",
  107. In = ParameterLocation.Header,
  108. Description = "Enter 'Bearer' [space] and then your valid token in the text input below.\r\n\r\nExample: \"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"",
  109. });
  110. swagger.AddSecurityRequirement(new OpenApiSecurityRequirement
  111. {
  112. {
  113. new OpenApiSecurityScheme
  114. {
  115. Reference = new OpenApiReference
  116. {
  117. Type = ReferenceType.SecurityScheme,
  118. Id = "Bearer"
  119. }
  120. },
  121. new string[] {}
  122. }
  123. });
  124. });
  125. builder.Services.AddSignalR(options =>
  126. {
  127. options.EnableDetailedErrors = true;
  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.UseCors(x => x
  140. .AllowAnyMethod()
  141. .AllowAnyHeader()
  142. .SetIsOriginAllowed(origin =>
  143. true) // allow any origin
  144. .AllowCredentials()); // allow credentials
  145. //app.MapPost("broadcast", async (string message, IHubContext<ChatHub, IChatClient> context) =>
  146. //{
  147. // await context.Clients.All.ReceiveMessage(message);
  148. // return Results.NoContent();
  149. //});
  150. //app.UseEndpoints(endpoints =>
  151. //{
  152. // endpoints.MapHub<ChatHub>("/chatHub");
  153. //});
  154. app.UseHttpsRedirection();
  155. app.UseAuthentication();
  156. app.UseAuthorization();
  157. app.UseMiddleware<LoggingMiddleware>();
  158. app.MapControllers();
  159. app.MapHub<ChatHub>("/chatHub");
  160. app.Run();