Compare commits

...

6 Commits

3 changed files with 35 additions and 10 deletions

View File

@ -65,6 +65,9 @@ namespace Minecraft_Realms_Emulator.Controllers.Admin
if (world == null) return BadRequest("World not found");
world.State = "OPEN";
_context.SaveChanges();
new DockerHelper(world).StartServer();
return Ok(true);
@ -77,6 +80,9 @@ namespace Minecraft_Realms_Emulator.Controllers.Admin
if (world == null) return BadRequest("World not found");
world.State = "CLOSED";
_context.SaveChanges();
new DockerHelper(world).StopServer();
return Ok(true);

View File

@ -17,14 +17,20 @@ else
@server.State
</div>
<div class="buttons">
<button class="btn btn-success" @onclick="StartServer">
<i class="bi-caret-right-fill"></i>
Start
</button>
<button class="btn btn-danger" @onclick="StopServer">
<i class="bi-stop-circle-fill"></i>
Stop
</button>
@if (server.State == "CLOSED")
{
<button class="btn btn-success" @onclick="StartServer">
<i class="bi-caret-right-fill"></i>
Start
</button>
}
else
{
<button class="btn btn-danger" @onclick="StopServer">
<i class="bi-stop-circle-fill"></i>
Stop
</button>
}
</div>
</div>
@ -47,6 +53,11 @@ else
private List<string> logs = [];
protected override async Task OnInitializedAsync()
{
await GetServer();
}
public async Task GetServer()
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
@ -75,7 +86,9 @@ else
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
await JS.InvokeVoidAsync("serverLogsStream.start", DotNetObjectReference.Create(this), wId);
await httpClient.PutAsync($"http://localhost:8080/api/admin/servers/{wId}/open", new StringContent(""));
await GetServer();
}
public async Task StopServer()
@ -83,6 +96,8 @@ else
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
await JS.InvokeVoidAsync("serverLogsStream.stop");
await httpClient.PutAsync($"http://localhost:8080/api/admin/servers/{wId}/close", new StringContent(""));
await GetServer();
}
}

View File

@ -1,9 +1,13 @@
window.serverLogsStream = {
logs: "x",
start: (dotNetObject, serverId) => {
const logs = new EventSource(`http://localhost:8080/api/admin/servers/${serverId}/logs`, { withCredentials: true });
logs = new EventSource(`http://localhost:8080/api/admin/servers/${serverId}/logs`, { withCredentials: true });
logs.onmessage = event => {
dotNetObject.invokeMethodAsync("ReceiveLog", event.data);
}
}
},
stop: () => logs.close()
}