public Link Parents(int id)
 {
     return(new Link("relatedParents",
                     string.Format("~/{0}/{1}/parents/{2}{{?relationType}}", RouteConstants.GetRestRootPath(_version), RouteConstants.RelationsSegment, id)));
 }
示例#2
0
 public Link PagedDescendants(int id)
 {
     return(new Link("descendants",
                     string.Format("~/{0}/{1}/{2}/{3}/descendants{{?pageIndex,pageSize}}", RouteConstants.GetRestRootPath(_version), RouteConstants.ContentSegment, RouteConstants.PublishedSegment, id)));
 }
示例#3
0
 public Link PagedChildren(int id)
 {
     return(new Link("children",
                     string.Format("~/{0}/{1}/{2}/{3}/children{{?pageIndex,pageSize}}", RouteConstants.GetRestRootPath(_version), RouteConstants.ContentSegment, RouteConstants.PublishedSegment, id)));
 }
示例#4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var mysqlConnectionString = _configuration.GetConnectionString("mysql");

            if (string.IsNullOrEmpty(mysqlConnectionString))
            {
                throw new Exception("mysql connection string is required");
            }
            services.AddDbContext <MolliesMoviesContext>(o => o
                                                         .UseMySql(mysqlConnectionString, ServerVersion.AutoDetect(mysqlConnectionString)));

            if (_env.IsEnvironment(MigrationEnvironment))
            {
                services.AddHostedService <MigrationHostedService>();
                return;
            }

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                {
                    var origins = _configuration.GetSection("CorsOrigins").GetChildren()
                                  .Select(x => x.Get <string>())
                                  .ToArray();
                    builder.WithOrigins(origins);
                });
            });

            services.AddSpaStaticFiles(configuration => configuration.RootPath = "wwwroot");

            services.AddControllers(o =>
            {
                o.Filters.Add <ApiExceptionFilter>();
                o.Conventions.Add(new RouteTokenTransformerConvention(
                                      new SlugifyParameterTransformer()));
            })
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                options.JsonSerializerOptions.IgnoreNullValues = true;
            })
            .AddFluentValidation(c =>
            {
                c.RegisterValidatorsFromAssemblyContaining <Startup>();
                c.ImplicitlyValidateChildProperties = true;
            });

            services.AddRouting(o =>
            {
                o.LowercaseUrls = true;
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Mollies Movies", Version = "v1"
                });
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(RouteConstants.PublicApi(1), new OpenApiInfo {
                    Title = "Public Mollies Movies API", Version = "v1"
                });
                c.CustomSchemaIds(t => t.DtoSafeFriendlyId());
                c.CustomOperationIds(ad => ad.GetOperationId());
                c.DocInclusionPredicate((name, api) => api.IsApi(name));
            });

            services.Configure <MovieOptions>(_configuration.GetSection("Movie"));
            services.Configure <ScraperOptions>(_configuration.GetSection("Scraper"));
            services.Configure <TransmissionOptions>(_configuration.GetSection("Transmission"));
            services.PostConfigure <TransmissionOptions>(o => o.RpcUri = GetApiUrl("transmission"));

            services.AddSingleton <ScraperBackgroundService>();
            services.AddSingleton <IHostedService, ScraperBackgroundService>(p => p.GetRequiredService <ScraperBackgroundService>());
            services.AddSingleton <IScraperBackgroundService, ScraperBackgroundService>(p => p.GetRequiredService <ScraperBackgroundService>());

            services.AddTransient <IScraperService, ScraperService>();
            services.AddTransient <IScraperInternalService, ScraperService>();
            services.AddTransient <IScraper, YtsScraper>();
            services.AddTransient <IScraper, PlexScraper>();

            services.AddTransient <IMovieService, MovieService>();

            services.AddTransient <ITransmissionService, TransmissionService>();

            services.AddSingleton <ISystemClock, SystemClock>();
            services.AddAutoMapper(typeof(Startup));

            var ytsUri = GetApiUrl("yts");

            var ytsClientBuilder = services.AddHttpClient <IYtsClient, YtsClient>(c => { c.BaseAddress = ytsUri; });

            var proxyUrl = _configuration.GetConnectionString("proxy");

            if (!string.IsNullOrEmpty(proxyUrl))
            {
                if (!Uri.TryCreate(proxyUrl, UriKind.Absolute, out var uri))
                {
                    throw new Exception("proxy url is invalid");
                }

                ytsClientBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
                {
                    Proxy = new HttpToSocks5Proxy(uri.Host, uri.Port)
                });
            }

            var plexUri = GetApiUrl("plex");

            services.AddHttpClient <IPlexApiClient, PlexApiClient>(c => { c.BaseAddress = plexUri; });
        }