Esempio n. 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            SchemaService.Initialize();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                //app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseCors(opts => opts.AllowAnyMethod().AllowAnyOrigin().WithHeaders("Content-Type", "Authorization"));
            app.UseMiddleware <TokenAuthService>();

            app.UseRouting();
            app.UseEndpoints(endpoints => endpoints.MapControllers());
        }
Esempio n. 2
0
        private async Task MainAsync()
        {
            ThreadPool.SetMinThreads(32, 32);
            ThreadPool.SetMaxThreads(128, 128);

            Console.WriteLine("Starting PluralKit...");

            InitUtils.Init();

            // Set up a CancellationToken and a SIGINT hook to properly dispose of things when the app is closed
            // The Task.Delay line will throw/exit (forgot which) and the stack and using statements will properly unwind
            var token = new CancellationTokenSource();

            Console.CancelKeyPress += delegate(object e, ConsoleCancelEventArgs args)
            {
                args.Cancel = true;
                token.Cancel();
            };

            var builder = new ContainerBuilder();

            builder.RegisterInstance(_config);
            builder.RegisterModule(new ConfigModule <BotConfig>("Bot"));
            builder.RegisterModule(new LoggingModule("bot"));
            builder.RegisterModule(new MetricsModule());
            builder.RegisterModule <DataStoreModule>();
            builder.RegisterModule <BotModule>();

            using var services = builder.Build();

            var logger = services.Resolve <ILogger>().ForContext <Initialize>();

            try
            {
                SchemaService.Initialize();

                var coreConfig = services.Resolve <CoreConfig>();
                var botConfig  = services.Resolve <BotConfig>();
                var schema     = services.Resolve <SchemaService>();

                using var _ = Sentry.SentrySdk.Init(coreConfig.SentryUrl);

                logger.Information("Connecting to database");
                await schema.ApplyMigrations();

                logger.Information("Connecting to Discord");
                var client = services.Resolve <DiscordShardedClient>();
                await client.LoginAsync(TokenType.Bot, botConfig.Token);

                logger.Information("Initializing bot");
                await client.StartAsync();

                await services.Resolve <Bot>().Init();

                try
                {
                    await Task.Delay(-1, token.Token);
                }
                catch (TaskCanceledException) { } // We'll just exit normally
            }
            catch (Exception e)
            {
                logger.Fatal(e, "Unrecoverable error while initializing bot");
            }

            logger.Information("Shutting down");
        }