diff --git a/Danmu.sln.DotSettings.user b/Danmu.sln.DotSettings.user index 8cf72f2..7d837ef 100644 --- a/Danmu.sln.DotSettings.user +++ b/Danmu.sln.DotSettings.user @@ -21,6 +21,7 @@ <Assembly Path="C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Private.CoreLib.dll" /> <Assembly Path="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.3\Microsoft.Extensions.Http.dll" /> </AssemblyExplorer> + True True True True diff --git a/Danmu/Danmu.csproj b/Danmu/Danmu.csproj index 0aa375d..7633f0e 100644 --- a/Danmu/Danmu.csproj +++ b/Danmu/Danmu.csproj @@ -50,7 +50,8 @@ - + + diff --git a/Danmu/Model/Config/KestrelSettings.cs b/Danmu/Model/Config/KestrelSettings.cs index 5019b96..0349cff 100644 --- a/Danmu/Model/Config/KestrelSettings.cs +++ b/Danmu/Model/Config/KestrelSettings.cs @@ -5,11 +5,11 @@ public class KestrelSettings /// /// 服务运行端口 /// - public int[] Port { get; set; } = {5001}; + public int[] Port { get; set; } /// /// UnixSocketPath /// public string[] UnixSocketPath { get; set; } } -} \ No newline at end of file +} diff --git a/Danmu/Model/DbContext/BaseContext.cs b/Danmu/Model/DbContext/BaseContext.cs index 122d7c7..6a4fcc6 100644 --- a/Danmu/Model/DbContext/BaseContext.cs +++ b/Danmu/Model/DbContext/BaseContext.cs @@ -1,10 +1,6 @@ -using System.IO; -using System.Text; -using System.Text.Json; using Danmu.Model.Config; +using Danmu.Utils.Configuration; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Debug; namespace Danmu.Model.DbContext { @@ -14,10 +10,7 @@ public class BaseContext : Microsoft.EntityFrameworkCore.DbContext public BaseContext(DbContextOptions options) : base(options) { - using var sr = new StreamReader("appsettings.json", Encoding.UTF8); - var s = sr.ReadToEnd(); - var settings = JsonSerializer.Deserialize(s); - Sql = settings.DanmuSql; + Sql = AppConfiguration.AppSettings.DanmuSql; } } } diff --git a/Danmu/Program.cs b/Danmu/Program.cs index fad53da..6e510dc 100644 --- a/Danmu/Program.cs +++ b/Danmu/Program.cs @@ -1,41 +1,48 @@ using System; -using System.IO; using System.Net; -using System.Text; -using System.Text.Json; using Danmu.Model.Config; using Danmu.Model.DbContext; using Danmu.Utils.Dao; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +#if LINUX +using System.IO; +#endif namespace Danmu { public class Program { - public static void Main(string[] args) - { - AppSettings appSettings; - using (var sr = new StreamReader("appsettings.json", Encoding.UTF8)) - { - var s = sr.ReadToEnd(); - appSettings = JsonSerializer.Deserialize(s); - } + private static AppSettings _appSettings = new AppSettings(); - var host = CreateHostBuilder(args, appSettings.KestrelSettings).Build(); - CreateDbIfNotExists(host, appSettings); + private static void Main(string[] args) + { + var host = CreateHostBuilder(args).Build(); + CreateDbIfNotExists(host); host.Run(); } - public static IHostBuilder CreateHostBuilder(string[] args, KestrelSettings ks) + private static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((context, builder) => + { + var env = context.HostingEnvironment; + builder + .AddJsonFile("appsettings.json", true, true) + .AddYamlFile("appsettings.yml", true, true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.yml", true); + _appSettings = builder.Build().Get(); + }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureKestrel(options => { + var ks = _appSettings.KestrelSettings; #if LINUX if (ks.UnixSocketPath.Length > 0) foreach (var path in ks.UnixSocketPath) @@ -51,14 +58,14 @@ public static IHostBuilder CreateHostBuilder(string[] args, KestrelSettings ks) }); } - private static void CreateDbIfNotExists(IHost host, AppSettings appSettings) + private static void CreateDbIfNotExists(IHost host) { using var scope = host.Services.CreateScope(); var services = scope.ServiceProvider; try { var context = services.GetRequiredService(); - DbInitializer.Initialize(context, appSettings); + DbInitializer.Initialize(context, _appSettings); } catch (Exception ex) { diff --git a/Danmu/Utils/Configuration/AppConfiguration.cs b/Danmu/Utils/Configuration/AppConfiguration.cs index 40cd0ba..48a041d 100644 --- a/Danmu/Utils/Configuration/AppConfiguration.cs +++ b/Danmu/Utils/Configuration/AppConfiguration.cs @@ -5,11 +5,13 @@ namespace Danmu.Utils.Configuration { public class AppConfiguration { + internal static AppSettings AppSettings; private readonly AppSettings _appConfiguration; public AppConfiguration(IConfiguration configuration) { _appConfiguration = new AppSettings(configuration); + AppSettings = _appConfiguration; } public AppSettings GetAppSetting() @@ -17,4 +19,4 @@ public AppSettings GetAppSetting() return _appConfiguration; } } -} \ No newline at end of file +} diff --git a/Danmu/appsettings.json b/Danmu/appsettings.json index 81ec6cb..601432b 100644 --- a/Danmu/appsettings.json +++ b/Danmu/appsettings.json @@ -1,41 +1,9 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Warning", "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.Hosting.Lifetime": "Warning" } - }, - "KestrelSettings": { - "Port": [ 5000 ], - "UnixSocketPath": [ "/tmp/danmu.sock" ] - }, - "WithOrigins": [ - "*" - ], - "LiveWithOrigins": [ - "http://localhost", - "http://localhost:4000", - "https://doc.video.xwhite.studio" - ], - "AdminWithOrigins": [ "http://localhost:5000" ], - "DanmuSql": { - "Sql": 0, - "Host": "127.0.0.1", - "Port": 0, - "UserName": "danmaku", - "PassWord": "danmaku", - "DataBase": "danmaku", - "PoolSize": 16 - }, - "Admin": { - "User": "MonoLogueChi", - "Password": "000000", - "MaxAge": 60 - }, - "BiliBiliSetting": { - "Cookie": "", - "CidCacheTime": 168, - "DanmuCacheTime": 12 } } \ No newline at end of file diff --git a/Danmu/appsettings.yml b/Danmu/appsettings.yml new file mode 100644 index 0000000..473d2d4 --- /dev/null +++ b/Danmu/appsettings.yml @@ -0,0 +1,29 @@ +KestrelSettings: + Port: + - 5000 + UnixSocketPath: + - "/tmp/danmu.sock" +WithOrigins: + - "*" +LiveWithOrigins: + - http://localhost + - http://localhost:4000 + - https://doc.video.xwhite.studio +AdminWithOrigins: + - http://localhost:5000 +DanmuSql: + Sql: 0 + Host: 127.0.0.1 + Port: 0 + UserName: danmaku + PassWord: danmaku + DataBase: danmaku + PoolSize: 16 +Admin: + User: MonoLogueChi + Password: "000000" + MaxAge: 60 +BiliBiliSetting: + Cookie: "" + CidCacheTime: 168 + DanmuCacheTime: 12