1af50bfd34
* Changed application to use background services and removed hacky http server startup * Small improvements and method removals * Removed Core dependency on Web application SDK * Fixed wrong imported type --------- Co-authored-by: Alex <clodanSPT@hotmail.com> Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
97 lines
2.4 KiB
C#
97 lines
2.4 KiB
C#
using System.Collections.Frozen;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using Microsoft.AspNetCore.Http;
|
|
using SPTarkov.DI.Annotations;
|
|
using SPTarkov.Server.Core.Models.Spt.Config;
|
|
using SPTarkov.Server.Core.Servers;
|
|
|
|
namespace SPTarkov.Server.Core.Helpers;
|
|
|
|
[Injectable(InjectionType.Singleton)]
|
|
public class HttpServerHelper(ConfigServer configServer)
|
|
{
|
|
protected readonly HttpConfig _httpConfig = configServer.GetConfig<HttpConfig>();
|
|
|
|
protected readonly FrozenDictionary<string, string> mime = new Dictionary<string, string>
|
|
{
|
|
{ "css", "text/css" },
|
|
{ "bin", "application/octet-stream" },
|
|
{ "html", "text/html" },
|
|
{ "jpg", "image/jpeg" },
|
|
{ "js", "text/javascript" },
|
|
{ "json", "application/json" },
|
|
{ "png", "image/png" },
|
|
{ "svg", "image/svg+xml" },
|
|
{ "txt", "text/plain" },
|
|
}.ToFrozenDictionary();
|
|
|
|
public string? GetMimeText(string key)
|
|
{
|
|
return mime.GetValueOrDefault(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Combine ip and port into address
|
|
/// </summary>
|
|
/// <returns>URI</returns>
|
|
public string BuildUrl()
|
|
{
|
|
return $"{_httpConfig.BackendIp}:{_httpConfig.BackendPort}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepend http to the url:port
|
|
/// </summary>
|
|
/// <returns>URI</returns>
|
|
public string GetBackendUrl()
|
|
{
|
|
return $"https://{BuildUrl()}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get websocket url + port
|
|
/// </summary>
|
|
/// <returns>wss:// address</returns>
|
|
public string GetWebsocketUrl()
|
|
{
|
|
return $"wss://{BuildUrl()}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method to determine if another version of the server is already running
|
|
/// </summary>
|
|
/// <returns>bool isAlreadyRunning</returns>
|
|
public bool IsAlreadyRunning()
|
|
{
|
|
TcpListener? listener = null;
|
|
|
|
try
|
|
{
|
|
listener = new(IPAddress.Parse(_httpConfig.Ip), _httpConfig.Port);
|
|
listener.Start();
|
|
return false;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return true;
|
|
}
|
|
finally
|
|
{
|
|
listener?.Stop();
|
|
}
|
|
}
|
|
|
|
public void SendTextJson(HttpResponse resp, object output)
|
|
{
|
|
resp.Headers.Append("Content-Type", mime["json"]);
|
|
resp.StatusCode = 200;
|
|
/* TODO: figure this one out
|
|
resp.writeHead(200, "OK", {
|
|
"Content-Type": this.mime.json
|
|
});
|
|
resp.end(output);
|
|
*/
|
|
}
|
|
}
|