[7146ebb] | 1 | using FinkiChattery.Api.ApplicationServices.Authentication;
|
---|
| 2 | using FinkiChattery.Api.Services;
|
---|
| 3 | using FinkiChattery.Commands.Questioning;
|
---|
[e6a6d9a] | 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;
|
---|
[7146ebb] | 10 | using FinkiChattery.Persistence.Repositories;
|
---|
[95d80e4] | 11 | using FinkiChattery.Persistence.UnitOfWork;
|
---|
[b25b9ea] | 12 | using FinkiChattery.Queries.Questioning;
|
---|
[ad058b3] | 13 | using Hangfire;
|
---|
| 14 | using Hangfire.SqlServer;
|
---|
[e6a6d9a] | 15 | using MediatR;
|
---|
| 16 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
---|
[7146ebb] | 17 | using Microsoft.AspNetCore.Authorization;
|
---|
[e6a6d9a] | 18 | using Microsoft.AspNetCore.Http;
|
---|
| 19 | using Microsoft.AspNetCore.Identity;
|
---|
| 20 | using Microsoft.Extensions.Configuration;
|
---|
| 21 | using Microsoft.Extensions.DependencyInjection;
|
---|
[ad058b3] | 22 | using System;
|
---|
[e6a6d9a] | 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<,>));
|
---|
[b25b9ea] | 33 | services.AddMediatR(typeof(AskQuestionCommand), typeof(GetQuestionStateQuery));
|
---|
[e6a6d9a] | 34 | }
|
---|
| 35 |
|
---|
[ad058b3] | 36 | public static void AddHangfireService(this IServiceCollection services, IConfiguration configuration)
|
---|
| 37 | {
|
---|
[eb21f00] | 38 | string connectionString = "HangfireConnection";
|
---|
| 39 |
|
---|
| 40 | #if DEBUG_DOCKER
|
---|
| 41 | connectionString = "HangfireConnectionDocker";
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
[ad058b3] | 44 | services.AddHangfire(x =>
|
---|
| 45 | {
|
---|
[eb21f00] | 46 | x.UseSqlServerStorage(configuration.GetConnectionString(connectionString), new SqlServerStorageOptions
|
---|
[ad058b3] | 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 |
|
---|
[e6a6d9a] | 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 |
|
---|
[7146ebb] | 86 | services.AddScoped<IAuthorizationHandler, CurrentUserIsOfRoleRequirementHandler>();
|
---|
| 87 |
|
---|
| 88 | services.AddAuthorization(options =>
|
---|
| 89 | {
|
---|
| 90 | options.AddPolicy(AuthenticationPolicy.Student, policy => policy.Requirements.Add(new StudentRequirement()));
|
---|
| 91 |
|
---|
| 92 | });
|
---|
[e6a6d9a] | 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 |
|
---|
[95d80e4] | 110 | public static void AddUnitOfWork(this IServiceCollection services)
|
---|
[e6a6d9a] | 111 | {
|
---|
[95d80e4] | 112 | services.AddScoped<IUnitOfWork, UnitOfWork>();
|
---|
[e6a6d9a] | 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 | }
|
---|