Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
update to dotnet 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Nov 19, 2019
1 parent 82b80a1 commit 965caa2
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

/EhTT-DB/

# User-specific files
*.suo
*.user
Expand Down
2 changes: 1 addition & 1 deletion EhDbReleaseBuilder/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"EhDbReleaseBuilder": {
"commandName": "Project",
"commandLineArgs": "C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\EhTagConnector\\EhTagApi\\Db\\ C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\EhTagConnector\\EhTagApi\\Db\\publish"
"commandLineArgs": "C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\EhTagConnector\\EhTT-DB\\ C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\EhTagConnector\\EhTT-DB\\publish"
}
}
}
4 changes: 2 additions & 2 deletions EhTagApi/Controllers/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public object Get()
head.Message,
},
Version = _Database.GetVersion(),
Data = _Database.Values.Select(v => new { v.Namespace, v.Count }),
Data = _Database.Values.Select(v => new { v.Namespace, v.Count, v.FrontMatters }),
};
}

Expand Down Expand Up @@ -123,7 +123,7 @@ public object GetBadgeNs()
public object Get([SingleNamespace] Namespace @namespace)
{
var dic = _Database[@namespace];
return new { dic.Namespace, dic.Count };
return new { dic.Namespace, dic.Count, dic.FrontMatters };
}

[HttpHead("{namespace}/{raw}")]
Expand Down
1 change: 1 addition & 0 deletions EhTagApi/EhTagApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Octokit" Version="0.36.0" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions EhTagApi/Formatters/AstOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace EhTagApi.Formatters
{
public class AstOutputFormatter : JsonOutputFormatter
public class AstOutputFormatter : NewtonsoftJsonOutputFormatter
{
public AstOutputFormatter() : base(Consts.SerializerSettings, ArrayPool<char>.Shared)
public AstOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool<char>.Shared, mvcOptions)
{
SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Ast));
SupportedMediaTypes.Clear();
Expand Down
16 changes: 16 additions & 0 deletions EhTagApi/Formatters/FullOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using EhTagClient;
using Microsoft.AspNetCore.Mvc.Formatters;
using System.Buffers;

namespace EhTagApi.Formatters
{
public class FullOutputFormatter : NewtonsoftJsonOutputFormatter
{
public FullOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool<char>.Shared, mvcOptions)
{
SupportedMediaTypes.Clear();
SupportedMediaTypes.Add("application/json");
SupportedMediaTypes.Add("application/problem+json");
}
}
}
4 changes: 2 additions & 2 deletions EhTagApi/Formatters/HtmlOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace EhTagApi.Formatters
{
public class HtmlOutputFormatter : JsonOutputFormatter
public class HtmlOutputFormatter : NewtonsoftJsonOutputFormatter
{
public HtmlOutputFormatter() : base(Consts.SerializerSettings, ArrayPool<char>.Shared)
public HtmlOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool<char>.Shared, mvcOptions)
{
SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Html));
SupportedMediaTypes.Clear();
Expand Down
6 changes: 2 additions & 4 deletions EhTagApi/Formatters/RawOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace EhTagApi.Formatters
{
public class RawOutputFormatter : JsonOutputFormatter
public class RawOutputFormatter : NewtonsoftJsonOutputFormatter
{
public RawOutputFormatter() : base(Consts.SerializerSettings, ArrayPool<char>.Shared)
public RawOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool<char>.Shared, mvcOptions)
{
SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Raw));
SupportedMediaTypes.Clear();
SupportedMediaTypes.Add("application/raw+json");
}

public override bool CanWriteResult(OutputFormatterCanWriteContext context) => base.CanWriteResult(context);
}
}
7 changes: 3 additions & 4 deletions EhTagApi/Formatters/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
{
var request = context.HttpContext.Request;
Microsoft.Net.Http.Headers.MediaTypeHeaderValue.TryParse(request.ContentType, out var content);
using (var reader = context.ReaderFactory(request.Body, content?.Encoding ?? Encoding.UTF8))
{
return await InputFormatterResult.SuccessAsync(await reader.ReadToEndAsync());
}
using var reader = new StreamReader(request.Body, content?.Encoding ?? Encoding.UTF8);
var data = await reader.ReadToEndAsync();
return await InputFormatterResult.SuccessAsync(data);
}
}
}
4 changes: 2 additions & 2 deletions EhTagApi/Formatters/TextOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace EhTagApi.Formatters
{
public class TextOutputFormatter : JsonOutputFormatter
public class TextOutputFormatter : NewtonsoftJsonOutputFormatter
{
public TextOutputFormatter() : base(Consts.SerializerSettings, ArrayPool<char>.Shared)
public TextOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool<char>.Shared, mvcOptions)
{
SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Text));
SupportedMediaTypes.Clear();
Expand Down
47 changes: 24 additions & 23 deletions EhTagApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
Expand Down Expand Up @@ -53,18 +54,24 @@ public void ConfigureServices(IServiceCollection services)
.Build());
});

services.AddMvc(options =>
services.AddControllers(options =>
{
var defOutput = options.OutputFormatters.OfType<Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter>().First();
defOutput.SupportedMediaTypes.Clear();
defOutput.SupportedMediaTypes.Add("application/json");
defOutput.SupportedMediaTypes.Add("application/problem+json");
options.OutputFormatters.Add(new Formatters.RawOutputFormatter());
options.OutputFormatters.Add(new Formatters.TextOutputFormatter());
options.OutputFormatters.Add(new Formatters.HtmlOutputFormatter());
options.OutputFormatters.Add(new Formatters.AstOutputFormatter());
options.OutputFormatters.Add(new Formatters.FullOutputFormatter(options));
options.OutputFormatters.Add(new Formatters.RawOutputFormatter(options));
options.OutputFormatters.Add(new Formatters.TextOutputFormatter(options));
options.OutputFormatters.Add(new Formatters.HtmlOutputFormatter(options));
options.OutputFormatters.Add(new Formatters.AstOutputFormatter(options));

options.InputFormatters.Add(new Formatters.TextFormatter());
}).AddNewtonsoftJson(options =>
{
var serializer = Newtonsoft.Json.JsonSerializer.Create(new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All,
});
var tw = new StringWriter();
serializer.Serialize(tw, Consts.SerializerSettings);
serializer.Populate(new StringReader(tw.ToString()), options.SerializerSettings);
})
.AddFormatterMappings(options =>
{
Expand All @@ -74,21 +81,11 @@ public void ConfigureServices(IServiceCollection services)
options.SetMediaTypeMappingForFormat("ast.json", "application/ast+json");
})
.AddXmlSerializerFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
var serializer = Newtonsoft.Json.JsonSerializer.Create(new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All,
});
var tw = new StringWriter();
serializer.Serialize(tw, Consts.SerializerSettings);
serializer.Populate(new StringReader(tw.ToString()), options.SerializerSettings);
});
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand All @@ -97,10 +94,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
else
{
}

app.UseHttpsRedirection();
app.UseCors();
app.UseRouting();
app.UseResponseCompression();
app.UseMvc();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
6 changes: 4 additions & 2 deletions EhTagApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
}
2 changes: 1 addition & 1 deletion EhTagClient/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace EhTagClient
{
public static class Consts
{
internal const string REPO_PATH = "./Db";
internal const string REPO_PATH = "../EhTT-DB";
internal const string OWNER = "EhTagTranslation";
internal const string REPO = "Database";

Expand Down
17 changes: 17 additions & 0 deletions EhTagClient/FrontMatters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using YamlDotNet.Serialization;

namespace EhTagClient
{
public class FrontMatters : Dictionary<string, object>
{
private readonly static IDeserializer _Deserializer = new DeserializerBuilder()
//.WithNamingConvention(YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention.Instance)
.Build();

public static FrontMatters Parse(string value)
{
return _Deserializer.Deserialize<FrontMatters>(value);
}
}
}
19 changes: 9 additions & 10 deletions EhTagClient/RecordDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public RecordDictionary(Namespace ns, RepoClient repoClient)

public Namespace Namespace { get; }

public IDictionary<string, object> FrontMatters { get; set; }
public FrontMatters FrontMatters { get; set; }

[JsonIgnore]
public string Prefix { get; set; }
Expand Down Expand Up @@ -176,6 +176,7 @@ public void Load()

Prefix = prefix.ToString();
Suffix = suffix.ToString();
FrontMatters = FrontMatters.Parse(frontMatters.ToString());

MapData.Clear();

Expand All @@ -190,17 +191,15 @@ public void Load()

public void Save()
{
using (var sw = new StreamWriter(FilePath))
using var sw = new StreamWriter(FilePath);
sw.Write(Prefix);
foreach (var item in RawData)
{
sw.Write(Prefix);
foreach (var item in RawData)
{
if (item.Key is null)
continue;
sw.WriteLine(item.Value.ToString(item.Key));
}
sw.Write(Suffix);
if (item.Key is null)
continue;
sw.WriteLine(item.Value.ToString(item.Key));
}
sw.Write(Suffix);
}

public Record Find(string key)
Expand Down

0 comments on commit 965caa2

Please sign in to comment.