Add endpoint routing

This commit is contained in:
Neo 2020-11-01 02:24:02 +00:00
parent e9cf056b37
commit 664042ec44
3 changed files with 52 additions and 2 deletions

View File

@ -2,10 +2,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KamihamaWeb.Interfaces;
using KamihamaWeb.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Configuration;
using Serilog;
// ReSharper disable InconsistentNaming
@ -15,17 +18,41 @@ namespace KamihamaWeb.Controllers
[ApiController]
public class APIController : ControllerBase
{
public APIController(IConfiguration config)
public APIController(IConfiguration config, IMasterSingleton master)
{
_config = config;
_master = master;
}
private IConfiguration _config { get; set; }
private IMasterSingleton _master { get; set; }
[Route("endpoint")]
public IActionResult GetEndpoint()
{
var response = new APIResult(200, "ok");
response.Add("endpoint", _config["MagiRecoServer:Endpoint"]);
if (_master.Endpoints.Count == 0) // Endpoint server
{
response.Add("endpoint", _config["MagiRecoServer:Endpoint"]);
}
else // Master server
{
if (Request.Headers.ContainsKey("CF-IPCountry"))
{
if (_master.Endpoints.ContainsKey(Request.Headers["CF-IPCountry"]))
{
response.Add("endpoint", _master.Endpoints[Request.Headers["CF-IPCountry"]]);
}
else
{
response.Add("endpoint", _master.Endpoints["*"]);
}
}
else {
Log.Warning("No Cloudflare IP header found.");
response.Add("endpoint", _master.Endpoints["*"]);
}
}
response.Add("version", int.Parse(_config["MagiRecoServer:Version"]));
response.Add("max_threads", int.Parse(_config["MagiRecoServer:MaxThreads"]));
return response;

View File

@ -17,6 +17,7 @@ namespace KamihamaWeb.Interfaces
public long AssetsCurrentVersion { get; set; }
public Dictionary<string, GamedataAsset> EnglishMasterAssets { get; set; }
public Dictionary<string, Dictionary<string, GamedataAsset>> GamedataAssets { get; set; }
public Dictionary<string, string> Endpoints { get; set; }
public Task<string> ProvideJson(string which);
public Task<bool> RunUpdate();
}

View File

@ -45,10 +45,32 @@ namespace KamihamaWeb.Services
_cache = ((RedisCache) cache).GetConnection().GetDatabase();
_rest = rest;
_builder = builder;
if (_config["MagiRecoServer:Type"] == "master")
{
Log.Information("This is a master server, populating endpoints.");
foreach (var item in _config.GetSection("MagiRecoNodes").Get<Dictionary<string, string>>())
{
Log.Information($"{item.Key} -> {item.Value}");
foreach (var endpoint in item.Key.Split(","))
{
Endpoints.Add(endpoint, item.Value);
}
}
if (!Endpoints.ContainsKey("*"))
{
Log.Fatal("Missing * endpoint in config! Please add one!");
throw new Exception("Missing * endpoint in config! Please add one!");
}
}
Task.Run(Initialize);
}
public Guid Guid { get; set; }
public Dictionary<string, string> Endpoints { get; set; } = new Dictionary<string, string>();
public List<string> ModdedAssetLists = new List<string>()
{
"asset_main",