source: src/FinkiChattery/FinkiChattery.Api/Services/RegisterServices.cs@ 846cf1a

dev
Last change on this file since 846cf1a was ad079e5, checked in by Стојков Марко <mst@…>, 3 years ago

Vote answer endpoint

  • Property mode set to 100644
File size: 6.1 KB
Line 
1using FinkiChattery.Api.ApplicationServices.Authentication;
2using FinkiChattery.Api.ApplicationServices.Questioning.EventHandlers;
3using FinkiChattery.Api.Services;
4using FinkiChattery.Commands.Questioning;
5using FinkiChattery.Common.Mediator;
6using FinkiChattery.Common.Mediator.Interfaces;
7using FinkiChattery.Common.User;
8using FinkiChattery.Common.Validation;
9using FinkiChattery.Persistence.Context;
10using FinkiChattery.Persistence.Models;
11using FinkiChattery.Persistence.Repositories;
12using FinkiChattery.Persistence.UnitOfWork;
13using FinkiChattery.Queries.Questioning;
14using Hangfire;
15using Hangfire.SqlServer;
16using MediatR;
17using Microsoft.AspNetCore.Authentication.JwtBearer;
18using Microsoft.AspNetCore.Authorization;
19using Microsoft.AspNetCore.Http;
20using Microsoft.AspNetCore.Identity;
21using Microsoft.Extensions.Configuration;
22using Microsoft.Extensions.DependencyInjection;
23using System;
24
25namespace FinkiChattery.Api.Server
26{
27 public static class RegisterServices
28 {
29 public static void AddMediator(this IServiceCollection services)
30 {
31 services.AddScoped<IMediatorService, MediatorService>();
32 services.AddScoped<IEventService, EventService>();
33 services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
34 services.AddMediatR(typeof(AskQuestionCommand), typeof(GetQuestionStateQuery), typeof(UpdateAnswerVotesEventHandler));
35 }
36
37 public static void AddHangfireService(this IServiceCollection services, IConfiguration configuration)
38 {
39 string connectionString = "HangfireConnection";
40#if DEBUG_DOCKER
41 connectionString = "HangfireConnectionDocker";
42#endif
43
44 services.AddHangfire(x =>
45 {
46 x.UseSqlServerStorage(configuration.GetConnectionString(connectionString), new SqlServerStorageOptions
47 {
48 CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
49 SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
50 QueuePollInterval = TimeSpan.Zero,
51 UseRecommendedIsolationLevel = true,
52 DisableGlobalLocks = true
53 });
54 x.UseMediatR();
55 });
56 services.AddHangfireServer();
57
58 services.AddScoped<IBackgroundJobClient>(provider =>
59 {
60 return new BackgroundJobClient(JobStorage.Current);
61 });
62 }
63
64 public static void AddSingletonServices(this IServiceCollection services)
65 {
66 services.AddSingleton<AppSettings, AppSettings>();
67 }
68
69 public static void AddIdentityService(this IServiceCollection services, IConfiguration configuration)
70 {
71 var appSettings = new AppSettings(configuration);
72
73 services.AddIdentity<ApplicationUser, ApplicationRole>(o =>
74 {
75 o.User.RequireUniqueEmail = true;
76 })
77 .AddEntityFrameworkStores<ApplicationDbContext>()
78 .AddDefaultTokenProviders();
79
80 services.AddAuthentication(options =>
81 {
82 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
83 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
84 }).AddJwtBearer(o =>
85 {
86 o.Authority = appSettings.IdentitySettings.Authority;
87 o.Audience = appSettings.IdentitySettings.Audience;
88 o.RequireHttpsMetadata = appSettings.IdentitySettings.RequireHttpsMetadata;
89 });
90
91 services.AddScoped<IAuthorizationHandler, CurrentUserIsOfRoleRequirementHandler>();
92
93 services.AddAuthorization(options =>
94 {
95 options.AddPolicy(AuthenticationPolicy.Student, policy => policy.Requirements.Add(new StudentRequirement()));
96
97 });
98 }
99
100 public static void AddEmailService(this IServiceCollection services)
101 {
102 // TODO: Add email service
103 // services.AddScoped<IEmailService, EmailService>();
104 }
105
106 public static void AddCurrentUser(this IServiceCollection services)
107 {
108 services.AddScoped(x =>
109 {
110 var httpContext = x.GetRequiredService<IHttpContextAccessor>();
111 return CurrentUser.GetCurrentUser(httpContext);
112 });
113 }
114
115 public static void AddUnitOfWork(this IServiceCollection services)
116 {
117 services.AddScoped<IUnitOfWork, UnitOfWork>();
118 }
119
120 public static void AddOriginUrlSettings(this IServiceCollection services)
121 {
122 // TODO: ADD ORIGIN URLS
123 /*services.AddScoped<IOriginUrlSettings>(provider =>
124 {
125 var httpContextAccessor = provider.GetService<IHttpContextAccessor>();
126
127 string originUrl = string.Empty;
128 if (httpContextAccessor.HttpContext != null && httpContextAccessor.HttpContext.Request.Headers.TryGetValue("Origin", out StringValues headerValues))
129 {
130 if (headerValues.FirstOrDefault() != null)
131 {
132 originUrl = headerValues.FirstOrDefault();
133 }
134 }
135
136 return new OriginUrlSettings(originUrl);
137 });*/
138 }
139
140 public static void AddAwsClient(this IServiceCollection services, IConfiguration configuration)
141 {
142 // ADD AWS FOR MAILS AND S3 AND CDN
143 /* var appSettings = new AppSettings(configuration);
144
145 services.AddAWSService<IAmazonS3>();
146 services.AddSingleton<IAmazonS3>(provider =>
147 {
148 return new AmazonS3Client(
149 appSettings.AwsStorageSettings.AccessKey,
150 appSettings.AwsStorageSettings.SecretKey,
151 Amazon.RegionEndpoint.GetBySystemName(appSettings.AwsStorageSettings.StorageServerRegion));
152 });
153 services.AddScoped<IStorageService, AwsStorageService>();*/
154 }
155 }
156
157}
Note: See TracBrowser for help on using the repository browser.