Program.cs 4.0 KB

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