PTMagic/Monitor/Startup.cs

111 lines
3.6 KiB
C#
Raw Permalink Normal View History

2018-05-22 10:11:50 +02:00
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2019-12-10 00:21:27 +01:00
using Microsoft.AspNetCore.Server.Kestrel.Core;
2018-05-22 10:11:50 +02:00
using Core.Main;
using Core.Helper;
2018-05-22 10:11:50 +02:00
using System.Runtime.InteropServices;
using System.Diagnostics;
2019-11-13 00:17:02 +01:00
namespace Monitor
{
public class Startup
{
2018-05-22 10:11:50 +02:00
PTMagicConfiguration systemConfiguration = null;
2019-11-13 00:17:02 +01:00
public Startup()
{
2018-05-22 10:11:50 +02:00
}
// This method gets called by the runtime. Use this method to add services to the container.
2019-11-13 00:17:02 +01:00
public void ConfigureServices(IServiceCollection services)
{
2018-05-22 10:11:50 +02:00
string monitorBasePath = Directory.GetCurrentDirectory();
2019-11-13 00:17:02 +01:00
if (!System.IO.File.Exists(monitorBasePath + Path.DirectorySeparatorChar + "appsettings.json"))
{
2018-05-22 10:11:50 +02:00
monitorBasePath += Path.DirectorySeparatorChar + "Monitor";
}
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(monitorBasePath)
.AddJsonFile("appsettings.json", false)
.Build();
string ptMagicBasePath = config.GetValue<string>("PTMagicBasePath");
2019-11-13 00:17:02 +01:00
if (!ptMagicBasePath.EndsWith(Path.DirectorySeparatorChar))
{
2018-05-22 10:11:50 +02:00
ptMagicBasePath += Path.DirectorySeparatorChar;
}
2019-12-10 00:21:27 +01:00
systemConfiguration = new PTMagicConfiguration(ptMagicBasePath);
2018-05-22 10:11:50 +02:00
2019-12-10 00:21:27 +01:00
services.AddMvc(option => option.EnableEndpointRouting = false);
2018-05-22 10:11:50 +02:00
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDistributedMemoryCache();
2019-11-13 00:17:02 +01:00
services.AddSession(options =>
{
2024-01-21 05:22:10 +01:00
options.IdleTimeout = TimeSpan.FromSeconds(1800);
2018-05-22 10:11:50 +02:00
options.Cookie.HttpOnly = true;
options.Cookie.Name = "PTMagicMonitor" + systemConfiguration.GeneralSettings.Monitor.Port.ToString();
});
2019-12-10 00:21:27 +01:00
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
// Remove the old tmp folder if it exists
string oldTmpFolder = monitorBasePath + System.IO.Path.DirectorySeparatorChar + "wwwroot" + System.IO.Path.DirectorySeparatorChar + "assets" + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar;
if (System.IO.Directory.Exists(oldTmpFolder))
{
System.IO.Directory.Delete(oldTmpFolder, true);
}
2018-05-22 10:11:50 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2019-12-10 00:21:27 +01:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2019-11-13 00:17:02 +01:00
{
// Register global exception handler
2019-12-10 00:21:27 +01:00
if (env.EnvironmentName == "Development")
2019-11-13 00:17:02 +01:00
{
2018-05-22 10:11:50 +02:00
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
2019-11-13 00:17:02 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
app.UseExceptionHandler("/Error");
}
2019-11-13 00:17:02 +01:00
// Configure request pipeline
app.UseStaticFiles();
2018-05-22 10:11:50 +02:00
app.UseSession();
app.UseMvcWithDefaultRoute();
2019-11-13 00:17:02 +01:00
// Open the browser
2018-05-22 10:11:50 +02:00
if (systemConfiguration.GeneralSettings.Monitor.OpenBrowserOnStart) OpenBrowser("http://localhost:" + systemConfiguration.GeneralSettings.Monitor.Port.ToString());
}
2019-11-13 00:17:02 +01:00
public static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
2018-05-22 10:11:50 +02:00
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); // Works ok on windows
2019-11-13 00:17:02 +01:00
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
2018-05-22 10:11:50 +02:00
Process.Start("xdg-open", url); // Works ok on linux
2019-11-13 00:17:02 +01:00
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
2018-05-22 10:11:50 +02:00
Process.Start("open", url); // Not tested
}
}
}
}