mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Web server - Added web server service
UI - Added remote management for bringing to foreground, restarting and shutting down UI - Simplified services namespaces
This commit is contained in:
1 parent
7d7a985d35
commit
28e1532064
31 files changed
+351
-59
No files matched your search
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using EmbedIO;
|
||||
using EmbedIO.WebApi;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// A service that provides access to the local Artemis web server
|
||||
/// </summary>
|
||||
public interface IWebServerService : IArtemisService
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the currently active instance of the web server
|
||||
/// </summary>
|
||||
WebServer? Server { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new Web API controller and restarts the web server
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of Web API controller to remove</typeparam>
|
||||
void AddController<T>() where T : WebApiController;
|
||||
|
||||
/// <summary>
|
||||
/// Removes an existing Web API controller and restarts the web server
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of Web API controller to remove</typeparam>
|
||||
void RemoveController<T>() where T : WebApiController;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a new instance of the web server was been created
|
||||
/// </summary>
|
||||
event EventHandler? WebServerCreated;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Threading.Tasks;
|
||||
using EmbedIO;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
internal class PluginsModule : WebModuleBase
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public PluginsModule(string baseRoute) : base(baseRoute)
|
||||
{
|
||||
}
|
||||
|
||||
#region Overrides of WebModuleBase
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnRequestAsync(IHttpContext context)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsFinalHandler => true;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using EmbedIO.WebApi;
|
||||
using Ninject;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
internal class WebApiControllerRegistration<T> : WebApiControllerRegistration where T : WebApiController
|
||||
{
|
||||
public WebApiControllerRegistration(IKernel kernel) : base(typeof(T))
|
||||
{
|
||||
Factory = () => kernel.Get<T>();
|
||||
}
|
||||
|
||||
public Func<T> Factory { get; set; }
|
||||
public override object UntypedFactory => Factory;
|
||||
}
|
||||
|
||||
internal abstract class WebApiControllerRegistration
|
||||
{
|
||||
protected WebApiControllerRegistration(Type controllerType)
|
||||
{
|
||||
ControllerType = controllerType;
|
||||
}
|
||||
|
||||
public abstract object UntypedFactory { get; }
|
||||
public Type ControllerType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using EmbedIO;
|
||||
using EmbedIO.Actions;
|
||||
using EmbedIO.WebApi;
|
||||
using Ninject;
|
||||
using Serilog;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
internal class WebServerService : IWebServerService, IDisposable
|
||||
{
|
||||
private readonly IKernel _kernel;
|
||||
private readonly ILogger _logger;
|
||||
private readonly PluginsModule _pluginModule;
|
||||
private readonly PluginSetting<int> _webServerPortSetting;
|
||||
private readonly List<WebApiControllerRegistration> _controllers;
|
||||
|
||||
public WebServerService(IKernel kernel, ILogger logger, ISettingsService settingsService)
|
||||
{
|
||||
_kernel = kernel;
|
||||
_logger = logger;
|
||||
_controllers = new List<WebApiControllerRegistration>();
|
||||
|
||||
_webServerPortSetting = settingsService.GetSetting("WebServer.Port", 9696);
|
||||
_webServerPortSetting.SettingChanged += WebServerPortSettingOnSettingChanged;
|
||||
|
||||
_pluginModule = new PluginsModule("/plugin");
|
||||
Server = CreateWebServer();
|
||||
Server.Start();
|
||||
}
|
||||
|
||||
public WebServer? Server { get; private set; }
|
||||
|
||||
#region Web server managament
|
||||
|
||||
private WebServer CreateWebServer()
|
||||
{
|
||||
Server?.Dispose();
|
||||
Server = null;
|
||||
|
||||
string url = $"http://localhost:{_webServerPortSetting.Value}/";
|
||||
WebApiModule apiModule = new("/api/");
|
||||
WebServer server = new WebServer(o => o.WithUrlPrefix(url).WithMode(HttpListenerMode.EmbedIO))
|
||||
.WithLocalSessionManager()
|
||||
.WithModule(apiModule)
|
||||
.WithModule(_pluginModule)
|
||||
.WithModule(new ActionModule("/", HttpVerbs.Any, ctx => ctx.SendDataAsync(new {Message = "Error"})));
|
||||
|
||||
// Add controllers to the API module
|
||||
foreach (WebApiControllerRegistration registration in _controllers)
|
||||
apiModule.RegisterController(registration.ControllerType, (Func<WebApiController>) registration.UntypedFactory);
|
||||
|
||||
// Listen for state changes.
|
||||
server.StateChanged += (s, e) => _logger.Verbose("WebServer new state - {state}", e.NewState);
|
||||
|
||||
// Store the URL in a webserver.txt file so that remote applications can find it
|
||||
File.WriteAllText(Path.Combine(Constants.DataFolder, "webserver.txt"), url);
|
||||
OnWebServerCreated();
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void WebServerPortSettingOnSettingChanged(object? sender, EventArgs e)
|
||||
{
|
||||
Server = CreateWebServer();
|
||||
Server.Start();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
Server?.Dispose();
|
||||
_webServerPortSetting.SettingChanged -= WebServerPortSettingOnSettingChanged;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Controller management
|
||||
|
||||
public void AddController<T>() where T : WebApiController
|
||||
{
|
||||
_controllers.Add(new WebApiControllerRegistration<T>(_kernel));
|
||||
Server = CreateWebServer();
|
||||
Server.Start();
|
||||
}
|
||||
|
||||
public void RemoveController<T>() where T : WebApiController
|
||||
{
|
||||
_controllers.RemoveAll(r => r.ControllerType == typeof(T));
|
||||
Server = CreateWebServer();
|
||||
Server.Start();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler? WebServerCreated;
|
||||
|
||||
protected virtual void OnWebServerCreated()
|
||||
{
|
||||
WebServerCreated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user