Shutdown ("Exit", "Stop") now exist

This commit is contained in:
2026-04-13 16:27:12 -04:00
parent 085507519a
commit 627d67be39
3 changed files with 42 additions and 2 deletions

View File

@@ -20,12 +20,12 @@ var kira = await CreateUserAsync(db, "Ru_Kira", "jduesling13@gmail.com", "passwo
var test = await CreateUserAsync(db, "Test", "test@gmail.com", "password");
var server = new Program();
server.Main(db);
Console.WriteLine($"Keeper created: {ToJsonString(keeper)}");
Console.WriteLine($"Kira created: {ToJsonString(kira)}");
Console.WriteLine($"Test created: {ToJsonString(test)}");
await server.Main(db);
Console.ReadKey(true);
return;

View File

@@ -4,6 +4,7 @@ using RelayServer.Services.Core;
using RelayServer.Services.Data;
using RelayServer.Services.Rtc;
using RelayShared.Rtc;
using RelayShared.Services;
using WebSocketSharp.Server;
var surrealService = new SurrealService();
@@ -41,7 +42,8 @@ Console.WriteLine("WebSocket server started");
await app.StartAsync();
Console.WriteLine("HTTP API started");
Console.ReadKey(true); // TODO: Make program stop be a console command
ConsoleCommandService.Start();
await Task.Delay(Timeout.Infinite, ConsoleCommandService.ShutdownTokenSource.Token);
wssv.Stop();
await app.StopAsync();

View File

@@ -0,0 +1,38 @@
namespace RelayShared.Services;
public static class ConsoleCommandService
{
public static CancellationTokenSource ShutdownTokenSource { get; } = new();
public static void Start()
{
Task.Run(() =>
{
while (!ShutdownTokenSource.IsCancellationRequested)
{
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
continue;
HandleCommand(input.Trim().ToLower());
}
});
}
private static void HandleCommand(string command)
{
switch (command)
{
case "exit":
case "stop":
Console.WriteLine("Shutting down...");
ShutdownTokenSource.Cancel();
break;
default:
Console.WriteLine($"Unknown command: {command}");
break;
}
}
}