From 59317749bcdb3546f3039398a1c2ed394999c120 Mon Sep 17 00:00:00 2001 From: Loretta Date: Sun, 12 Jul 2026 08:36:14 +0200 Subject: [PATCH] Set console title based on DB environment (DEV/PROD) Added SetConsoleTitleFromCatalog to detect the database environment from the connection string and set the console window title to [PROD] or [DEV] accordingly. Throws on ambiguous catalog names and prints a red warning for production. Switched appsettings.json to use the DEV database. Added Microsoft.Data.SqlClient for connection string parsing. This reduces risk of accidental operations on the wrong database. --- .../Infrastructure/PluginNopStartup.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs index d9f67ea..ea55452 100644 --- a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs +++ b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Protocol; +using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -63,6 +64,8 @@ public class PluginNopStartup : INopStartup // Enforce registration of source generated serializers which might not run due to AssemblyLoadContext restrictions Mango.Nop.Core.AcBinaryForcedInit.ForceRegister(); + SetConsoleTitleFromCatalog(configuration); + services.AddScoped(); services.AddTransient(); @@ -286,6 +289,48 @@ public class PluginNopStartup : INopStartup #endif } + /// + /// Console title = [PROD]/[DEV] badge from the connection string's Initial Catalog. + /// Both flags require their own positive evidence; an ambiguous catalog name must fail the startup + /// instead of showing a possibly wrong badge. + /// + private static void SetConsoleTitleFromCatalog(IConfiguration configuration) + { + // Empty during nopCommerce first-run install (no DB configured yet) — no badge, no error. + var connectionString = configuration["ConnectionStrings:ConnectionString"]; + if (string.IsNullOrEmpty(connectionString)) return; + + var catalog = new SqlConnectionStringBuilder(connectionString).InitialCatalog; + + var isDev = catalog.Contains("_DEV", StringComparison.OrdinalIgnoreCase) + || catalog.Contains("_TEST", StringComparison.OrdinalIgnoreCase); + var isProd = catalog.Contains("_PROD", StringComparison.OrdinalIgnoreCase); + + if (isProd == isDev) + throw new InvalidOperationException( + $"Database catalog '{catalog}' cannot be classified: isProd={isProd}, isDev={isDev}. " + + "Fix the catalog naming or the detection rules — refusing to start with an ambiguous environment badge."); + + try + { + if (!OperatingSystem.IsWindows()) return; + + Console.Title = (isProd ? "[PROD]" : "[DEV]") + " FruitBank server"; + + if (isProd) + { + var previousColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"PRODUCTION DATABASE: {catalog}"); + Console.ForegroundColor = previousColor; + } + } + catch + { + // No console (IIS / Windows service) — badge skipped, classification already ran above. + } + } + /// /// Configure the using of added middleware ///