feat: overhaul Web with new arbiter code

This commit is contained in:
rjindael 2023-09-10 02:31:23 -07:00
parent 1e0648a005
commit 9e4cb6bfce
No known key found for this signature in database
GPG Key ID: D069369C906CCF31
5 changed files with 43 additions and 49 deletions

View File

@ -111,7 +111,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
} }
} }
var clientRelease = await Http.GetJson<ClientRelease>(Web.Url($"/api/setup/{Arguments["Version"]}")); var clientRelease = await Http.GetJson<ClientRelease>(Web.FormatUrl($"/api/setup/{Arguments["Version"]}"));
bool clientUpToDate = true; bool clientUpToDate = true;
bool createStudioShortcut = false; bool createStudioShortcut = false;
@ -226,7 +226,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
StartInfo = new() StartInfo = new()
{ {
FileName = Path.Combine(Paths.Versions, Arguments["Version"], $"{Constants.PROJECT_NAME}.Player.exe"), FileName = Path.Combine(Paths.Versions, Arguments["Version"], $"{Constants.PROJECT_NAME}.Player.exe"),
Arguments = $"-a \"{Web.Url("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\"", Arguments = $"-a \"{Web.FormatUrl("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\"",
UseShellExecute = true, UseShellExecute = true,
} }
}; };
@ -446,7 +446,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
} }
// Load the license... // Load the license...
while (!Web.LoadLicense(File.ReadAllText(Paths.License))) while (!Web.License(File.ReadAllText(Paths.License)))
{ {
// ...and if it's corrupt, keep asking for a new one. // ...and if it's corrupt, keep asking for a new one.
File.Delete(Paths.License); File.Delete(Paths.License);

View File

@ -0,0 +1,8 @@
namespace Kiseki.Launcher.Enums;
public enum HealthCheckStatus
{
Failure,
Maintenance,
Success
}

View File

@ -1,9 +1,7 @@
namespace Kiseki.Launcher.Models; namespace Kiseki.Launcher.Models;
using System.Text.Json.Serialization;
public class HealthCheck public class HealthCheck
{ {
[JsonPropertyName("status")] [JsonPropertyName("status")]
public int Status { get; set; } = -1; public HealthCheckStatus Status { get; set; }
} }

View File

@ -1,13 +0,0 @@
namespace Kiseki.Launcher.Models;
public readonly struct WebResponse
{
public WebResponse(int status, object? data)
{
Status = status;
Data = data;
}
public int Status { get; }
public object? Data { get; }
}

View File

@ -1,51 +1,33 @@
namespace Kiseki.Launcher; namespace Kiseki.Launcher;
using System.Text.Json;
public static class Web public static class Web
{ {
public const int RESPONSE_FAILURE = -1; public static string? CurrentUrl { get; private set; } = null;
public const int RESPONSE_SUCCESS = 0; public static bool IsConnected { get; private set; } = false;
public const int RESPONSE_MAINTENANCE = 1;
public static string CurrentUrl { get; private set; } = "";
public static bool IsInMaintenance { get; private set; } = false; public static bool IsInMaintenance { get; private set; } = false;
public static readonly HttpClient HttpClient = new(); public static readonly HttpClient HttpClient = new();
public static bool Initialize() public static async void Initialize()
{ {
CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL; CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL;
// Synchronous block is intentional HealthCheckStatus status = await GetHealthStatus();
Task<Models.WebResponse> task = CheckHealth(); if (status != HealthCheckStatus.Success)
task.Wait();
int response = task.Result.Status;
if (response != RESPONSE_SUCCESS)
{ {
if (response == RESPONSE_MAINTENANCE) IsInMaintenance = true; if (status == HealthCheckStatus.Maintenance)
{
IsInMaintenance = true;
}
return false; return;
} }
return true; IsConnected = true;
} }
public static string Url(string path) => $"https://{CurrentUrl}{path}"; public static bool License(string license)
public static async Task<Models.WebResponse> CheckHealth()
{ {
var response = await Utilities.Http.GetJson<Models.HealthCheck>(Url("/api/health"));
return new Models.WebResponse(response?.Status ?? RESPONSE_FAILURE, response);
}
public static bool LoadLicense(string license)
{
// The license is just headers required to access the website in a JSON document
Dictionary<string, string> headers; Dictionary<string, string> headers;
try try
@ -64,4 +46,23 @@ public static class Web
return true; return true;
} }
public static async Task<HealthCheckStatus> GetHealthStatus()
{
var response = await Http.GetJson<HealthCheck>(FormatUrl("/health-check"));
return response?.Status ?? HealthCheckStatus.Failure;
}
public static string FormatUrl(string path, string? subdomain = null)
{
string scheme = "https";
string url = subdomain == null ? CurrentUrl! : $"{subdomain!}.{CurrentUrl!}";
#if DEBUG
scheme = "http";
#endif
return $"{scheme}://{url}{path}";
}
} }