adding master api project
This commit is contained in:
20
Application/API/Master/ApiInstance.cs
Normal file
20
Application/API/Master/ApiInstance.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using RestEase;
|
||||
|
||||
namespace IW4MAdmin.Application.API.Master
|
||||
{
|
||||
public class ApiInstance
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public string Id { get; set; }
|
||||
[JsonProperty("uptime")]
|
||||
public int Uptime { get; set; }
|
||||
[JsonProperty("version")]
|
||||
public float Version { get; set; }
|
||||
[JsonProperty("servers")]
|
||||
public List<ApiServer> Servers { get; set; }
|
||||
}
|
||||
}
|
27
Application/API/Master/ApiServer.cs
Normal file
27
Application/API/Master/ApiServer.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace IW4MAdmin.Application.API.Master
|
||||
{
|
||||
public class ApiServer
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int Id { get; set; }
|
||||
[JsonProperty("port")]
|
||||
public short Port { get; set; }
|
||||
[JsonProperty("gametype")]
|
||||
public string Gametype { get; set; }
|
||||
[JsonProperty("map")]
|
||||
public string Map { get; set; }
|
||||
[JsonProperty("game")]
|
||||
public string Game { get; set; }
|
||||
[JsonProperty("hostname")]
|
||||
public string Hostname { get; set; }
|
||||
[JsonProperty("clientnum")]
|
||||
public int ClientNum { get; set; }
|
||||
[JsonProperty("maxclientnum")]
|
||||
public int MaxClientNum { get; set; }
|
||||
}
|
||||
}
|
60
Application/API/Master/Heartbeat.cs
Normal file
60
Application/API/Master/Heartbeat.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using RestEase;
|
||||
using SharedLibraryCore;
|
||||
|
||||
namespace IW4MAdmin.Application.API.Master
|
||||
{
|
||||
public class Heartbeat
|
||||
{
|
||||
static IMasterApi api;
|
||||
|
||||
public static async Task Send(ApplicationManager mgr, bool firstHeartbeat = false)
|
||||
{
|
||||
|
||||
if (firstHeartbeat)
|
||||
{
|
||||
api = RestClient.For<IMasterApi>("http://127.0.0.1");
|
||||
|
||||
var token = await api.Authenticate(new AuthenticationId()
|
||||
{
|
||||
Id = mgr.GetApplicationSettings().Configuration().Id
|
||||
});
|
||||
|
||||
api.AuthorizationToken = $"Bearer {token.AccessToken}";
|
||||
}
|
||||
|
||||
var instance = new ApiInstance()
|
||||
{
|
||||
Id = mgr.GetApplicationSettings().Configuration().Id,
|
||||
Uptime = (int)(DateTime.UtcNow - mgr.StartTime).TotalSeconds,
|
||||
Version = (float)Program.Version,
|
||||
Servers = mgr.Servers.Select(s =>
|
||||
new ApiServer()
|
||||
{
|
||||
ClientNum = s.ClientNum,
|
||||
Game = s.GameName.ToString(),
|
||||
Gametype = s.Gametype,
|
||||
Hostname = s.Hostname,
|
||||
Map = s.CurrentMap.Name,
|
||||
MaxClientNum = s.MaxClients,
|
||||
Id = s.GetHashCode(),
|
||||
Port = (short)s.GetPort()
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
if (firstHeartbeat)
|
||||
{
|
||||
instance = await api.AddInstance(instance);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
instance = await api.UpdateInstance(instance.Id, instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
Application/API/Master/IMasterApi.cs
Normal file
37
Application/API/Master/IMasterApi.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using RestEase;
|
||||
|
||||
namespace IW4MAdmin.Application.API.Master
|
||||
{
|
||||
public class AuthenticationId
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public string Id { get; set; }
|
||||
}
|
||||
|
||||
public class TokenId
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
}
|
||||
|
||||
[Header("User-Agent", "IW4MAdmin-RestEase")]
|
||||
public interface IMasterApi
|
||||
{
|
||||
[Header("Authorization")]
|
||||
string AuthorizationToken { get; set; }
|
||||
|
||||
[Post("authenticate")]
|
||||
Task<TokenId> Authenticate([Body] AuthenticationId Id);
|
||||
|
||||
[Post("instance/")]
|
||||
Task<ApiInstance> AddInstance([Body] ApiInstance instance);
|
||||
|
||||
[Put("instance/{id}")]
|
||||
Task<ApiInstance> UpdateInstance([Path] string id, [Body] ApiInstance instance);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user