mirror of
https://github.com/islehorse/HISP.git
synced 2025-04-20 19:59:15 +12:00
Add ability to run as a linux service
This commit is contained in:
parent
fb8eab0f8e
commit
1735c35a7c
30 changed files with 935 additions and 489 deletions
|
@ -1,43 +1,133 @@
|
|||
using HISP.Server;
|
||||
using System;
|
||||
using HISP.Server;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HISP
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static bool ShuttingDown = false;
|
||||
public static string LogFile;
|
||||
public static string BaseDir;
|
||||
|
||||
public static void OnShutdown()
|
||||
{
|
||||
ShuttingDown = true;
|
||||
}
|
||||
public static void LogToFile(string text)
|
||||
namespace HISP.Cli
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private static StreamWriter sw = null;
|
||||
private static FileStream fs = null;
|
||||
private static string logFile;
|
||||
|
||||
public static bool ShuttingDown = false;
|
||||
public static string BaseDir;
|
||||
public static string LogFile
|
||||
{
|
||||
File.AppendAllTextAsync(text, LogFile);
|
||||
}
|
||||
public static void LogStdout(string text)
|
||||
get
|
||||
{
|
||||
return logFile;
|
||||
}
|
||||
set
|
||||
{
|
||||
logFile = value;
|
||||
if(sw != null)
|
||||
{
|
||||
sw.Flush();
|
||||
sw.Dispose();
|
||||
sw = null;
|
||||
}
|
||||
if(fs != null)
|
||||
{
|
||||
fs.Flush();
|
||||
fs.Dispose();
|
||||
fs = null;
|
||||
}
|
||||
|
||||
fs = File.OpenWrite(logFile);
|
||||
sw = new StreamWriter(fs);
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnShutdown()
|
||||
{
|
||||
Console.Out.WriteAsync(text + Console.Out.NewLine);
|
||||
}
|
||||
public static void Main(string[] args)
|
||||
ShuttingDown = true;
|
||||
}
|
||||
public static void LogToFile(bool error, string type,string text)
|
||||
{
|
||||
sw.WriteLineAsync(text + sw.NewLine);
|
||||
}
|
||||
public static void LogStdout(bool error, string type, string text)
|
||||
{
|
||||
if (error)
|
||||
Console.Error.WriteAsync("[" + type + "] " + text + Console.Error.NewLine);
|
||||
else
|
||||
Console.Out.WriteAsync("[" + type + "] " + text + Console.Out.NewLine);
|
||||
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string BaseDir = Directory.GetCurrentDirectory();
|
||||
#if DEB_PACKAGE
|
||||
ConfigReader.ConfigurationFileName = "/etc/hisp.conf"
|
||||
LogFile = "/var/log/hisp.log"
|
||||
Logger.SetCallback(LogToFile);
|
||||
#else
|
||||
Logger.SetCallback(LogStdout);
|
||||
#endif
|
||||
|
||||
Entry.SetShutdownCallback(OnShutdown);
|
||||
Entry.Start();
|
||||
|
||||
while (!ShuttingDown) { /* Allow asyncronous operations to happen. */ };
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Logger.SetCallback(LogStdout);
|
||||
|
||||
string HispConfVar = Environment.GetEnvironmentVariable("HISP_CONF_FILE");
|
||||
string HispLogVar = Environment.GetEnvironmentVariable("HISP_LOG_FILE");
|
||||
string HispBaseDir = Environment.GetEnvironmentVariable("HISP_BASE_DIR");
|
||||
|
||||
foreach (string arg in args)
|
||||
{
|
||||
switch (arg)
|
||||
{
|
||||
case "--install-service":
|
||||
#if OS_LINUX
|
||||
File.WriteAllText("/etc/systemd/system/HISP.service", Properties.Resources.HISPService);
|
||||
LogStdout(false, "INFO", "Crreated Service! enable it with \"sudo systemctl enable HISP\"");
|
||||
#else
|
||||
LogStdout(true, "ERROR", "Installing as a service unsupported on this platform");
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
if (arg.Contains("="))
|
||||
{
|
||||
string[] argu = arg.Split("=");
|
||||
if (argu.Length >= 2)
|
||||
{
|
||||
switch (argu[0])
|
||||
{
|
||||
case "--config-file":
|
||||
ConfigReader.ConfigurationFileName = argu[1];
|
||||
break;
|
||||
case "--log-to-file":
|
||||
LogFile = argu[1];
|
||||
Logger.SetCallback(LogToFile);
|
||||
break;
|
||||
case "--base-directory":
|
||||
BaseDir = argu[1];
|
||||
Directory.SetCurrentDirectory(BaseDir);
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (HispConfVar != null)
|
||||
{
|
||||
ConfigReader.ConfigurationFileName = HispConfVar;
|
||||
}
|
||||
|
||||
if (HispLogVar != null)
|
||||
{
|
||||
LogFile = HispLogVar;
|
||||
Logger.SetCallback(LogToFile);
|
||||
}
|
||||
|
||||
if (HispBaseDir != null)
|
||||
{
|
||||
BaseDir = HispBaseDir;
|
||||
Directory.SetCurrentDirectory(BaseDir);
|
||||
}
|
||||
|
||||
Entry.SetShutdownCallback(OnShutdown);
|
||||
Entry.Start();
|
||||
while (!ShuttingDown) { /* Allow asyncronous operations to happen. */ };
|
||||
Task.WaitAll();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue