Program.cs 4.3 KB

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