This commit is contained in:
2021-11-30 23:50:06 +00:00
parent 86cf0d86b3
commit 262e758a4b

View File

@@ -1,17 +1,19 @@
global using System.Diagnostics; global using System.Diagnostics;
global using System.Text.Json; global using System.Text.Json;
// Prevent from ending if CTL+C is pressed. // Prevent from ending if CTL+C is pressed.
Console.TreatControlCAsInput = true; Console.TreatControlCAsInput = true;
Console.WriteLine($"AyaNova server launcher {AyaNovaVersion.VersionString}"); Console.WriteLine($"AyaNova server launcher {AyaNovaVersion.VersionString}");
//Locate the ayanova executable folder as the basis point
//Locate the ayanova executable folder as the basis point for all other paths later
var AyaNovaProgramFolder = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..")); var AyaNovaProgramFolder = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), ".."));
#if(DEBUG) #if(DEBUG)
AyaNovaProgramFolder = @"c:\Program Files\ayanova\"; AyaNovaProgramFolder = @"c:\Program Files\ayanova\";
#endif #endif
//read in the config.json file to get it's current values //read in the config.json file to get it's current values
var ConfigFile = Path.Combine(AyaNovaProgramFolder, "config.json"); var ConfigFile = Path.Combine(AyaNovaProgramFolder, "config.json");
if (!File.Exists(ConfigFile)) if (!File.Exists(ConfigFile))
@@ -45,77 +47,80 @@ catch (Exception ex)
return; return;
} }
// Console.WriteLine($"DataPath resolved to: {DataPath}");
// Console.WriteLine($"AyaNovaProgramFolder resolved to: {AyaNovaProgramFolder}");
//================== LAUNCH POSTGRES SERVER ================== //================== LAUNCH POSTGRES SERVER ==================
//https://notepad.onghu.com/2021/portable-postgresql-on-windows-without-installation/ //https://notepad.onghu.com/2021/portable-postgresql-on-windows-without-installation/
//ARGUMENTS DOC: https://www.postgresql.org/docs/11/app-postgres.html
ProcessStartInfo PGStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin","pg_ctl.exe")); var PGProcess = new Process();
ProcessStartInfo PGStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin", "pg_ctl.exe"));
PGStartInfo.EnvironmentVariables["PGDATA"] = Path.Combine(DataPath, "database"); PGStartInfo.EnvironmentVariables["PGDATA"] = Path.Combine(DataPath, "database");
//PGStartInfo.EnvironmentVariables["PGDATABASE"] = "AyaNova";
PGStartInfo.EnvironmentVariables["PGUSER"] = "postgres"; PGStartInfo.EnvironmentVariables["PGUSER"] = "postgres";
PGStartInfo.EnvironmentVariables["PGPORT"] = "5432"; PGStartInfo.EnvironmentVariables["PGPORT"] = "5432";
PGStartInfo.EnvironmentVariables["PGLOCALEDIR"] = Path.Combine(AyaNovaProgramFolder, "local-postgres", "share", "locale"); PGStartInfo.EnvironmentVariables["PGLOCALEDIR"] = Path.Combine(AyaNovaProgramFolder, "local-postgres", "share", "locale");
PGStartInfo.EnvironmentVariables["PGLOGS "] = Path.Combine(DataPath, "logs"); PGStartInfo.EnvironmentVariables["PGLOGS "] = Path.Combine(DataPath, "logs","postgres.log");
//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.Arguments = "start"; PGStartInfo.Arguments = "start";
//PGStartInfo.FileName = "pg_ctl_test.exe";
//PGStartInfo.WorkingDirectory = Path.Combine(DataPath, "database");
PGStartInfo.WorkingDirectory = Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin"); PGStartInfo.WorkingDirectory = Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin");
PGStartInfo.UseShellExecute = false; PGStartInfo.UseShellExecute = false;
//PGStartInfo.RedirectStandardOutput = true;
PGProcess.StartInfo = PGStartInfo;
//PGProcess.OutputDataReceived += new DataReceivedEventHandler(PostgresOutputHandler);
PGProcess.Start();
//Console.WriteLine($"PGStartInfo environment: {string.Join(",",PGStartInfo.EnvironmentVariables.Values.GetEnumerator())}"); //PGProcess.BeginOutputReadLine();
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 ================== //================== 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 var ServerProcess = new Process();
ProcessStartInfo ServerStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "AyaNova.exe")); ProcessStartInfo ServerStartInfo = new ProcessStartInfo(Path.Combine(AyaNovaProgramFolder, "AyaNova.exe"));
//ServerStartInfo.FileName = "AyaNova.exe"; //ServerStartInfo.FileName = "AyaNova.exe";
ServerStartInfo.WorkingDirectory = AyaNovaProgramFolder; ServerStartInfo.WorkingDirectory = AyaNovaProgramFolder;
ServerStartInfo.UseShellExecute = false; ServerStartInfo.UseShellExecute = false;
var ServerProcess = Process.Start(ServerStartInfo); 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 ================ //================== WAIT FOR SHUTDOWN ================
Console.WriteLine("Waiting for server to exit; do not close this window");
if (ServerProcess != null) if (ServerProcess != null)
ServerProcess.WaitForExit(); ServerProcess.WaitForExit();
//shutdown postgres //shutdown postgres
if (PGProcess != null) if (PGProcess != null)
{ {
PGProcess.CloseMainWindow(); Console.WriteLine("Shutting down Postgres database server...");
PGProcess.StandardInput.Close();
// PGProcess.CloseMainWindow();
// PGProcess.Close();
PGProcess.WaitForExit(); PGProcess.WaitForExit();
Console.WriteLine("Postgres shut down, exiting.");
} }
// ####################### OLD SHIT ################################ 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)
// {
// Console.WriteLine($"DB:{outLine.Data}");
// }
// ####################### OLD ################################
//This works but launches three windows //This works but launches three windows