Пример #1
0
        public static IServiceCollection AddDefaultServices(this IServiceCollection services, IConfiguration config)
        {
            string dbConnString = EnviromentHelper.FindGlobalEnviromentVariable("STORAGE_DB")
                                  ?? (!string.IsNullOrWhiteSpace(config.GetConnectionString("StorageFactory"))
                        ? config.GetConnectionString("StorageFactory")
                        : throw new ApplicationException("STORAGE_DB NOT SPECIFIED. USE AN ENVIROMENT VAR."));

            return(services
                   // Services
                   .AddSingleton <EpisodeProcessor>()
                   .AddSingleton <IShowStore, ShowPhysicalStore>()
                   .AddSingleton <IEpisodeStore, EpisodePhysicalStore>()
                   .AddSingleton <StorageService>()
                   .AddSingleton <EpisodeProcessor>()
                   // Config
                   .AddLogging(configure => configure.AddConsole())
                   .AddSingleton(BuildMediaStorageOptions(config))
                   .AddSingleton(BuildBrokerOptions(config))
                   .AddSingleton(BuildMediaInfoClientOptions(config))
                   // Infrastructure
                   .AddSingleton <IMessageBroker, RabbitBroker>(provider => {
                var options = provider.GetRequiredService <BrokerOptions>();
                return new RabbitBroker(options.Address, options.UserName, options.Password, provider.GetRequiredService <ILogger <RabbitBroker> >());
            })
                   .AddSingleton <IMediaInfoClient, MediaInfoClient>()
                   .AddSingleton <IProcessedEpisodeRepository, ProcessedEpisodesRepository>()
                   .AddSingleton <IMemoryCache, MemoryCache>()
                   .AddSingleton <Compiler>(x => new SqlServerCompiler())
                   .AddSingleton <IDbConnectionFactory>(_ => new SqlConnectionFactory(dbConnString))
                   .AddSingleton <HttpClient>());
        }
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration, ILogger?logger = null)
        {
            services
            // Helpers.
            .AddSingleton <ITvDbClient, TvDbClient>()
            .AddSingleton <IMemoryCache, MemoryCache>()
            .AddSingleton <Compiler>(x => new SqlServerCompiler())
            // Repos
            .AddTransient <IShowRepository, ShowRepository>()
            .AddTransient <IEpisodeRepository, EpisodeRepository>()
            // API
            .AddTransient <IApiSearch, TvDbSearch>()
            .AddTransient <IEpisodeApiSearch, TvDbSearch>()
            .AddTransient <IShowApiSearch, TvDbSearch>();

            // Add Config. Prioritize ENV over APPSettings.
            // TVDB Options
            var tvDbOptions = configuration.GetSection("TvDb").Get <TvDbOptions>();

            tvDbOptions.ApiKey = EnviromentHelper.FindGlobalEnviromentVariable("TVDB_APIKEY")
                                 ?? (!string.IsNullOrWhiteSpace(tvDbOptions.ApiKey) ? tvDbOptions.ApiKey : throw new ApplicationException("TVDB_APIKEY NOT SPECIFIED. USE AN ENVIROMENT VAR."));
            // MediaInfo Options
            var mediaInfoConnString = GetMediaInfoDbConnection(configuration);

            logger?.LogInformation($"[SETTINGS]: TVDB_APIKEY - {tvDbOptions.ApiKey}");
            logger?.LogInformation($"[SETTINGS]: MEDIAINFO_DB - {mediaInfoConnString}");

            return(services
                   .AddSingleton <IDbConnectionFactory>(_ => new SqlConnectionFactory(mediaInfoConnString))
                   .AddSingleton(tvDbOptions));
        }
        private static string GetMediaInfoDbConnection(IConfiguration configuration, ILogger?logger = null)
        {
            var connection = EnviromentHelper.FindGlobalEnviromentVariable("MEDIAINFO_DB")
                             ?? (!string.IsNullOrWhiteSpace(configuration.GetConnectionString("MediaInfo")) ? configuration.GetConnectionString("MediaInfo") : throw new ApplicationException("MEDIAINFO_DB NOT SPECIFIED. USE AN ENVIROMENT VAR."));

            // Ensure we can connect..
            using (var db = new SqlConnectionFactory(connection).Create()) {
                int result = db.QueryFirst <int>("SELECT 1");
            }

            return(connection);
        }