Program.cs 4.7 KB

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