forked from ddrilling/AsbCloudServer
Add ProtobufController
This commit is contained in:
parent
e99ac84c46
commit
9ae268f4c5
87
AsbCloudWebApi/Controllers/ProtobufController.cs
Normal file
87
AsbCloudWebApi/Controllers/ProtobufController.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ProtoBuf.Meta;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// контроллер схем ProtoBuf
|
||||
/// </summary>
|
||||
[Route("api/proto")]
|
||||
[ApiController]
|
||||
public class ProtobufController : ControllerBase
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// названия доступных типов
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("types")]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public IActionResult GetTypes()
|
||||
{
|
||||
var typesNames = GetMetaTypes()
|
||||
.Select(t => t.Type?.Name);
|
||||
return Ok(typesNames);
|
||||
}
|
||||
|
||||
/// <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");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// .proto файл со всеми доступными типами
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("schema/{typeName}")]
|
||||
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public IActionResult GetSchema(string typeName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(typeName))
|
||||
return BadRequest("require typeName");
|
||||
|
||||
var type = RuntimeTypeModel.Default
|
||||
.GetTypes()
|
||||
.OfType<MetaType>()
|
||||
.FirstOrDefault(t => t.Type.Name.Equals(typeName, System.StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (type is null)
|
||||
return NoContent();
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user