diff --git a/Program.cs b/Program.cs index f7d44a3..bef96b7 100644 --- a/Program.cs +++ b/Program.cs @@ -1,21 +1,103 @@ global using System.Diagnostics; +global using System.Text.Json; // Prevent from ending if CTL+C is pressed. Console.TreatControlCAsInput = true; Console.WriteLine($"AyaNova server launcher {AyaNovaVersion.VersionString}"); -//================== LAUNCH AYANOVA SERVER ================== + +//Locate the ayanova executable folder as the basis point for all other paths later +var AyaNovaProgramFolder = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..")); + +//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 ================== +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"); +PGStartInfo.Arguments = "start"; +PGStartInfo.FileName = "pg_ctl.exe"; +PGStartInfo.WorkingDirectory = Path.Combine(AyaNovaProgramFolder, "local-postgres", "bin"); +PGStartInfo.UseShellExecute = false; +var PGProcess = Process.Start(PGStartInfo); + +/* +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 -var ServerExeFolderPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..")); + ProcessStartInfo ServerStartInfo = new ProcessStartInfo(); ServerStartInfo.FileName = "AyaNova.exe"; -ServerStartInfo.WorkingDirectory = ServerExeFolderPath; +ServerStartInfo.WorkingDirectory = AyaNovaProgramFolder; ServerStartInfo.UseShellExecute = true; var ServerProcess = Process.Start(ServerStartInfo); -Console.WriteLine("Waiting for server to exit"); + + + +//================== 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(); +} +