~furry/mamoru-server

7b286a50ece64e6f00b4fb458a192ad7a795b115 — nora 2 months ago ba92f07
use yaml instead of json for rest
3 files changed, 9 insertions(+), 15 deletions(-)

M Mamoru.csproj
M Routes/StatusRoute.cs
M Utils/Response.cs
M Mamoru.csproj => Mamoru.csproj +0 -1
@@ 10,7 10,6 @@

    <ItemGroup>
        <PackageReference Include="EXILED" Version="9.0.0-beta.4" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="WebSocketSharp" Version="1.0.3-rc11"/>
    </ItemGroup>


M Routes/StatusRoute.cs => Routes/StatusRoute.cs +1 -1
@@ 16,6 16,6 @@ public class StatusRoute : AbstractRoute {
      Assembly.GetAssembly(typeof(Exiled.Loader.Loader)).GetName().Version.ToString(),
      Assembly.GetExecutingAssembly().GetName().Version.ToString()
    );
    JsonResponse.Create(ref res, status);
    YamlResponse.Create(ref res, status);
  }
}
\ No newline at end of file

M Utils/Response.cs => Utils/Response.cs +8 -13
@@ 1,25 1,20 @@
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using WebSocketSharp.Net;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace Mamoru.Utils;

public static class JsonResponse {
  private static readonly IContractResolver ContractResolver = new DefaultContractResolver {
    NamingStrategy = new SnakeCaseNamingStrategy(),
  };

  private static readonly JsonSerializerSettings Settings = new() {
    ContractResolver = ContractResolver,
    Formatting = Formatting.None
  };
public static class YamlResponse {
  private static readonly ISerializer Serializer = new SerializerBuilder()
    .WithNamingConvention(UnderscoredNamingConvention.Instance)
    .Build();
  
  public static void Create(ref HttpListenerResponse res, object value, HttpStatusCode statusCode = HttpStatusCode.OK) {
    var serialized = JsonConvert.SerializeObject(value, Settings);
    var serialized = Serializer.Serialize(value);
    var data = Utf8.GetBytes(serialized);
    res.StatusCode = (int)statusCode;
    res.ContentType = "application/json";
    res.ContentType = "application/yaml";
    res.ContentEncoding = Encoding.UTF8;
    res.ContentLength64 = data.LongLength;
    res.Close(data, false);