From 965caa2fcebfd2def81f8d0ff34391a190bab76e Mon Sep 17 00:00:00 2001 From: Opportunity Date: Tue, 19 Nov 2019 14:10:57 +0800 Subject: [PATCH] update to dotnet 3.0 --- .gitignore | 2 + .../Properties/launchSettings.json | 2 +- EhTagApi/Controllers/DatabaseController.cs | 4 +- EhTagApi/EhTagApi.csproj | 1 + EhTagApi/Formatters/AstOutputFormatter.cs | 4 +- EhTagApi/Formatters/FullOutputFormatter.cs | 16 +++++++ EhTagApi/Formatters/HtmlOutputFormatter.cs | 4 +- EhTagApi/Formatters/RawOutputFormatter.cs | 6 +-- EhTagApi/Formatters/TextFormatter.cs | 7 ++- EhTagApi/Formatters/TextOutputFormatter.cs | 4 +- EhTagApi/Startup.cs | 47 ++++++++++--------- EhTagApi/appsettings.json | 6 ++- EhTagClient/Consts.cs | 2 +- EhTagClient/FrontMatters.cs | 17 +++++++ EhTagClient/RecordDictionary.cs | 19 ++++---- 15 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 EhTagApi/Formatters/FullOutputFormatter.cs create mode 100644 EhTagClient/FrontMatters.cs diff --git a/.gitignore b/.gitignore index be62c92..0f5417f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/EhDbReleaseBuilder/Properties/launchSettings.json b/EhDbReleaseBuilder/Properties/launchSettings.json index 8f4551a..71697b7 100644 --- a/EhDbReleaseBuilder/Properties/launchSettings.json +++ b/EhDbReleaseBuilder/Properties/launchSettings.json @@ -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" } } } \ No newline at end of file diff --git a/EhTagApi/Controllers/DatabaseController.cs b/EhTagApi/Controllers/DatabaseController.cs index 69463c6..40838d3 100644 --- a/EhTagApi/Controllers/DatabaseController.cs +++ b/EhTagApi/Controllers/DatabaseController.cs @@ -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 }), }; } @@ -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}")] diff --git a/EhTagApi/EhTagApi.csproj b/EhTagApi/EhTagApi.csproj index 0e3ad9e..7bd1e86 100644 --- a/EhTagApi/EhTagApi.csproj +++ b/EhTagApi/EhTagApi.csproj @@ -6,6 +6,7 @@ + diff --git a/EhTagApi/Formatters/AstOutputFormatter.cs b/EhTagApi/Formatters/AstOutputFormatter.cs index 66acf73..b874fa6 100644 --- a/EhTagApi/Formatters/AstOutputFormatter.cs +++ b/EhTagApi/Formatters/AstOutputFormatter.cs @@ -9,9 +9,9 @@ namespace EhTagApi.Formatters { - public class AstOutputFormatter : JsonOutputFormatter + public class AstOutputFormatter : NewtonsoftJsonOutputFormatter { - public AstOutputFormatter() : base(Consts.SerializerSettings, ArrayPool.Shared) + public AstOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool.Shared, mvcOptions) { SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Ast)); SupportedMediaTypes.Clear(); diff --git a/EhTagApi/Formatters/FullOutputFormatter.cs b/EhTagApi/Formatters/FullOutputFormatter.cs new file mode 100644 index 0000000..b3258ab --- /dev/null +++ b/EhTagApi/Formatters/FullOutputFormatter.cs @@ -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.Shared, mvcOptions) + { + SupportedMediaTypes.Clear(); + SupportedMediaTypes.Add("application/json"); + SupportedMediaTypes.Add("application/problem+json"); + } + } +} diff --git a/EhTagApi/Formatters/HtmlOutputFormatter.cs b/EhTagApi/Formatters/HtmlOutputFormatter.cs index c4bd30b..599a713 100644 --- a/EhTagApi/Formatters/HtmlOutputFormatter.cs +++ b/EhTagApi/Formatters/HtmlOutputFormatter.cs @@ -4,9 +4,9 @@ namespace EhTagApi.Formatters { - public class HtmlOutputFormatter : JsonOutputFormatter + public class HtmlOutputFormatter : NewtonsoftJsonOutputFormatter { - public HtmlOutputFormatter() : base(Consts.SerializerSettings, ArrayPool.Shared) + public HtmlOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool.Shared, mvcOptions) { SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Html)); SupportedMediaTypes.Clear(); diff --git a/EhTagApi/Formatters/RawOutputFormatter.cs b/EhTagApi/Formatters/RawOutputFormatter.cs index be103f8..ff631d2 100644 --- a/EhTagApi/Formatters/RawOutputFormatter.cs +++ b/EhTagApi/Formatters/RawOutputFormatter.cs @@ -4,15 +4,13 @@ namespace EhTagApi.Formatters { - public class RawOutputFormatter : JsonOutputFormatter + public class RawOutputFormatter : NewtonsoftJsonOutputFormatter { - public RawOutputFormatter() : base(Consts.SerializerSettings, ArrayPool.Shared) + public RawOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool.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); } } diff --git a/EhTagApi/Formatters/TextFormatter.cs b/EhTagApi/Formatters/TextFormatter.cs index e3dd6af..b4f3b96 100644 --- a/EhTagApi/Formatters/TextFormatter.cs +++ b/EhTagApi/Formatters/TextFormatter.cs @@ -22,10 +22,9 @@ public override async Task 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); } } } diff --git a/EhTagApi/Formatters/TextOutputFormatter.cs b/EhTagApi/Formatters/TextOutputFormatter.cs index c3f46f9..292c419 100644 --- a/EhTagApi/Formatters/TextOutputFormatter.cs +++ b/EhTagApi/Formatters/TextOutputFormatter.cs @@ -4,9 +4,9 @@ namespace EhTagApi.Formatters { - public class TextOutputFormatter : JsonOutputFormatter + public class TextOutputFormatter : NewtonsoftJsonOutputFormatter { - public TextOutputFormatter() : base(Consts.SerializerSettings, ArrayPool.Shared) + public TextOutputFormatter(Microsoft.AspNetCore.Mvc.MvcOptions mvcOptions) : base(Consts.SerializerSettings, ArrayPool.Shared, mvcOptions) { SerializerSettings.Converters.Add(new MdConverter(MdConverter.ConvertType.Text)); SupportedMediaTypes.Clear(); diff --git a/EhTagApi/Startup.cs b/EhTagApi/Startup.cs index 8d00963..be57857 100644 --- a/EhTagApi/Startup.cs +++ b/EhTagApi/Startup.cs @@ -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; @@ -53,18 +54,24 @@ public void ConfigureServices(IServiceCollection services) .Build()); }); - services.AddMvc(options => + services.AddControllers(options => { - var defOutput = options.OutputFormatters.OfType().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 => { @@ -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()) { @@ -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(); + }); } } } diff --git a/EhTagApi/appsettings.json b/EhTagApi/appsettings.json index b7c4ed9..84563e5 100644 --- a/EhTagApi/appsettings.json +++ b/EhTagApi/appsettings.json @@ -1,8 +1,10 @@ { "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" -} +} \ No newline at end of file diff --git a/EhTagClient/Consts.cs b/EhTagClient/Consts.cs index 7418bda..35eef5e 100644 --- a/EhTagClient/Consts.cs +++ b/EhTagClient/Consts.cs @@ -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"; diff --git a/EhTagClient/FrontMatters.cs b/EhTagClient/FrontMatters.cs new file mode 100644 index 0000000..cfe50f2 --- /dev/null +++ b/EhTagClient/FrontMatters.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using YamlDotNet.Serialization; + +namespace EhTagClient +{ + public class FrontMatters : Dictionary + { + private readonly static IDeserializer _Deserializer = new DeserializerBuilder() + //.WithNamingConvention(YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention.Instance) + .Build(); + + public static FrontMatters Parse(string value) + { + return _Deserializer.Deserialize(value); + } + } +} diff --git a/EhTagClient/RecordDictionary.cs b/EhTagClient/RecordDictionary.cs index 9bf9446..07204ae 100644 --- a/EhTagClient/RecordDictionary.cs +++ b/EhTagClient/RecordDictionary.cs @@ -22,7 +22,7 @@ public RecordDictionary(Namespace ns, RepoClient repoClient) public Namespace Namespace { get; } - public IDictionary FrontMatters { get; set; } + public FrontMatters FrontMatters { get; set; } [JsonIgnore] public string Prefix { get; set; } @@ -176,6 +176,7 @@ public void Load() Prefix = prefix.ToString(); Suffix = suffix.ToString(); + FrontMatters = FrontMatters.Parse(frontMatters.ToString()); MapData.Clear(); @@ -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)