Shutdown ("Exit", "Stop") now exist
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
38
RelayShared/Services/ConsoleCommandService.cs
Normal file
38
RelayShared/Services/ConsoleCommandService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user