preliminary implementation of Mochafile processing

This commit is contained in:
Michael Becker 2025-03-06 16:16:59 -05:00
parent 3d0e92c1e4
commit ae64d219ec
5 changed files with 190 additions and 36 deletions

View File

@ -8,6 +8,8 @@ using MBS.Core;
using Mocha.Core;
using Mocha.Core.UI.Server;
using Mocha.Core.Logging;
using Mocha.Core.Logging.Loggers;
/// <summary>
/// Provides the entry point for a simple application which starts a Web server
@ -33,8 +35,6 @@ public class Program : WebApplication
private LibraryHandle l_System, l_Web;
public Oms Oms { get; }
private System.IO.StreamWriter? logSt = null;
protected override WebServer CreateWebServer()
{
@ -56,21 +56,15 @@ public class Program : WebApplication
Console.Error.WriteLine("oms: error: failed to write PID file; graceful control will be unavailable");
}
string logFile = "/var/log/mocha/oms-dotnet.log";
try
{
OpenLogFile(logFile);
}
catch (UnauthorizedAccessException ex)
{
logFile = System.IO.Path.Combine(new string[] { System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "oms-dotnet.log" });
OpenLogFile(logFile);
}
DefaultFileLogger logger = new DefaultFileLogger();
ConsoleLogger logger2 = new ConsoleLogger();
Log.Loggers.Add(logger);
Log.Loggers.Add(logger2);
logSt?.WriteLine("**********");
logSt?.WriteLine("Mocha .NET OMS, version {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
logSt?.WriteLine(String.Format("{0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
logSt?.WriteLine("**********");
Log.WriteLine("**********");
Log.WriteLine("Mocha .NET OMS, version {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Log.WriteLine(String.Format("{0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
Log.WriteLine("**********");
// this all has to be done before the Web server is started...
Oms.Initialize();
@ -132,17 +126,6 @@ public class Program : WebApplication
base.OnStartup(e);
}
private void OpenLogFile(string logFile)
{
string logDir = System.IO.Path.GetDirectoryName(logFile);
if (!System.IO.Directory.Exists(logDir))
{
System.IO.Directory.CreateDirectory(logDir);
}
logSt = new StreamWriter(System.IO.File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Read), System.Text.Encoding.UTF8);
logSt.AutoFlush = true;
}
private bool UpdateMochafile()
{
bool enableMochaFile = true;
@ -151,11 +134,11 @@ public class Program : WebApplication
// check to see if we have a Mochafile that we need to load
// (this should only be done on development build)
string Mochafilename = "/var/mocha/uploads/Mochafile.json";
logSt.WriteLine(String.Format("oms-dotnet: looking for Mochafile '{0}'", Mochafilename));
Log.WriteLine("oms-dotnet: looking for Mochafile '{0}'", Mochafilename);
if (System.IO.File.Exists(Mochafilename))
{
logSt.WriteLine("oms-dotnet: Mochafile found");
Log.WriteLine("oms-dotnet: Mochafile found");
if (LoadMochafile(Mochafilename))
{
return true;
@ -163,24 +146,24 @@ public class Program : WebApplication
}
else
{
logSt.WriteLine("oms-dotnet: Mochafile not found");
Log.WriteLine("oms-dotnet: Mochafile not found");
}
}
else
{
logSt.WriteLine("oms-dotnet: skipping Mochafile processing (not enabled)");
Log.WriteLine("oms-dotnet: skipping Mochafile processing (not enabled)");
}
return false;
}
private bool LoadMochafile(string mochafilename)
{
logSt?.Write(String.Format("oms-dotnet: loading Mochafile at '{0}'", mochafilename));
Log.WriteLine("oms-dotnet: loading Mochafile at '{0}'", mochafilename);
JsonObject? json = JsonNode.Parse(System.IO.File.ReadAllText(mochafilename)) as JsonObject;
if (json == null)
{
logSt?.Write("oms-dotnet: Mochafile error: could not parse JSON");
Log.WriteLine("oms-dotnet: Mochafile error: could not parse JSON");
return false;
}
@ -193,16 +176,16 @@ public class Program : WebApplication
if (jo.ContainsKey("name"))
{
string name = jo["name"].GetValue<string>();
logSt?.Write(String.Format("oms-dotnet: Mochafile says create tenant '{0}'", name));
Log.WriteLine("oms-dotnet: Mochafile says create tenant '{0}'", name);
TenantHandle th = Oms.CreateTenant(name);
logSt?.Write(String.Format("oms-dotnet: create tenant ok, handle is '{0}'", th));
Log.WriteLine("oms-dotnet: create tenant ok, handle is '{0}'", th);
}
}
}
}
logSt?.Write("oms-dotnet: Mochafile processing complete");
Log.WriteLine("oms-dotnet: Mochafile processing complete");
return true;
}

View File

@ -0,0 +1,39 @@
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
namespace Mocha.Core.Logging;
public static class Log
{
public static List<Logger> Loggers { get; } = new List<Logger>();
public static void WriteLine(string text)
{
foreach (Logger logger in Loggers)
{
logger.WriteLine(text);
}
}
public static void WriteLine(string text, params object[] format)
{
foreach (Logger logger in Loggers)
{
logger.WriteLine(text, format);
}
}
}

View File

@ -0,0 +1,59 @@
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
namespace Mocha.Core.Logging;
public class Logger
{
public Stream? Stream { get; protected set; }
private StreamWriter? _sw;
protected StreamWriter? StreamWriter
{
get
{
if (_sw == null)
{
if (Stream != null)
{
_sw = new StreamWriter(Stream, System.Text.Encoding.UTF8);
_sw.AutoFlush = true;
}
}
return _sw;
}
}
private System.IO.StreamWriter? sw = null;
protected Logger()
{
}
public Logger(Stream st)
{
Stream = st;
}
public void WriteLine(string text)
{
StreamWriter?.WriteLine(text);
}
public void WriteLine(string text, params object?[] format)
{
StreamWriter?.WriteLine(text, format);
}
}

View File

@ -0,0 +1,26 @@
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
namespace Mocha.Core.Logging.Loggers;
public class ConsoleLogger : Logger
{
public ConsoleLogger()
{
Stream = Console.OpenStandardOutput();
}
}

View File

@ -0,0 +1,47 @@
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
namespace Mocha.Core.Logging.Loggers;
public class DefaultFileLogger : Logger
{
public DefaultFileLogger()
{
string logFile = "/var/log/mocha/oms-dotnet.log";
try
{
OpenLogFile(logFile);
}
catch (UnauthorizedAccessException ex)
{
logFile = System.IO.Path.Combine(new string[] { System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "oms-dotnet.log" });
OpenLogFile(logFile);
}
}
private void OpenLogFile(string logFile)
{
string logDir = System.IO.Path.GetDirectoryName(logFile);
if (!System.IO.Directory.Exists(logDir))
{
System.IO.Directory.CreateDirectory(logDir);
}
Stream st = System.IO.File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Read);
}
}