2021-06-02 09:39:20 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
2021-06-11 16:08:31 -04:00
|
|
|
using EpicGames.Core;
|
2021-06-02 09:39:20 -04:00
|
|
|
using HordeServer.Models;
|
|
|
|
|
using HordeServer.Utilities;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-02 15:16:14 -04:00
|
|
|
using System.IO;
|
2021-06-02 09:39:20 -04:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2021-06-02 15:16:14 -04:00
|
|
|
using System.Reflection;
|
2021-06-02 09:39:20 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HordeServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller for the /api/v1/schema endpoint
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class SchemaController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
class CatalogItem
|
|
|
|
|
{
|
|
|
|
|
public string? Name { get; set; }
|
2021-06-02 10:55:54 -04:00
|
|
|
public string? Description { get; set; }
|
2021-06-02 15:16:14 -04:00
|
|
|
public string[]? FileMatch { get; set; }
|
2021-06-02 09:39:20 -04:00
|
|
|
public Uri? Url { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CatalogRoot
|
|
|
|
|
{
|
|
|
|
|
[JsonPropertyName("$schema")]
|
|
|
|
|
public string Schema { get; set; } = "https://json.schemastore.org/schema-catalog.json";
|
|
|
|
|
public int Version { get; set; } = 1;
|
2021-06-02 11:21:56 -04:00
|
|
|
public List<CatalogItem> Schemas { get; set; } = new List<CatalogItem>();
|
2021-06-02 09:39:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the catalog for config schema
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Information about all the schedules</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api/v1/schema/catalog.json")]
|
|
|
|
|
public ActionResult GetCatalog()
|
|
|
|
|
{
|
|
|
|
|
string? Host = null;
|
|
|
|
|
|
|
|
|
|
StringValues Hosts;
|
|
|
|
|
if (Request.Headers.TryGetValue("Host", out Hosts))
|
|
|
|
|
{
|
|
|
|
|
Host = Hosts.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Host == null)
|
|
|
|
|
{
|
|
|
|
|
Host = Dns.GetHostName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CatalogRoot Root = new CatalogRoot();
|
2021-06-02 15:16:14 -04:00
|
|
|
foreach (Type SchemaType in Program.ConfigSchemas)
|
2021-06-02 09:39:20 -04:00
|
|
|
{
|
2021-06-02 15:16:14 -04:00
|
|
|
JsonSchemaAttribute? SchemaAttribute = SchemaType.GetCustomAttribute<JsonSchemaAttribute>();
|
|
|
|
|
if (SchemaAttribute != null)
|
|
|
|
|
{
|
|
|
|
|
JsonSchemaCatalogAttribute? CatalogAttribute = SchemaType.GetCustomAttribute<JsonSchemaCatalogAttribute>();
|
|
|
|
|
if (CatalogAttribute != null)
|
|
|
|
|
{
|
|
|
|
|
Uri Url = new Uri($"https://{Host}/api/v1/schema/types/{SchemaType.Name}.json");
|
2021-06-04 17:51:30 -04:00
|
|
|
Root.Schemas.Add(new CatalogItem { Name = CatalogAttribute.Name, Description = CatalogAttribute.Description, FileMatch = CatalogAttribute.FileMatch, Url = Url });
|
2021-06-02 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-02 09:39:20 -04:00
|
|
|
}
|
|
|
|
|
return Ok(Root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a specific schema
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="TypeName">The type name</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api/v1/schema/types/{TypeName}.json")]
|
|
|
|
|
public ActionResult GetSchema(string TypeName)
|
|
|
|
|
{
|
2021-06-02 15:16:14 -04:00
|
|
|
foreach (Type SchemaType in Program.ConfigSchemas)
|
2021-06-02 09:39:20 -04:00
|
|
|
{
|
2021-06-02 15:16:14 -04:00
|
|
|
if (SchemaType.Name.Equals(TypeName, StringComparison.OrdinalIgnoreCase))
|
2021-06-02 09:39:20 -04:00
|
|
|
{
|
2021-06-02 15:16:14 -04:00
|
|
|
JsonSchema Schema = Schemas.CreateSchema(SchemaType);
|
|
|
|
|
|
|
|
|
|
using MemoryStream Stream = new MemoryStream();
|
2021-06-11 16:08:31 -04:00
|
|
|
Schema.Write(Stream);
|
2021-06-02 15:16:14 -04:00
|
|
|
|
|
|
|
|
return new FileContentResult(Stream.ToArray(), "application/json");
|
2021-06-02 09:39:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|