Improved UI experience for the Main page
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using DiscordBotCore.Bot;
|
||||
using DiscordBotCore.PluginManagement.Loading;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI.Models;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI.Controllers;
|
||||
@@ -8,25 +8,64 @@ namespace WebUI.Controllers;
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
public HomeController(ILogger logger)
|
||||
private readonly IDiscordBotApplication _discordBotApplication;
|
||||
private readonly IPluginLoader _pluginLoader;
|
||||
|
||||
public HomeController(ILogger logger, IDiscordBotApplication discordBotApplication, IPluginLoader pluginLoader)
|
||||
{
|
||||
_logger = logger;
|
||||
_discordBotApplication = discordBotApplication;
|
||||
_pluginLoader = pluginLoader;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
_logger.Log("Index page loaded", this);
|
||||
ViewBag.IsRunning = _discordBotApplication.IsReady;
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> StartApplication()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
if (_discordBotApplication.IsReady)
|
||||
{
|
||||
_logger.Log("Application already started", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
await _discordBotApplication.StartAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> StopApplication()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
if (!_discordBotApplication.IsReady)
|
||||
{
|
||||
_logger.Log("Application already stopped", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
await _discordBotApplication.StopAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public JsonResult GetLogs()
|
||||
{
|
||||
var logText = _logger.GetLogsHistory();
|
||||
var logs = logText.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
return Json(logs);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> LoadPlugins()
|
||||
{
|
||||
_logger.Log("Loading plugins", this);
|
||||
await _pluginLoader.LoadPlugins();
|
||||
_logger.Log("Plugins loaded", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,15 @@ builder.Services.AddSingleton<IPluginManager>(sp =>
|
||||
IPluginManager pluginManager = new PluginManager(pluginRepository, logger, configuration);
|
||||
return pluginManager;
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<IPluginLoader>(sp =>
|
||||
{
|
||||
IPluginManager pluginManager = sp.GetRequiredService<IPluginManager>();
|
||||
ILogger logger = sp.GetRequiredService<ILogger>();
|
||||
IConfiguration configuration = sp.GetRequiredService<IConfiguration>();
|
||||
return new PluginLoader(pluginManager, logger, configuration);
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<IDiscordBotApplication>(sp =>
|
||||
{
|
||||
ILogger logger = sp.GetRequiredService<ILogger>();
|
||||
@@ -150,14 +159,7 @@ builder.Services.AddSingleton<IDiscordBotApplication>(sp =>
|
||||
return new DiscordBotApplication(logger, configuration, pluginLoader);
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<IPluginLoader>(sp =>
|
||||
{
|
||||
IPluginManager pluginManager = sp.GetRequiredService<IPluginManager>();
|
||||
ILogger logger = sp.GetRequiredService<ILogger>();
|
||||
IConfiguration configuration = sp.GetRequiredService<IConfiguration>();
|
||||
IDiscordBotApplication discordBotApplication = sp.GetRequiredService<IDiscordBotApplication>();
|
||||
return new PluginLoader(pluginManager, logger, configuration, discordBotApplication.Client);
|
||||
});
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -1,8 +1,59 @@
|
||||
@{
|
||||
ViewData["Title"] = "Home Page";
|
||||
var isRunning = ViewBag.IsRunning ?? false;
|
||||
ViewBag.Title = "Console Log Viewer";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">Welcome</h1>
|
||||
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
||||
</div>
|
||||
<div class="container mt-5">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<h4>Console Log</h4>
|
||||
<div id="consoleLog" class="border p-3 bg-dark text-white" style="height: 400px; overflow-y: scroll; font-family: monospace;"></div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<h4>Controls</h4>
|
||||
<form method="post" asp-action="StartApplication">
|
||||
<button type="submit" class="btn btn-success mb-2">Start Application</button>
|
||||
</form>
|
||||
@if (isRunning)
|
||||
{
|
||||
<form method="post" asp-action="StopApplication">
|
||||
<button type="submit" class="btn btn-danger mb-2">Stop Application</button>
|
||||
</form>
|
||||
<form method="post" asp-action="LoadPlugins">
|
||||
<button type="submit" class="btn btn-info mb-2">Load Plugins</button>
|
||||
</form>
|
||||
}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
let lastLogCount = 0;
|
||||
|
||||
function fetchLogs() {
|
||||
fetch("/Home/GetLogs")
|
||||
.then(res => res.json())
|
||||
.then(logs => {
|
||||
const logDiv = document.getElementById("consoleLog");
|
||||
if (logs.length !== lastLogCount) {
|
||||
logDiv.innerHTML = logs.map(line => formatLog(line)).join('');
|
||||
logDiv.scrollTop = logDiv.scrollHeight;
|
||||
lastLogCount = logs.length;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function formatLog(line) {
|
||||
if (line.includes("[Error]")) return `<div style="color: #ff4d4d;">${line}</div>`;
|
||||
if (line.includes("[Warning]")) return `<div style="color: #ffa500;">${line}</div>`;
|
||||
if (line.includes("[Debug]")) return `<div style="color: #88f;">${line}</div>`;
|
||||
return `<div>${line}</div>`;
|
||||
}
|
||||
|
||||
setInterval(fetchLogs, 2000);
|
||||
fetchLogs();
|
||||
</script>
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
@{
|
||||
ViewData["Title"] = "Privacy Policy";
|
||||
}
|
||||
<h1>@ViewData["Title"]</h1>
|
||||
|
||||
<p>Use this page to detail your site's privacy policy.</p>
|
||||
Reference in New Issue
Block a user