global using System.Diagnostics; global using System.Text.Json; //checkpoint commit comment for case 4586 prior to backend dependency update // Prevent from ending if CTL+C is pressed. Console.TreatControlCAsInput = true; Console.WriteLine($"AyaNova server launcher {AyaNovaVersion.VersionString}"); //Locate the ayanova executable folder as the basis point var AyaNovaProgramFolder = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..")); #if(DEBUG) AyaNovaProgramFolder = @"c:\Program Files\ayanova\"; #endif //read in the config.json file to get it's current values var ConfigFile = Path.Combine(AyaNovaProgramFolder, "config.json"); if (!File.Exists(ConfigFile)) { Console.WriteLine($"FAIL: Unable to find expected configuration file: '{ConfigFile}'"); return; } string ConfigText = File.ReadAllText(ConfigFile); if (string.IsNullOrWhiteSpace(ConfigText)) { Console.WriteLine($"FAIL: empty configuration file: '{ConfigFile}'"); return; } string? DataPath = null; try { var ConfigJSON = JsonDocument.Parse(ConfigText); var element = ConfigJSON.RootElement.GetProperty("AYANOVA_DATA_PATH"); DataPath = element.GetString(); if (DataPath == null || string.IsNullOrWhiteSpace(DataPath)) { throw new System.ArgumentNullException("AYANOVA_DATA_PATH configuration property is empty or missing but required to locate database"); } DataPath = System.Environment.ExpandEnvironmentVariables(DataPath);//%ProgramData% expansion } catch (Exception ex) { Console.WriteLine($"FAIL: error getting database location from configuration file: '{ConfigFile}'\nError was {ex.Message}"); return; } //================== LAUNCH POSTGRES SERVER ================== //https://notepad.onghu.com/2021/portable-postgresql-on-windows-without-installation/ //ARGUMENTS DOC: https://www.postgresql.org/docs/11/app-postgres.html var PGProcess = new Process(); ProcessStartInfo PGStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin", "pg_ctl.exe")); //NOTE: have to set log here in arguments, doesn't work as an environment variable, perhaps none of them work when starting this way //so if anything is missing it probably needs to be added as a argument here //This is all because otherwise it logs to launcher console and messes up display as mixed in with ayanova server notices and also has misleading errors that don't apply //like missing ayanova database which is about to be created anyway //Name PID Status User name CPU Memory (active private working set) Command line UAC virtualization //postgres.exe 10676 Running cardj 00 1,532 K "C:/Program Files/ayanova/local-postgres/bin/postgres.exe" -D "C:/ProgramData/ayanova/database" -p 5432 Disabled //"C:/Program Files/ayanova/local-postgres/bin/postgres.exe" -D "C:/ProgramData/ayanova/database" -p 5432 PGStartInfo.Arguments = $"-D {Path.Combine(DataPath, "database")} -o \"-p 5432\" -l {Path.Combine(DataPath, "logs","postgres.log")} start"; PGStartInfo.WorkingDirectory = Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin"); PGStartInfo.UseShellExecute = false; PGProcess.StartInfo = PGStartInfo; PGProcess.Start(); //================== LAUNCH AYANOVA SERVER ================== var ServerProcess = new Process(); ProcessStartInfo ServerStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "AyaNova.exe")); //ServerStartInfo.FileName = "AyaNova.exe"; ServerStartInfo.WorkingDirectory = AyaNovaProgramFolder; ServerStartInfo.UseShellExecute = false; ServerStartInfo.RedirectStandardOutput = true; //ServerStartInfo.RedirectStandardError=true; ServerProcess.StartInfo = ServerStartInfo; ServerProcess.OutputDataReceived += new DataReceivedEventHandler(AyaNovaOutputHandler); //process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); ServerProcess.Start(); ServerProcess.BeginOutputReadLine(); //================== WAIT FOR SHUTDOWN ================ if (ServerProcess != null) ServerProcess.WaitForExit(); //shutdown postgres if (PGProcess != null) { Console.WriteLine("Shutting down Postgres database server..."); PGProcess.StandardInput.Close(); // PGProcess.CloseMainWindow(); // PGProcess.Close(); PGProcess.WaitForExit(); Console.WriteLine("Postgres shut down, exiting."); } static void AyaNovaOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { //* Do your stuff with the output (write to console/log/StringBuilder) Console.WriteLine($"{outLine.Data}"); if (outLine.Data != null && outLine.Data.Contains("COMPLETED - SERVER OPEN")) { Process.Start(new ProcessStartInfo(){FileName="http://localhost:7575/api/v8/",UseShellExecute=true}); Process.Start(new ProcessStartInfo(){FileName="http://localhost:7575",UseShellExecute=true}); } // } // static void PostgresOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) // { // //File.AppendAllLines(outLine.Data); // Console.WriteLine($"DB:{outLine.Data}"); // } // ####################### OLD ################################ //some env variables that are not working out with single window start, had to move them to arguments of process instead // PGStartInfo.EnvironmentVariables["PGDATA"] = Path.Combine(DataPath, "database"); // PGStartInfo.EnvironmentVariables["PGUSER"] = "postgres"; // PGStartInfo.EnvironmentVariables["PGPORT"] = "5432"; // PGStartInfo.EnvironmentVariables["PGLOCALEDIR"] = Path.Combine(AyaNovaProgramFolder, "local-postgres", "share", "locale"); // PGStartInfo.EnvironmentVariables["PGLOGS"] = Path.Combine(DataPath, "logs","postgres.log"); // Console.WriteLine($"PG LOGS: {Path.Combine(DataPath, "logs","postgres.log")}"); //PGStartInfo.Arguments = "start"; //This works but launches three windows /* //================== LAUNCH POSTGRES SERVER ================== //https://notepad.onghu.com/2021/portable-postgresql-on-windows-without-installation/ ProcessStartInfo PGStartInfo = new ProcessStartInfo(); //PGStartInfo.EnvironmentVariables["PGDATA"] = Path.Combine(DataPath, "database"); ////PGStartInfo.EnvironmentVariables["PGDATABASE"] = "AyaNova"; //PGStartInfo.EnvironmentVariables["PGUSER"] = "postgres"; //PGStartInfo.EnvironmentVariables["PGPORT"] = "5432"; //PGStartInfo.EnvironmentVariables["PGLOCALEDIR"] = Path.Combine(AyaNovaProgramFolder, "local-postgres", "share", "locale"); //PGStartInfo.EnvironmentVariables["PGLOGS "] = Path.Combine(DataPath, "logs"); //ARGUMENTS DOC: https://www.postgresql.org/docs/11/app-postgres.html PGStartInfo.Arguments = $"-D {Path.Combine(DataPath, "database")} -o \"-p 5432\" -l {Path.Combine(DataPath, "logs","postgres.log")} start"; PGStartInfo.FileName = "pg_ctl.exe"; //PGStartInfo.WorkingDirectory = Path.Combine(DataPath, "database"); PGStartInfo.WorkingDirectory = Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin"); PGStartInfo.UseShellExecute = true; var PGProcess = Process.Start(PGStartInfo); // "-D C:\ProgramData\ayanova\database -p 5432 -l C:\ProgramData\ayanova\logs start" // old batch file settings required for postgres // SET PATH="%~dp0postgres\bin";%PATH% // SET PGDATA=%~dp0data\database // SET PGDATABASE=AyaNova // SET PGUSER=postgres // SET PGPORT=5432 // SET PGLOCALEDIR=%~dp0postgres\share\locale // set AYANOVA_USE_URLS=http://*:7575; // set AYANOVA_DB_CONNECTION=Server=localhost;Username=postgres;Password=mypasswordforpostgres;Database=AyaNova; // set AYANOVA_DEFAULT_TRANSLATION=en // set AYANOVA_BACKUP_PG_DUMP_PATH=%~dp0postgres\bin\ // set AYANOVA_DATA_PATH=%~dp0data // "%~dp0\postgres\bin\pg_ctl" -D "%~dp0/data/database" -l %~dp0data\logs\postgreslog start //================== LAUNCH AYANOVA SERVER ================== //Go UP one folder from the current launcher folder which should *always* be contained within the AyaNova.exe containing folder if it was installed ProcessStartInfo ServerStartInfo = new ProcessStartInfo(); ServerStartInfo.FileName = "AyaNova.exe"; ServerStartInfo.WorkingDirectory = AyaNovaProgramFolder; ServerStartInfo.UseShellExecute = true; var ServerProcess = Process.Start(ServerStartInfo); //================== WAIT FOR SHUTDOWN ================ Console.WriteLine("Waiting for server to exit; do not close this window"); if (ServerProcess != null) ServerProcess.WaitForExit(); //shutdown postgres if (PGProcess != null) { PGProcess.CloseMainWindow(); PGProcess.WaitForExit(); } */ //############################################################################ //this was original ctr.-c shutdown from one window concept // Console.WriteLine("#############################################"); // Console.WriteLine("Press CTL-C key to shut down AyaNova Server"); // Console.WriteLine("#############################################"); // bool Quit = false; // ConsoleKeyInfo cki; // do // { // cki = Console.ReadKey(false); // if (cki.Key == ConsoleKey.C && (cki.Modifiers & ConsoleModifiers.Control) != 0) // { // Quit = true; // } // else // { // Console.WriteLine("Press CTL-C key to shut down AyaNova Server"); // } // //Console.WriteLine(cki.Key.ToString()); // } while (!Quit); // if (ServerProcess != null) // { // ServerProcess.CloseMainWindow(); // ServerProcess.Close(); // }