using System.Diagnostics; using System.Runtime.InteropServices; using Application.Helpers; using Infrastructure; using Microsoft.EntityFrameworkCore; using MongoDB.Driver; using Newtonsoft.Json.Linq; namespace API; public static class StartupHelper { public static void EnsurePythonEnvironment(string contentRootPath) { contentRootPath = Path.Combine(contentRootPath, ".."); var venvPath = Path.Combine(contentRootPath, "venv"); if (!Directory.Exists(venvPath)) { Console.WriteLine("Setting up Python virtual environment..."); // Create virtual environment ExecuteCommand($"python -m venv \"{venvPath}\""); // Upgrade pip and install requirements var pipExecutable = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Scripts\\pip.exe" : "bin/pip"; var pipPath = Path.Combine(venvPath, pipExecutable); ExecuteCommand($"\"{pipPath}\" install --upgrade pip"); ExecuteCommand($"\"{pipPath}\" install -r \"{Path.Combine(contentRootPath, "requirements.txt")}\""); } else { Console.WriteLine("Python virtual environment already exists."); } } private static void ExecuteCommand(string command) { string fileName, argument; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { fileName = "cmd.exe"; // Wrap the entire command string in quotes to handle spaces argument = "/c \"" + command + "\""; } else { fileName = "/bin/bash"; // Use single quotes around the command for UNIX shells argument = "-c '" + command.Replace("'", "'\\''") + "'"; } var processInfo = new ProcessStartInfo(fileName, argument) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true }; using (var process = Process.Start(processInfo)) { if (process != null) { process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); var error = process.StandardError.ReadToEnd(); Console.WriteLine("Command executed: " + command); if (!string.IsNullOrEmpty(output)) Console.WriteLine("Output: " + output); if (!string.IsNullOrEmpty(error)) Console.WriteLine("Error: " + error); } } } public static void EnsureKeysGenerated(IConfiguration configuration, string contentRootPath) { var apiKey = configuration["ApiKey"]; var jwtSecretKey = configuration["Jwt:SecretKey"]; var appSettingsPath = Path.Combine(contentRootPath, "appsettings.json"); var json = JObject.Parse(File.ReadAllText(appSettingsPath)); var modified = false; if (string.IsNullOrEmpty(apiKey)) { var newApiKey = KeyGenerator.GenerateApiKey(); json["ApiKey"] = newApiKey; modified = true; } if (string.IsNullOrEmpty(jwtSecretKey)) { var newJwtSecretKey = KeyGenerator.GenerateJwtSecretKey(); json["Jwt"]["SecretKey"] = newJwtSecretKey; modified = true; } if (modified) File.WriteAllText(appSettingsPath, json.ToString()); } public static void EnsureMongoDatabaseAndCollectionsExist(IConfiguration configuration) { var mongoConnectionString = configuration["ConnectionStrings:MongoDBConnection"]; var mongoClient = new MongoClient(mongoConnectionString); var databaseName = configuration["HealthcareManagerDatabase:Name"]; var database = mongoClient.GetDatabase(databaseName); var requiredCollections = new List { configuration["HealthcareManagerDatabase:MedicalRecordCollectionName"], configuration["HealthcareManagerDatabase:ChatCollectionName"], configuration["HealthcareManagerDatabase:AppointmentsCollectionName"] }; var existingCollections = database.ListCollectionNames().ToList(); foreach (var collectionName in requiredCollections) if (!existingCollections.Contains(collectionName)) database.CreateCollection(collectionName); } public static void EnsureDatabaseCreated(HealthcareManagerDatabase dbContext) { if (dbContext.Database.GetPendingMigrations().Any()) dbContext.Database.Migrate(); } }