// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Text.Json.Nodes; using HordeServer.Server; using HordeServer.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace HordeServer.Parameters { /// /// Controller for the /api/v1/parameters endpoint. Provides configuration data to other tools. /// [ApiController] [AllowAnonymous] public class ParametersController : HordeControllerBase { private readonly IOptionsSnapshot _globalConfig; /// /// Constructor /// public ParametersController(IOptionsSnapshot globalConfig) { _globalConfig = globalConfig; } /// /// Query side-wide parameters published for automatic configuration of external tools. /// /// Base path for the object to return /// Filter for the properties to return /// Parameters matching the requested filter [HttpGet] [Route("/api/v1/parameters/{*path}")] [ProducesResponseType(typeof(object), 200)] public ActionResult GetParameters(string? path = null, [FromQuery] PropertyFilter? filter = null) { JsonObject? parameters = _globalConfig.Value.Parameters; if (parameters != null && !String.IsNullOrEmpty(path)) { foreach (string fragment in path.Split('/')) { parameters = parameters[fragment] as JsonObject; if (parameters == null) { break; } } } return PropertyFilter.Apply(parameters ?? new JsonObject(), filter); } } }