public Startup(IConfiguration configuration)
 {
     PasteDatabase.Init();
     AssemblyLoadContext.Default.Unloading += (context) =>
     {
         PasteDatabase.Shutdown();
     };
     AppDomain.CurrentDomain.ProcessExit += (obj, e) =>
     {
         PasteDatabase.Shutdown();
     };
     Configuration = configuration;
 }
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
 {
     lifetime.ApplicationStopping.Register(() =>
     {
         PasteDatabase.Shutdown();
     });
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     else
     {
         app.UseExceptionHandler("/Error/Any");
     }
     PasteServer.LoadConfig();
     app.Use(async(context, next) =>
     {
         string path = context.Request.Path.Value.ToLowerFast();
         if (path.StartsWith("/view/") && !path.StartsWith("/view/index"))
         {
             context.Items["viewable"] = path[("/view/".Length)..];
             context.Request.Path      = context.Request.Method == "POST" ? "/New/Edit" : "/View/Index";
         }
        public static async void RunConsole()
        {
            while (true)
            {
                string line = await Console.In.ReadLineAsync();

                if (line == null)
                {
                    return;
                }
                string[] split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                switch (split[0])
                {
                case "remove_bot_post":
                {
                    if (split.Length == 2 && long.TryParse(split[1], out long pasteId) && PasteDatabase.TryGetPaste(pasteId, out Paste paste))
                    {
                        paste.Type  = "text";
                        paste.Title = "REMOVED BOT POST";
                        if (string.IsNullOrWhiteSpace(paste.HistoricalContent))
                        {
                            paste.HistoricalContent = paste.Title + "\n\n" + paste.Raw;
                        }
                        paste.Raw       = "Bot post removed from view.";
                        paste.Formatted = HighlighterCore.HighlightPlainText("Bot post removed from view.");
                        PasteDatabase.SubmitPaste(paste);
                        Console.WriteLine($"paste {pasteId} removed");
                    }
                    else
                    {
                        Console.WriteLine("remove_bot_post (ID HERE)");
                    }
                }
                break;

                case "rerender_type":
                {
                    if (split.Length == 2 && PasteType.ValidPasteTypes.TryGetValue(split[1], out PasteType type))
                    {
                        foreach (Paste paste in PasteDatabase.Internal.PasteCollection.Find(p => p.Type == type.Name))
                        {
                            try
                            {
                                Console.WriteLine($"Rerender paste {paste.ID}...");
                                PasteDatabase.FillPaste(paste);
                                string origFormat = paste.Formatted;
                                paste.Formatted = type.Highlight(paste.Raw);
                                if (origFormat.TrimEnd() != paste.Formatted.TrimEnd())
                                {
                                    Console.WriteLine($"Updating paste {paste.ID} (was {origFormat.Length} now {paste.Formatted.Length})...");
                                    PasteDatabase.SubmitPaste(paste);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Failed to rerender paste {paste.ID}: {ex}");
                            }
                        }
                        Console.WriteLine("Rerender done.");
                    }
                    else
                    {
                        Console.WriteLine("rerender_type (TYPE HERE)");
                    }
                }
                break;

                default:
                    Console.WriteLine("Unknown command.");
                    break;
                }
            }
        }