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