Пример #1
0
        /// <summary>The method called by the runtime to add services to the container.</summary>
        /// <param name="services">The service injection container.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            // init basic services
            services
            .Configure <ApiClientsConfig>(this.Configuration.GetSection("ApiClients"))
            .Configure <BackgroundServicesConfig>(this.Configuration.GetSection("BackgroundServices"))
            .Configure <ModCompatibilityListConfig>(this.Configuration.GetSection("ModCompatibilityList"))
            .Configure <ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck"))
            .Configure <StorageConfig>(this.Configuration.GetSection("Storage"))
            .Configure <SiteConfig>(this.Configuration.GetSection("Site"))
            .Configure <RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
            .AddLogging()
            .AddMemoryCache();
            StorageConfig storageConfig = this.Configuration.GetSection("Storage").Get <StorageConfig>();
            StorageMode   storageMode   = storageConfig.Mode;

            // init MVC
            services
            .AddControllers()
            .AddNewtonsoftJson(options => this.ConfigureJsonNet(options.SerializerSettings))
            .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider()));
            services
            .AddRazorPages();

            // init storage
            switch (storageMode)
            {
            case StorageMode.InMemory:
                services.AddSingleton <IModCacheRepository>(new ModCacheMemoryRepository());
                services.AddSingleton <IWikiCacheRepository>(new WikiCacheMemoryRepository());
                break;

            case StorageMode.Mongo:
            case StorageMode.MongoInMemory:
            {
                // local MongoDB instance
                services.AddSingleton <MongoDbRunner>(_ => storageMode == StorageMode.MongoInMemory
                            ? MongoDbRunner.Start()
                            : throw new NotSupportedException($"The in-memory MongoDB runner isn't available in storage mode {storageMode}.")
                                                      );

                // MongoDB
                services.AddSingleton <IMongoDatabase>(serv =>
                    {
                        BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
                        return(new MongoClient(this.GetMongoDbConnectionString(serv, storageConfig))
                               .GetDatabase(storageConfig.Database));
                    });

                // repositories
                services.AddSingleton <IModCacheRepository>(serv => new ModCacheMongoRepository(serv.GetRequiredService <IMongoDatabase>()));
                services.AddSingleton <IWikiCacheRepository>(serv => new WikiCacheMongoRepository(serv.GetRequiredService <IMongoDatabase>()));
            }
            break;

            default:
                throw new NotSupportedException($"Unhandled storage mode '{storageMode}'.");
            }

            // init Hangfire
            services
            .AddHangfire((serv, config) =>
            {
                config
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings();

                switch (storageMode)
                {
                case StorageMode.InMemory:
                    config.UseMemoryStorage();
                    break;

                case StorageMode.MongoInMemory:
                case StorageMode.Mongo:
                    string connectionString = this.GetMongoDbConnectionString(serv, storageConfig);
                    config.UseMongoStorage(MongoClientSettings.FromConnectionString(connectionString), $"{storageConfig.Database}-hangfire", new MongoStorageOptions
                    {
                        MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop),
                        CheckConnection  = false        // error on startup takes down entire process
                    });
                    break;
                }
            });

            // init background service
            {
                BackgroundServicesConfig config = this.Configuration.GetSection("BackgroundServices").Get <BackgroundServicesConfig>();
                if (config.Enabled)
                {
                    services.AddHostedService <BackgroundService>();
                }
            }

            // init API clients
            {
                ApiClientsConfig api       = this.Configuration.GetSection("ApiClients").Get <ApiClientsConfig>();
                string           version   = this.GetType().Assembly.GetName().Version.ToString(3);
                string           userAgent = string.Format(api.UserAgent, version);

                services.AddSingleton <IChucklefishClient>(new ChucklefishClient(
                                                               userAgent: userAgent,
                                                               baseUrl: api.ChucklefishBaseUrl,
                                                               modPageUrlFormat: api.ChucklefishModPageUrlFormat
                                                               ));

                services.AddSingleton <ICurseForgeClient>(new CurseForgeClient(
                                                              userAgent: userAgent,
                                                              apiUrl: api.CurseForgeBaseUrl
                                                              ));

                services.AddSingleton <IGitHubClient>(new GitHubClient(
                                                          baseUrl: api.GitHubBaseUrl,
                                                          userAgent: userAgent,
                                                          acceptHeader: api.GitHubAcceptHeader,
                                                          username: api.GitHubUsername,
                                                          password: api.GitHubPassword
                                                          ));

                services.AddSingleton <IModDropClient>(new ModDropClient(
                                                           userAgent: userAgent,
                                                           apiUrl: api.ModDropApiUrl,
                                                           modUrlFormat: api.ModDropModPageUrl
                                                           ));

                services.AddSingleton <INexusClient>(new NexusClient(
                                                         webUserAgent: userAgent,
                                                         webBaseUrl: api.NexusBaseUrl,
                                                         webModUrlFormat: api.NexusModUrlFormat,
                                                         webModScrapeUrlFormat: api.NexusModScrapeUrlFormat,
                                                         apiAppVersion: version,
                                                         apiKey: api.NexusApiKey
                                                         ));

                services.AddSingleton <IPastebinClient>(new PastebinClient(
                                                            baseUrl: api.PastebinBaseUrl,
                                                            userAgent: userAgent
                                                            ));
            }

            // init helpers
            services
            .AddSingleton <IGzipHelper>(new GzipHelper())
            .AddSingleton <IStorageProvider>(serv => new StorageProvider(
                                                 serv.GetRequiredService <IOptions <ApiClientsConfig> >(),
                                                 serv.GetRequiredService <IPastebinClient>(),
                                                 serv.GetRequiredService <IGzipHelper>()
                                                 ));
        }
Пример #2
0
        /// <summary>The method called by the runtime to add services to the container.</summary>
        /// <param name="services">The service injection container.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            // init basic services
            services
            .Configure <ApiClientsConfig>(this.Configuration.GetSection("ApiClients"))
            .Configure <BackgroundServicesConfig>(this.Configuration.GetSection("BackgroundServices"))
            .Configure <ModCompatibilityListConfig>(this.Configuration.GetSection("ModCompatibilityList"))
            .Configure <ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck"))
            .Configure <MongoDbConfig>(this.Configuration.GetSection("MongoDB"))
            .Configure <SiteConfig>(this.Configuration.GetSection("Site"))
            .Configure <RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
            .AddLogging()
            .AddMemoryCache()
            .AddMvc()
            .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider()))
            .AddJsonOptions(options =>
            {
                foreach (JsonConverter converter in new JsonHelper().JsonSettings.Converters)
                {
                    options.SerializerSettings.Converters.Add(converter);
                }

                options.SerializerSettings.Formatting        = Formatting.Indented;
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });
            MongoDbConfig mongoConfig = this.Configuration.GetSection("MongoDB").Get <MongoDbConfig>();

            // init background service
            {
                BackgroundServicesConfig config = this.Configuration.GetSection("BackgroundServices").Get <BackgroundServicesConfig>();
                if (config.Enabled)
                {
                    services.AddHostedService <BackgroundService>();
                }
            }

            // init MongoDB
            services.AddSingleton <MongoDbRunner>(serv => !mongoConfig.IsConfigured()
                ? MongoDbRunner.Start()
                : throw new InvalidOperationException("The MongoDB connection is configured, so the local development version should not be used.")
                                                  );
            services.AddSingleton <IMongoDatabase>(serv =>
            {
                // get connection string
                string connectionString = mongoConfig.IsConfigured()
                    ? mongoConfig.ConnectionString
                    : serv.GetRequiredService <MongoDbRunner>().ConnectionString;

                // get client
                BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
                return(new MongoClient(connectionString).GetDatabase(mongoConfig.Database));
            });
            services.AddSingleton <IModCacheRepository>(serv => new ModCacheRepository(serv.GetRequiredService <IMongoDatabase>()));
            services.AddSingleton <IWikiCacheRepository>(serv => new WikiCacheRepository(serv.GetRequiredService <IMongoDatabase>()));

            // init Hangfire
            services
            .AddHangfire(config =>
            {
                config
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings();

                if (mongoConfig.IsConfigured())
                {
                    config.UseMongoStorage(mongoConfig.ConnectionString, $"{mongoConfig.Database}-hangfire", new MongoStorageOptions
                    {
                        MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop),
                        CheckConnection  = false    // error on startup takes down entire process
                    });
                }
                else
                {
                    config.UseMemoryStorage();
                }
            });

            // init API clients
            {
                ApiClientsConfig api       = this.Configuration.GetSection("ApiClients").Get <ApiClientsConfig>();
                string           version   = this.GetType().Assembly.GetName().Version.ToString(3);
                string           userAgent = string.Format(api.UserAgent, version);

                services.AddSingleton <IChucklefishClient>(new ChucklefishClient(
                                                               userAgent: userAgent,
                                                               baseUrl: api.ChucklefishBaseUrl,
                                                               modPageUrlFormat: api.ChucklefishModPageUrlFormat
                                                               ));
                services.AddSingleton <ICurseForgeClient>(new CurseForgeClient(
                                                              userAgent: userAgent,
                                                              apiUrl: api.CurseForgeBaseUrl
                                                              ));

                services.AddSingleton <IGitHubClient>(new GitHubClient(
                                                          baseUrl: api.GitHubBaseUrl,
                                                          userAgent: userAgent,
                                                          acceptHeader: api.GitHubAcceptHeader,
                                                          username: api.GitHubUsername,
                                                          password: api.GitHubPassword
                                                          ));

                services.AddSingleton <IModDropClient>(new ModDropClient(
                                                           userAgent: userAgent,
                                                           apiUrl: api.ModDropApiUrl,
                                                           modUrlFormat: api.ModDropModPageUrl
                                                           ));

                services.AddSingleton <INexusClient>(new NexusClient(
                                                         webUserAgent: userAgent,
                                                         webBaseUrl: api.NexusBaseUrl,
                                                         webModUrlFormat: api.NexusModUrlFormat,
                                                         webModScrapeUrlFormat: api.NexusModScrapeUrlFormat,
                                                         apiAppVersion: version,
                                                         apiKey: api.NexusApiKey
                                                         ));

                services.AddSingleton <IPastebinClient>(new PastebinClient(
                                                            baseUrl: api.PastebinBaseUrl,
                                                            userAgent: userAgent
                                                            ));
            }

            // init helpers
            services
            .AddSingleton <IGzipHelper>(new GzipHelper())
            .AddSingleton <IStorageProvider>(serv => new StorageProvider(
                                                 serv.GetRequiredService <IOptions <ApiClientsConfig> >(),
                                                 serv.GetRequiredService <IPastebinClient>(),
                                                 serv.GetRequiredService <IGzipHelper>()
                                                 ));
        }
Пример #3
0
        /// <summary>The method called by the runtime to add services to the container.</summary>
        /// <param name="services">The service injection container.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            // init basic services
            services
            .Configure <ApiClientsConfig>(this.Configuration.GetSection("ApiClients"))
            .Configure <BackgroundServicesConfig>(this.Configuration.GetSection("BackgroundServices"))
            .Configure <ModCompatibilityListConfig>(this.Configuration.GetSection("ModCompatibilityList"))
            .Configure <ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck"))
            .Configure <SiteConfig>(this.Configuration.GetSection("Site"))
            .Configure <RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
            .AddLogging()
            .AddMemoryCache();

            // init MVC
            services
            .AddControllers()
            .AddNewtonsoftJson(options => this.ConfigureJsonNet(options.SerializerSettings))
            .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider()));
            services
            .AddRazorPages();

            // init storage
            services.AddSingleton <IModCacheRepository>(new ModCacheMemoryRepository());
            services.AddSingleton <IWikiCacheRepository>(new WikiCacheMemoryRepository());

            // init Hangfire
            services
            .AddHangfire((serv, config) =>
            {
                config
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseMemoryStorage();
            });

            // init background service
            {
                BackgroundServicesConfig config = this.Configuration.GetSection("BackgroundServices").Get <BackgroundServicesConfig>();
                if (config.Enabled)
                {
                    services.AddHostedService <BackgroundService>();
                }
            }

            // init API clients
            {
                ApiClientsConfig api       = this.Configuration.GetSection("ApiClients").Get <ApiClientsConfig>();
                string           version   = this.GetType().Assembly.GetName().Version.ToString(3);
                string           userAgent = string.Format(api.UserAgent, version);

                services.AddSingleton <IChucklefishClient>(new ChucklefishClient(
                                                               userAgent: userAgent,
                                                               baseUrl: api.ChucklefishBaseUrl,
                                                               modPageUrlFormat: api.ChucklefishModPageUrlFormat
                                                               ));

                services.AddSingleton <ICurseForgeClient>(new CurseForgeClient(
                                                              userAgent: userAgent,
                                                              apiUrl: api.CurseForgeBaseUrl
                                                              ));

                services.AddSingleton <IGitHubClient>(new GitHubClient(
                                                          baseUrl: api.GitHubBaseUrl,
                                                          userAgent: userAgent,
                                                          acceptHeader: api.GitHubAcceptHeader,
                                                          username: api.GitHubUsername,
                                                          password: api.GitHubPassword
                                                          ));

                services.AddSingleton <IModDropClient>(new ModDropClient(
                                                           userAgent: userAgent,
                                                           apiUrl: api.ModDropApiUrl,
                                                           modUrlFormat: api.ModDropModPageUrl
                                                           ));

                services.AddSingleton <INexusClient>(new NexusClient(
                                                         webUserAgent: userAgent,
                                                         webBaseUrl: api.NexusBaseUrl,
                                                         webModUrlFormat: api.NexusModUrlFormat,
                                                         webModScrapeUrlFormat: api.NexusModScrapeUrlFormat,
                                                         apiAppVersion: version,
                                                         apiKey: api.NexusApiKey
                                                         ));

                services.AddSingleton <IPastebinClient>(new PastebinClient(
                                                            baseUrl: api.PastebinBaseUrl,
                                                            userAgent: userAgent
                                                            ));
            }

            // init helpers
            services
            .AddSingleton <IGzipHelper>(new GzipHelper())
            .AddSingleton <IStorageProvider>(serv => new StorageProvider(
                                                 serv.GetRequiredService <IOptions <ApiClientsConfig> >(),
                                                 serv.GetRequiredService <IPastebinClient>(),
                                                 serv.GetRequiredService <IGzipHelper>()
                                                 ));
        }