Minecraft-Realms-Emulator/Panel/Components/Pages/Servers/Server.razor

103 lines
3.0 KiB
Plaintext
Raw Normal View History

@page "/servers/{wId:int}"
@inject IJSRuntime JS
@rendermode InteractiveServer
<PageTitle>Server #@wId</PageTitle>
@if (server == null)
{
<p><em>Loading...</em></p>
}
else
{
<div class="panel">
<div class="header">
<div class="status">
@server.Name
@server.State
</div>
<div class="buttons">
@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>
<div class="console">
<pre class="output">
@foreach (var log in logs)
{
@log<br />
}
</pre>
</div>
</div>
}
@code {
[Parameter]
public int wId { get; set; }
private World? server;
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"));
server = await httpClient.GetFromJsonAsync<World>($"http://localhost:8080/api/admin/servers/{wId}");
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("serverLogsStream.start", DotNetObjectReference.Create(this), wId);
}
}
[JSInvokable]
public Task ReceiveLog(string log)
{
logs.Add(log);
InvokeAsync(StateHasChanged);
return Task.CompletedTask;
}
public async Task StartServer()
{
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()
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
2024-05-30 08:42:18 -04:00
await JS.InvokeVoidAsync("serverLogsStream.stop");
await httpClient.PutAsync($"http://localhost:8080/api/admin/servers/{wId}/close", new StringContent(""));
await GetServer();
}
}