2018-05-22 10:11:50 +02:00
using System ;
using System.IO ;
2019-11-12 00:36:18 +01:00
using System.Security.Permissions ;
2018-05-22 10:11:50 +02:00
using Microsoft.AspNetCore.Hosting ;
using Microsoft.Extensions.Configuration ;
using Core.Main ;
2019-11-12 00:36:18 +01:00
namespace Monitor
{
public class Program
{
// Main entry point
2023-07-01 07:45:51 +02:00
//[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
2019-11-12 00:36:18 +01:00
public static void Main ( string [ ] args )
{
// Register a global exception handler
AppDomain . CurrentDomain . UnhandledException + = new UnhandledExceptionEventHandler ( GlobalUnhandledExceptionHandler ) ;
// Start
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "##########################################################" ) ;
Logger . WriteLine ( "#********************************************************#" ) ;
Logger . WriteLine ( "INFO: Starting PT Magic Monitor..." ) ;
Logger . WriteLine ( "INFO: Beginning startup checks..." ) ;
2018-05-22 10:11:50 +02:00
string monitorBasePath = Directory . GetCurrentDirectory ( ) ;
2019-11-12 00:36:18 +01:00
if ( ! System . IO . File . Exists ( monitorBasePath + Path . DirectorySeparatorChar + "appsettings.json" ) )
{
2018-05-22 10:11:50 +02:00
monitorBasePath + = Path . DirectorySeparatorChar + "Monitor" ;
}
// Startup checks
string appsettingsJson = monitorBasePath + Path . DirectorySeparatorChar + "appsettings.json" ;
2019-11-12 00:36:18 +01:00
if ( ! File . Exists ( appsettingsJson ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: appsettings.json not found: '" + appsettingsJson + "'. Please check if the file exists. If not, review the PT Magic setup steps listed on the wiki!" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: appsettings.json found in " + monitorBasePath ) ;
2018-05-22 10:11:50 +02:00
IConfiguration config = new ConfigurationBuilder ( )
. SetBasePath ( monitorBasePath )
. AddJsonFile ( "appsettings.json" , false )
. Build ( ) ;
string ptMagicBasePath = config . GetValue < string > ( "PTMagicBasePath" ) ;
2019-11-12 00:36:18 +01:00
if ( ! ptMagicBasePath . EndsWith ( Path . DirectorySeparatorChar ) )
{
2018-05-22 10:11:50 +02:00
ptMagicBasePath + = Path . DirectorySeparatorChar ;
}
// More startup checks
// Check if PT Magic directoy is correctly configured
2019-11-12 00:36:18 +01:00
if ( ! Directory . Exists ( ptMagicBasePath ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: PT Magic directory not found: '" + ptMagicBasePath + "'. Please check your setting for 'PTMagicBasePath' in 'Monitor/appsettings.json'" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: PT Magic directory found at " + ptMagicBasePath ) ;
2018-05-22 10:11:50 +02:00
// Check if PT Magic settings file exists
string settingsGeneralJson = ptMagicBasePath + "settings.general.json" ;
2019-11-12 00:36:18 +01:00
if ( ! File . Exists ( settingsGeneralJson ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: PT Magic settings not found: '" + settingsGeneralJson + "'. Please check if you setup PT Magic correctly!" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: settings.general.json found at " + settingsGeneralJson ) ;
2018-05-22 10:11:50 +02:00
// Check if PT Magic settings file exists
string lastRuntimeSummaryJson = ptMagicBasePath + Constants . PTMagicPathData + Path . DirectorySeparatorChar + "LastRuntimeSummary.json" ;
2019-11-12 00:36:18 +01:00
if ( ! File . Exists ( lastRuntimeSummaryJson ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: PT Magic runtime summary not found: '" + lastRuntimeSummaryJson + "'. Please wait for PT Magic to complete its first run!" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: LastRuntimeSummary.json found at " + lastRuntimeSummaryJson ) ;
2018-05-22 10:11:50 +02:00
PTMagicConfiguration ptMagicConfiguration = null ;
2019-11-12 00:36:18 +01:00
try
{
2018-05-22 10:11:50 +02:00
ptMagicConfiguration = new PTMagicConfiguration ( ptMagicBasePath ) ;
2019-11-12 00:36:18 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
throw ex ;
}
string wwwrootPath = monitorBasePath + Path . DirectorySeparatorChar + "wwwroot" ;
2019-11-12 00:36:18 +01:00
if ( ! Directory . Exists ( wwwrootPath ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: wwwroot directory not found: '" + wwwrootPath + "'. Did you copy all files as instructed on the wiki?" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: wwwroot directory found at " + wwwrootPath ) ;
2018-05-22 10:11:50 +02:00
string assetsPath = wwwrootPath + Path . DirectorySeparatorChar + "assets" ;
2019-11-12 00:36:18 +01:00
if ( ! Directory . Exists ( assetsPath ) )
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "ERROR: assets directory not found: '" + assetsPath + "'. Did you copy all files as instructed on the wiki?" ) ;
2019-02-04 01:17:38 +01:00
if ( ! Console . IsInputRedirected ) Console . ReadKey ( ) ;
2019-11-12 00:36:18 +01:00
}
else
{
2019-11-13 00:17:02 +01:00
Logger . WriteLine ( "INFO: assets directory found at " + assetsPath ) ;
Logger . WriteLine ( "INFO: ALL CHECKS COMPLETED - ATTEMPTING TO START WEBSERVER..." ) ;
Logger . WriteLine ( "#********************************************************#" ) ;
Logger . WriteLine ( "" ) ;
Logger . WriteLine ( "DO NOT CLOSE THIS WINDOW! THIS IS THE WEBSERVER FOR YOUR MONITOR!" ) ;
Logger . WriteLine ( "" ) ;
Logger . WriteLine ( "##########################################################" ) ;
Logger . WriteLine ( "" ) ;
2018-05-22 10:11:50 +02:00
BuildWebHost ( args , monitorBasePath , monitorBasePath + Path . DirectorySeparatorChar + "wwwroot" , ptMagicConfiguration . GeneralSettings . Monitor . Port ) . Run ( ) ;
}
}
}
}
}
}
}
public static IWebHost BuildWebHost ( string [ ] args , string contentRoot , string webroot , int port ) = >
new WebHostBuilder ( )
. UseUrls ( "http://0.0.0.0:" + port . ToString ( ) )
. UseStartup < Startup > ( )
. UseKestrel ( )
. UseContentRoot ( contentRoot )
. UseWebRoot ( webroot )
. Build ( ) ;
2019-11-12 00:36:18 +01:00
// Global unhandled exception handler
private static void GlobalUnhandledExceptionHandler ( object sender , UnhandledExceptionEventArgs args )
{
Exception e = ( Exception ) args . ExceptionObject ;
2019-11-13 00:17:02 +01:00
Logger . WriteException ( e , "An unhandled fatal exception occurred" ) ;
2019-11-12 00:36:18 +01:00
}
2018-05-22 10:11:50 +02:00
}
2019-11-12 00:36:18 +01:00
2018-05-22 10:11:50 +02:00
}