예제 #1
0
        public void ConfigureServices(IServiceCollection services, ILoggerFactory loggerFactory)
        {
            var serializationSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting       = Formatting.None
            };

            JsonConvert.DefaultSettings = () => serializationSettings;

            using var vault = VaultHelper.CreateVaultClient(Configuration, loggerFactory);

            services.AddHttpClient();
            services.AddHealthChecks();

            services.AddHostedService <Host>();
        }
예제 #2
0
        public DirectContractsDbContext CreateDbContext(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
                                .AddJsonFile("appsettings.json",
                                             false,
                                             true)
                                .AddEnvironmentVariables()
                                .Build();

            using var vaultClient = VaultHelper.CreateVaultClient(configuration);
            vaultClient.Login(configuration[configuration["Vault:Token"]]).GetAwaiter().GetResult();
            var connectionString = VaultHelper.GetDbConnectionString(vaultClient, "Database:ConnectionOptions", "Database:ConnectionString", configuration);

            var dbContextOptions = new DbContextOptionsBuilder <DirectContractsDbContext>();

            dbContextOptions.UseNpgsql(connectionString,
                                       builder => builder.UseNetTopologySuite());

            return(new DirectContractsDbContext(dbContextOptions.Options));
        }
예제 #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().SetJsonOptions();

            using var vaultClient = VaultHelper.CreateVaultClient(Configuration);
            vaultClient.Login(Configuration[Configuration["Vault:Token"]]).GetAwaiter().GetResult();
            var dbConnectionString       = VaultHelper.GetDbConnectionString(vaultClient, "DirectContracts:Database:ConnectionOptions", "DirectContracts:Database:ConnectionString", Configuration);
            var redisEndpoint            = Configuration[Configuration["Redis:Endpoint"]];
            var amazonS3DocumentsOptions = VaultHelper.GetOptions(vaultClient, "DirectContracts:AmazonS3:Documents", Configuration);
            var amazonS3ImagesOptions    = VaultHelper.GetOptions(vaultClient, "DirectContracts:AmazonS3:Images", Configuration);
            var mailSenderOptions        = VaultHelper.GetOptions(vaultClient, "DirectContracts:Email", Configuration);
            var bookingWebhookOptions    = VaultHelper.GetOptions(vaultClient, "DirectContracts:BookingWebhookOptions", Configuration);

            services.AddDirectContractsServices(dbConnectionString);
            services.AddDirectManagerServices();
            services.AddAmazonS3Client(options =>
            {
                options.AccessKeyId    = amazonS3DocumentsOptions["accessKeyId"];
                options.AccessKey      = amazonS3DocumentsOptions["accessKey"];
                options.AmazonS3Config = new AmazonS3Config
                {
                    RegionEndpoint = RegionEndpoint.GetBySystemName(amazonS3DocumentsOptions["regionEndpoint"])
                };
            });

            services.AddLocalization();
            services.AddTracing(HostingEnvironment, Configuration);

            services.AddOptions()
            .Configure <RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture("en");
                options.SupportedCultures     = new[]
                {
                    new CultureInfo("ar"),
                    new CultureInfo("en"),
                    new CultureInfo("ru")
                };
                options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider {
                    Options = options
                });
            })
            .Configure <DocumentManagementServiceOptions>(options =>
            {
                options.AmazonS3Bucket = amazonS3DocumentsOptions["bucket"];
            })
            .Configure <ImageManagementServiceOptions>(options =>
            {
                options.AmazonS3Bucket         = amazonS3ImagesOptions["bucket"];
                options.AmazonS3RegionEndpoint = amazonS3ImagesOptions["regionEndpoint"];
            })
            .Configure <SenderOptions>(options =>
            {
                options.ApiKey        = mailSenderOptions["apiKey"];
                options.BaseUrl       = new Uri(mailSenderOptions["publicUrl"]);
                options.SenderAddress = new EmailAddress(mailSenderOptions["senderAddress"]);
            })
            .Configure <NotificationServiceOptions>(options =>
            {
                options.ManagerInvitationMessageTemplateId       = mailSenderOptions["regularManagerInvitationTemplateId"];
                options.ManagerRegistrationMessageTemplateId     = mailSenderOptions["regularManagerRegistrationTemplateId"];
                options.NewMasterManagerWelcomeMessageTemplateId = mailSenderOptions["newMasterManagerWelcomeTemplateId"];
            })
            .Configure <BookingWebhookOptions>(options =>
            {
                options.Key        = bookingWebhookOptions["key"];
                options.WebhookUrl = new Uri(bookingWebhookOptions["webhookUrl"]);
            })
            .Configure <ManagerInvitationOptions>(options =>
            {
                options.InvitationExpirationPeriod = TimeSpan.FromDays(7);
            });

            services.AddHealthChecks()
            .AddCheck <ControllerResolveHealthCheck>(nameof(ControllerResolveHealthCheck))
            .AddDbContextCheck <DirectContractsDbContext>()
            .AddRedis(redisEndpoint);

            services.AddApiVersioning(options =>
            {
                options.AssumeDefaultVersionWhenUnspecified = false;
                options.DefaultApiVersion = new ApiVersion(1, 0);
                options.ReportApiVersions = true;
            });

            services.AddResponseCompression()
            .AddHttpContextAccessor()
            .AddCors()
            .AddLocalization()
            .AddMemoryCache()
            .AddStackExchangeRedisCache(options =>
            {
                options.Configuration = redisEndpoint;
            })
            .AddDoubleFlow()
            .AddCacheFlowJsonSerialization();
            services.AddHttpContextAccessor();
            services.AddMvcCore(options =>
            {
                options.Conventions.Insert(0, new LocalizationConvention());
                options.Conventions.Add(new AuthorizeControllerModelConvention());
                options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipelineFilter)));
                options.Filters.Add(typeof(ModelValidationFilter));
            })
            .AddAuthorization()
            .AddControllersAsServices()
            .AddFormatterMappings()
            .AddApiExplorer()
            .AddFluentValidation()
            .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.ConfigureAuthentication(Configuration, HostingEnvironment, vaultClient)
            .ConfigureHttpClients(Configuration, HostingEnvironment, vaultClient)
            .AddServices();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1.0", new OpenApiInfo {
                    Title = "HappyTravel.com Direct Contracts API", Version = "v1.0"
                });

                var xmlCommentsFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlCommentsFilePath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFileName);
                options.CustomSchemaIds(t => t.FullName);
                options.IncludeXmlComments(xmlCommentsFilePath);
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });
                options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        Array.Empty <string>()
                    }
                });
            });
            services.AddSwaggerGenNewtonsoftSupport();
        }