2024-07-04 11:02:45 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-06-07 11:14:26 +05:00
|
|
|
|
using ProtoBuf.Meta;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Схемы ProtoBuf
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("api/proto")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ProtobufController : ControllerBase
|
2022-06-07 11:14:26 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
private IEnumerable<MetaType> GetMetaTypes()
|
|
|
|
|
{
|
|
|
|
|
var metaTypes = RuntimeTypeModel.Default
|
|
|
|
|
.GetTypes()
|
|
|
|
|
.OfType<MetaType>();
|
|
|
|
|
var fileteredMetaTypes = metaTypes
|
|
|
|
|
.Where(t => t.Type.MemberType == System.Reflection.MemberTypes.TypeInfo)
|
|
|
|
|
.Where(t => !t.Type.IsGenericType)
|
|
|
|
|
.Where(t => !t.Type.IsArray);
|
|
|
|
|
return fileteredMetaTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 11:14:26 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// названия доступных типов
|
2022-06-07 11:14:26 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("types")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public IActionResult GetTypes()
|
2022-06-07 11:14:26 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var typesNames = GetMetaTypes()
|
|
|
|
|
.Select(t => t.Type?.Name);
|
|
|
|
|
return Ok(typesNames);
|
|
|
|
|
}
|
2022-06-07 11:14:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// .proto файл со всеми доступными типами
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("schema")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public IActionResult GetSchema()
|
|
|
|
|
{
|
|
|
|
|
var types = GetMetaTypes().Select(mt => mt.Type);
|
|
|
|
|
SchemaGenerationOptions opts = new() { };
|
|
|
|
|
opts.Types.AddRange(types);
|
|
|
|
|
var protoFileContentString = ProtoBuf.Serializer.GetProto(opts);
|
|
|
|
|
var protoFileContentBytes = System.Text.Encoding.UTF8.GetBytes(protoFileContentString);
|
|
|
|
|
return File(protoFileContentBytes, "text/plain", "types.proto");
|
|
|
|
|
}
|
2022-06-07 11:14:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// .proto файл со всеми доступными типами
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("schema/{typeName}")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
|
|
|
|
public IActionResult GetSchema(string typeName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(typeName))
|
|
|
|
|
return this.ValidationBadRequest(nameof(typeName), "require typeName");
|
2022-06-07 11:14:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var type = RuntimeTypeModel.Default
|
|
|
|
|
.GetTypes()
|
|
|
|
|
.OfType<MetaType>()
|
|
|
|
|
.FirstOrDefault(t => t.Type.Name.Equals(typeName, System.StringComparison.InvariantCultureIgnoreCase));
|
2022-06-07 11:14:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (type is null)
|
|
|
|
|
return NoContent();
|
2022-06-07 11:14:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
SchemaGenerationOptions opts = new() { };
|
|
|
|
|
opts.Types.Add(type.Type);
|
|
|
|
|
var protoFileContentString = ProtoBuf.Serializer.GetProto(opts);
|
|
|
|
|
var protoFileContentBytes = System.Text.Encoding.UTF8.GetBytes(protoFileContentString);
|
|
|
|
|
return File(protoFileContentBytes, "text/plain", $"{typeName}.proto");
|
2022-06-07 11:14:26 +05:00
|
|
|
|
}
|
|
|
|
|
}
|