PTMagic/Monitor/Startup.cs

105 lines
3.1 KiB
C#
Raw 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;
2019-11-13 00:17:02 +01:00
using Microsoft.AspNetCore.Diagnostics;
2018-05-22 10:11:50 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Core.Main;
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-11-13 00:17:02 +01:00
try
{
2018-05-22 10:11:50 +02:00
systemConfiguration = new PTMagicConfiguration(ptMagicBasePath);
2019-11-13 00:17:02 +01:00
}
catch (Exception ex)
{
2018-05-22 10:11:50 +02:00
throw ex;
}
services.AddMvc();
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDistributedMemoryCache();
2019-11-13 00:17:02 +01:00
services.AddSession(options =>
{
2018-05-22 10:11:50 +02:00
options.IdleTimeout = TimeSpan.FromSeconds(900);
options.Cookie.HttpOnly = true;
options.Cookie.Name = "PTMagicMonitor" + systemConfiguration.GeneralSettings.Monitor.Port.ToString();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2019-11-13 00:17:02 +01:00
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Register global exception handler
if (env.IsDevelopment())
{
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.UseMvc();
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
}
}
}
}