Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. using Acresh.Services.DBRepository;
  2. using Acresh.Services.DBRepository.Contracts;
  3. using Acresh.Services.InitialSeed;
  4. using Acresh.Services.JWT;
  5. using Acresh.Services.Services;
  6. using Acresh.Services.Services.Contracts;
  7. using ACRESH_API.Hubs;
  8. using AutoMapper;
  9. using Common.AutomapperConfigurations;
  10. using Common.Tools;
  11. using Infrastructure.Data;
  12. using Infrastructure.Models;
  13. using Microsoft.AspNetCore.Builder;
  14. using Microsoft.AspNetCore.Hosting;
  15. using Microsoft.AspNetCore.Identity;
  16. using Microsoft.EntityFrameworkCore;
  17. using Microsoft.Extensions.Configuration;
  18. using Microsoft.Extensions.DependencyInjection;
  19. using System;
  20. using System.IO;
  21.  
  22. namespace ACRESH_API
  23. {
  24. public class Startup
  25. {
  26. private readonly IWebHostEnvironment env;
  27.  
  28. public Startup(IConfiguration configuration, IWebHostEnvironment env)
  29. {
  30. Configuration = configuration;
  31. this.env = env;
  32. }
  33.  
  34. public IConfiguration Configuration { get; }
  35.  
  36. // This method gets called by the runtime. Use this method to add services to the container.
  37. public void ConfigureServices(IServiceCollection services)
  38. {
  39. services.AddControllers();
  40.  
  41. // services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  42. if (env.EnvironmentName == "Development")
  43. {
  44. services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(Configuration.GetConnectionString("DevelopmentMySQL")));
  45. }
  46. else
  47. {
  48. services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(Configuration.GetConnectionString("ProductionMySQL")));
  49. }
  50.  
  51. services.AddScoped(typeof(IRepository<>), typeof(DbRepository<>));
  52. services.AddIdentity<AcUser, IdentityRole>(opt =>
  53. {
  54. opt.Password.RequireDigit = false;
  55. opt.Password.RequireNonAlphanumeric = false;
  56. opt.Password.RequireLowercase = false;
  57. opt.Password.RequireUppercase = false;
  58. opt.Password.RequiredLength = 4;
  59. opt.Password.RequiredUniqueChars = 2;
  60. opt.Lockout.MaxFailedAccessAttempts = 10;
  61. opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
  62. })
  63. .AddRoles<IdentityRole>()
  64. .AddEntityFrameworkStores<ApplicationDbContext>();
  65.  
  66. services.AddScoped<IUserClaimsPrincipalFactory<AcUser>, UserClaimsPrincipalFactory<AcUser, IdentityRole>>();
  67.  
  68. var mappingConfig = new MapperConfiguration(mc =>
  69. {
  70. mc.AddMaps(new[] { "DataTransferObjects", "Infrastructure.Models" });
  71. mc.AddProfile(new MaProfile());
  72. });
  73.  
  74. var JWTsettingsSection = Configuration.GetSection("JwtSettings");
  75. services.Configure<JWTSettings>(JWTsettingsSection);
  76. var jwtSettings = JWTsettingsSection.Get<JWTSettings>();
  77.  
  78. services.AddSignalR();
  79. services.AddControllers().AddNewtonsoftJson();
  80.  
  81. //Configuring OfJWTHappensHere
  82. ServiceJWT.ConfigureJWTAUth(services, jwtSettings.Secret, jwtSettings.Issuer);
  83. services.AddSingleton<Random>();
  84. IMapper mapper = mappingConfig.CreateMapper();
  85. services.AddSingleton(mapper);
  86. services.AddScoped<DataBaseSeeder>();
  87. services.AddScoped<ServiceJWT>();
  88. services.AddTransient<IUserDataService, UserDataService>();
  89. services.AddTransient<IMessageService, MessageService>();
  90. services.AddTransient<IRecipesService, RecipeService>();
  91. services.AddTransient<ICategoryService, CategoryService>();
  92. services.AddTransient<IIngredientService, IngredientService>();
  93. services.AddTransient<ICommentService, CommentService>();
  94. services.AddResponseCompression(opt => opt.EnableForHttps = true);
  95. }
  96.  
  97. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  98. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  99. {
  100. if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
  101. {
  102. app.UseDeveloperExceptionPage();
  103. }
  104.  
  105. app.Use(async (context, next) =>
  106. {
  107. await next();
  108. if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
  109. {
  110. // context.Response.StatusCode = 200;
  111. context.Request.Path = "/index.html";
  112. await next();
  113. }
  114. });
  115.  
  116. // if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development) app.UseHttpsRedirection();
  117.  
  118. app.UseRouting();
  119.  
  120. if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
  121. {
  122. app.UseCors(builder => builder
  123. .WithOrigins("http://localhost:4200")
  124. .AllowAnyMethod()
  125. .AllowAnyHeader()
  126. .AllowCredentials()
  127. );
  128. }
  129. else
  130. {
  131. app.UseDefaultFiles();
  132. app.UseStaticFiles();
  133. }
  134.  
  135.  
  136. app.UseAuthentication();
  137. app.UseAuthorization();
  138.  
  139. app.UseHsts();
  140. app.UseResponseCompression();
  141.  
  142. app.UseMiddleware<Middlewares.SeederMiddleware>();
  143.  
  144. app.UseEndpoints(endpoints =>
  145. {
  146. endpoints.MapControllers();
  147. endpoints.MapHub<RecievedMessagesHub>("/unread-messages");
  148. endpoints.MapHub<RecipeDetailsTrackHub>("/recipe-details");
  149. });
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement