Esempio n. 1
0
 public static NuGetApiOptions AddSqliteDatabase(
     this NuGetApiOptions options,
     string connectionStringName)
 {
     return(options.AddSqliteDatabase(o =>
                                      o.ConnectionString = options.Configuration.GetConnectionString(connectionStringName)));
 }
        public static NuGetApiOptions AddFeedServices(this NuGetApiOptions options)
        {
            options.Services.AddScoped <IPackageAuthenticationService, PackageAuthenticationService>();
            options.Services.AddScoped <INuGetFeedActionHandler, NuGetFeedActionHandler>();
            options.Services.AddScoped <IEmailService, EmailService>();
            options.Services.AddScoped <ITemplateResourceProvider, LocalTemplateResourceProvider>();
            options.Services.AddScoped <ISyndicationService, SyndicationService>();

            options.Services.AddTransient <ISendGridClient>(x =>
            {
                var options = x.GetRequiredService <EmailSettings>();
                if (string.IsNullOrEmpty(options.SendGridKey))
                {
                    return(new NullSendGridClient());
                }

                return(new SendGridClient(options.SendGridKey));
            });

            options.Services.AddDbContext <FeedContext>(o =>
            {
                o.UseSqlServer(options.Configuration.GetConnectionString("DefaultConnection"));
            });
            return(options);
        }
Esempio n. 3
0
 public static NuGetApiOptions AddMariaDb(
     this NuGetApiOptions options,
     string connectionStringName)
 {
     return(options.AddMariaDb(o =>
                               o.ConnectionString = options.Configuration.GetConnectionString(connectionStringName)));
 }
Esempio n. 4
0
        public static NuGetApiOptions AddAwsS3Storage(this NuGetApiOptions options)
        {
            options.Services.AddNuGetApiOptions <S3StorageOptions>(nameof(PackageFeedOptions.Storage));

            options.Services.AddTransient <S3StorageService>();
            options.Services.TryAddTransient <IStorageService>(provider => provider.GetRequiredService <S3StorageService>());

            options.Services.AddProvider <IStorageService>((provider, config) =>
            {
                if (!config.HasStorageType("AwsS3"))
                {
                    return(null);
                }

                return(provider.GetRequiredService <S3StorageService>());
            });

            options.Services.AddSingleton(provider =>
            {
                var s3Options = provider.GetRequiredService <IOptions <S3StorageOptions> >().Value;

                var config = new AmazonS3Config
                {
                    RegionEndpoint = RegionEndpoint.GetBySystemName(s3Options.Region)
                };

                if (s3Options.UseInstanceProfile)
                {
                    var credentials = FallbackCredentialsFactory.GetCredentials();
                    return(new AmazonS3Client(credentials, config));
                }

                if (!string.IsNullOrEmpty(s3Options.AssumeRoleArn))
                {
                    var credentials        = FallbackCredentialsFactory.GetCredentials();
                    var assumedCredentials = AssumeRoleAsync(
                        credentials,
                        s3Options.AssumeRoleArn,
                        $"NuGetApiApplication-Session-{Guid.NewGuid()}")
                                             .GetAwaiter()
                                             .GetResult();

                    return(new AmazonS3Client(assumedCredentials, config));
                }

                if (!string.IsNullOrEmpty(s3Options.AccessKey))
                {
                    return(new AmazonS3Client(
                               new BasicAWSCredentials(
                                   s3Options.AccessKey,
                                   s3Options.SecretKey),
                               config));
                }

                return(new AmazonS3Client(config));
            });

            return(options);
        }
Esempio n. 5
0
 public static NuGetApiOptions AddFileStorage(
     this NuGetApiOptions options,
     Action<FileSystemStorageOptions> configure)
 {
     options.AddFileStorage();
     options.Services.Configure(configure);
     return options;
 }
Esempio n. 6
0
 public static NuGetApiOptions AddSqliteDatabase(
     this NuGetApiOptions options,
     Action <DatabaseOptions> configure)
 {
     options.AddSqliteDatabase();
     options.Services.Configure(configure);
     return(options);
 }
Esempio n. 7
0
 public static NuGetApiOptions AddMariaDb(
     this NuGetApiOptions options,
     Action <MySqlDatabaseOptions> configure)
 {
     options.AddMariaDb();
     options.Services.Configure(configure);
     return(options);
 }
Esempio n. 8
0
 public static NuGetApiOptions AddAzureBlobStorage(
     this NuGetApiOptions options,
     Action <AzureBlobStorageOptions> configure)
 {
     options.AddAzureBlobStorage();
     options.Services.Configure(configure);
     return(options);
 }
Esempio n. 9
0
        public static NuGetApiOptions AddFeedConfiguration(this NuGetApiOptions options)
        {
            options.Services.Configure <FeedSettings>(options.Configuration.GetSection(nameof(FeedSettings)));
            options.Services.AddTransient(sp => sp.GetRequiredService <IOptionsSnapshot <FeedSettings> >().Value);

            options.Services.Configure <EmailSettings>(options.Configuration.GetSection(nameof(EmailSettings)));
            options.Services.AddTransient(sp => sp.GetRequiredService <IOptionsSnapshot <EmailSettings> >().Value);

            return(options);
        }
Esempio n. 10
0
        public static NuGetApiOptions AddSqliteDatabase(this NuGetApiOptions options)
        {
            options.Services.AddNuGetFeedDbContextProvider <SqliteContext>("Sqlite", (provider, builder) =>
            {
                var databaseOptions = provider.GetRequiredService <IOptionsSnapshot <DatabaseOptions> >();

                builder.UseSqlite(databaseOptions.Value.ConnectionString);
            });

            return(options);
        }
Esempio n. 11
0
        public static NuGetApiOptions AddMariaDb(this NuGetApiOptions options)
        {
            options.Services.AddNuGetApiOptions <MySqlDatabaseOptions>(nameof(PackageFeedOptions.Database));
            options.Services.AddNuGetFeedDbContextProvider <MySqlContext>("MariaDb", (provider, builder) =>
            {
                var databaseOptions = provider.GetRequiredService <IOptionsSnapshot <MySqlDatabaseOptions> >();
                var version         = new MariaDbServerVersion(databaseOptions.Value.Version);
                builder.UseMySql(databaseOptions.Value.ConnectionString, version);
            });

            return(options);
        }
Esempio n. 12
0
        public static NuGetApiOptions AddAzureBlobStorage(this NuGetApiOptions options)
        {
            options.Services.AddNuGetApiOptions <AzureBlobStorageOptions>(nameof(PackageFeedOptions.Storage));
            options.Services.AddTransient <BlobStorageService>();
            options.Services.TryAddTransient <IStorageService>(provider => provider.GetRequiredService <BlobStorageService>());

            options.Services.AddSingleton(provider =>
            {
                var options = provider.GetRequiredService <IOptions <AzureBlobStorageOptions> >().Value;

                if (!string.IsNullOrEmpty(options.ConnectionString))
                {
                    return(new BlobServiceClient(options.ConnectionString));
                }

                var credentials = new StorageSharedKeyCredential(options.AccountName, options.AccessKey);
                return(new BlobServiceClient(new Uri($"https://{options.AccountName.ToLower()}.blob.core.windows.net"), credentials));
            });

            options.Services.AddTransient(provider =>
            {
                var options = provider.GetRequiredService <IOptionsSnapshot <AzureBlobStorageOptions> >().Value;
                var account = provider.GetRequiredService <BlobServiceClient>();
                return(account.GetBlobContainerClient(options.Container));
            });

            options.Services.AddProvider <IStorageService>((provider, config) =>
            {
                if (!config.HasStorageType("AzureBlobStorage"))
                {
                    return(null);
                }

                return(provider.GetRequiredService <BlobStorageService>());
            });

            return(options);
        }
Esempio n. 13
0
 public static NuGetApiOptions AddFileStorage(this NuGetApiOptions options)
 {
     options.Services.TryAddTransient<IStorageService>(provider => provider.GetRequiredService<FileStorageService>());
     return options;
 }
Esempio n. 14
0
 public static NuGetApiOptions AddNullSearch(this NuGetApiOptions options)
 {
     options.Services.TryAddTransient<ISearchIndexer>(provider => provider.GetRequiredService<NullSearchIndexer>());
     options.Services.TryAddTransient<ISearchService>(provider => provider.GetRequiredService<NullSearchService>());
     return options;
 }