Program.cs 5.5 KB

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