Program.cs 5.0 KB

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