Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddCors(options =>
            {
                options.AddPolicy(MyAllowSpecificOrigins,
                                  builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            });
            services.AddDbContext <CTDbContext>(options =>
                                                options.UseLazyLoadingProxies().UseNpgsql(CTDbContext.ConnectionStringBuilder()));

            //// ===== Add Identity ========
            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequiredLength         = 4;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireLowercase       = false;
            }).AddRoles <IdentityRole>()
            .AddRoleManager <RoleManager <IdentityRole> >()
            .AddEntityFrameworkStores <CTDbContext>()
            .AddDefaultTokenProviders();
            // ===== Add Jwt Authentication ========
            var jwt_issuer = "https://" + Globals.appSettings.HOST_NAME;

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = jwt_issuer,
                    ValidAudience    = jwt_issuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Globals.appSettings.JWT_KEY)),
                    ClockSkew        = TimeSpan.Zero // remove delay of token when expire
                };
            });

            services.AddMvc().AddNewtonsoftJson().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            services.Configure <FormOptions>(x =>
            {
                x.ValueLengthLimit         = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
            });

            // Authorization handlers.
            services.AddScoped <IAuthorizationHandler,
                                ReadOfferingAuthorizationHandler>();
            services.AddScoped <IAuthorizationHandler,
                                UpdateOfferingAuthorizationHandler>();

            // Configure your policies
            services.AddAuthorization(options =>
            {
                options.AddPolicy(Globals.POLICY_UPDATE_OFFERING, policy =>
                                  policy.Requirements.Add(new UpdateOfferingRequirement()));
                options.AddPolicy(Globals.POLICY_READ_OFFERING,
                                  policy => policy.AddRequirements(new ReadOfferingRequirement()));
            });


            // Register the Swagger generator, defining 1 or more Swagger documents
            // new ApiKeyScheme { };
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "ClassTranscribeServer API",
                    Description = "An accessible video platform server. Internal Ref: 0x14cd. See ClassTranscribeServer/Controllers for implementation (https://github.com/classtranscribe/WebAPI/tree/master/ClassTranscribeServer/Controllers)"
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description =
                        "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: Bearer 12345abcde",
                    Name   = "Authorization",
                    In     = ParameterLocation.Header,
                    Type   = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme {
                            Reference = new OpenApiReference {
                                Id   = "Bearer", //The name of the previously defined security scheme.
                                Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });
                c.SchemaFilter <SwaggerSchemaFilter>();
            });
            services.AddApplicationInsightsTelemetry(Globals.appSettings.APPLICATION_INSIGHTS_KEY);
            services.AddScoped <RabbitMQConnection>();
            services.AddScoped <WakeDownloader>();
            services.AddScoped <Seeder>();
            services.AddScoped <UserUtils>();
            services.AddScoped <CaptionQueries>();

            // Configure ElasticSearch client
            if (!string.IsNullOrEmpty(Globals.appSettings.ES_CONNECTION_ADDR))
            {
                var connection = new Uri(Globals.appSettings.ES_CONNECTION_ADDR);
                using var settings = new ConnectionSettings(connection);
                var client = new ElasticClient(settings);
                services.AddSingleton <IElasticClient>(client);
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            var configuration = CTDbContext.GetConfigurations();

            // This project relies on Dependency Injection to configure its various services,
            // For more info, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1
            // All the services used are configured using the service provider.
            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder =>
            {
                builder.AddConsole();
                builder.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                    ("", LogLevel.Warning);
                builder.AddApplicationInsights(configuration.GetValue <string>("APPLICATION_INSIGHTS_KEY"));
            })
                                  .AddOptions()
                                  .Configure <AppSettings>(configuration)
                                  .AddDbContext <CTDbContext>(options => options.UseLazyLoadingProxies().UseNpgsql(CTDbContext.ConnectionStringBuilder()))
                                  .AddSingleton <RabbitMQConnection>()
                                  .AddSingleton <CaptionQueries>()
                                  .AddSingleton <DownloadPlaylistInfoTask>()
                                  .AddSingleton <DownloadMediaTask>()
                                  .AddSingleton <ConvertVideoToWavTask>()
                                  .AddSingleton <TranscriptionTask>()
                                  .AddSingleton <QueueAwakerTask>()
                                  .AddSingleton <GenerateVTTFileTask>()
                                  .AddSingleton <RpcClient>()
                                  .AddSingleton <ProcessVideoTask>()
                                  .AddSingleton <MSTranscriptionService>()
                                  .AddSingleton <SceneDetectionTask>()
                                  .AddSingleton <UpdateBoxTokenTask>()
                                  .AddSingleton <CreateBoxTokenTask>()
                                  .AddSingleton <BuildElasticIndexTask>()
                                  .AddSingleton <ExampleTask>()
                                  .AddSingleton <CleanUpElasticIndexTask>()

                                  .AddSingleton <BoxAPI>()
                                  .AddScoped <Seeder>()
                                  .AddScoped <SlackLogger>()
                                  .AddSingleton <TempCode>()
                                  .BuildServiceProvider();

            _serviceProvider = serviceProvider;
            _logger          = serviceProvider.GetRequiredService <ILogger <Program> >();

            Globals.appSettings           = serviceProvider.GetService <IOptions <AppSettings> >().Value;
            TaskEngineGlobals.KeyProvider = new KeyProvider(Globals.appSettings);

            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler);

            _logger.LogInformation("Seeding database");

            // Seed the database, with some initial data.
            Seeder seeder = serviceProvider.GetService <Seeder>();

            seeder.Seed();

            _logger.LogInformation("Starting TaskEngine");


            // Delete any pre-existing queues on rabbitMQ.
            RabbitMQConnection rabbitMQ = serviceProvider.GetService <RabbitMQConnection>();

            _logger.LogInformation("RabbitMQ - deleting all queues");

            rabbitMQ.DeleteAllQueues();
            //TODO/TOREVIEW: Should we create all of the queues _before_ starting them?
            // In the current version (all old messages deleted) it is ununnecessary
            // But it seems cleaner to me to create them all first before starting them



            ushort concurrent_videotasks     = toUInt16(Globals.appSettings.MAX_CONCURRENT_VIDEO_TASKS, NO_CONCURRENCY);
            ushort concurrent_synctasks      = toUInt16(Globals.appSettings.MAX_CONCURRENT_SYNC_TASKS, MIN_CONCURRENCY);
            ushort concurrent_transcriptions = toUInt16(Globals.appSettings.MAX_CONCURRENT_TRANSCRIPTIONS, MIN_CONCURRENCY);


            // Create and start consuming from all queues.


            // Upstream Sync related
            _logger.LogInformation($"Creating DownloadPlaylistInfoTask & DownloadMediaTask consumers. Concurrency={concurrent_synctasks} ");
            serviceProvider.GetService <DownloadPlaylistInfoTask>().Consume(concurrent_synctasks);
            serviceProvider.GetService <DownloadMediaTask>().Consume(concurrent_synctasks);

            // Transcription Related
            _logger.LogInformation($"Creating TranscriptionTask & GenerateVTTFileTask consumers. Concurrency={concurrent_transcriptions} ");

            serviceProvider.GetService <TranscriptionTask>().Consume(concurrent_transcriptions);
            serviceProvider.GetService <GenerateVTTFileTask>().Consume(concurrent_transcriptions);

            // Video Processing Related
            _logger.LogInformation($"Creating ProcessVideoTask & SceneDetectionTask consumers. Concurrency={concurrent_videotasks} ");
            serviceProvider.GetService <ProcessVideoTask>().Consume(concurrent_videotasks);
            serviceProvider.GetService <SceneDetectionTask>().Consume(concurrent_videotasks);

            // We dont want concurrency for these tasks
            _logger.LogInformation("Creating QueueAwakerTask and Box token tasks consumers.");
            serviceProvider.GetService <QueueAwakerTask>().Consume(NO_CONCURRENCY); //TODO TOREVIEW: NO_CONCURRENCY?
            serviceProvider.GetService <UpdateBoxTokenTask>().Consume(NO_CONCURRENCY);
            serviceProvider.GetService <CreateBoxTokenTask>().Consume(NO_CONCURRENCY);

            // Elastic Search index should be built after TranscriptionTask
            serviceProvider.GetService <BuildElasticIndexTask>().Consume(NO_CONCURRENCY);

            // Outdated Elastic Search index would be removed
            serviceProvider.GetService <CleanUpElasticIndexTask>().Consume(NO_CONCURRENCY);

            serviceProvider.GetService <ExampleTask>().Consume(NO_CONCURRENCY);

            _logger.LogInformation("Done creating task consumers");
            //nolonger used :
            // nope serviceProvider.GetService<nope ConvertVideoToWavTask>().Consume(concurrent_videotasks);

            bool hacktest = false;

            if (hacktest)
            {
                TempCode tempCode = serviceProvider.GetService <TempCode>();
                tempCode.Temp();
                return;
            }
            _logger.LogInformation("All done!");

            QueueAwakerTask queueAwakerTask = serviceProvider.GetService <QueueAwakerTask>();

            int periodicCheck = Math.Max(1, Convert.ToInt32(Globals.appSettings.PERIODIC_CHECK_EVERY_MINUTES));

            _logger.LogInformation("Periodic Check Every {0} minutes", periodicCheck);
            var timeInterval = new TimeSpan(0, periodicCheck, 0);

            // Check for new tasks every "timeInterval".
            // The periodic check will discover all undone tasks
            // TODO/REVIEW: However some tasks also publish the next items
            while (true)
            {
                queueAwakerTask.Publish(new JObject
                {
                    { "Type", TaskType.PeriodicCheck.ToString() }
                });
                Thread.Sleep(timeInterval);
            }
            ;
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var configuration = CTDbContext.GetConfigurations();
            //setup our DI
            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder =>
            {
                builder.AddConsole();
                builder.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                    ("", LogLevel.Warning);
                builder.AddApplicationInsights(configuration.GetValue <string>("APPLICATION_INSIGHTS_KEY"));
            })
                                  .AddOptions()
                                  .Configure <AppSettings>(configuration)
                                  .AddDbContext <CTDbContext>(options => options.UseLazyLoadingProxies().UseNpgsql(CTDbContext.ConnectionStringBuilder()))
                                  .AddScoped <SlackLogger>()
                                  .AddSingleton <MSTranscriptionService>()
                                  .AddSingleton <TempCode>()
                                  .AddSingleton <RpcClient>()
                                  .BuildServiceProvider();

            Globals.appSettings = serviceProvider.GetService <IOptions <AppSettings> >().Value;

            TempCode tempCode = serviceProvider.GetService <TempCode>();

            tempCode.Temp();
        }