Skip to content

Commit

Permalink
添加yaml配置文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
MonoLogueChi committed May 9, 2020
1 parent ecac815 commit 413e15f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 63 deletions.
1 change: 1 addition & 0 deletions Danmu.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
&lt;/AssemblyExplorer&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=artplayer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bili/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bilibili/@EntryIndexedValue">True</s:Boolean>
Expand Down
3 changes: 2 additions & 1 deletion Danmu/Danmu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="2.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
<PackageReference Include="VueCliMiddleware" Version="3.1.1" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Danmu/Model/Config/KestrelSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public class KestrelSettings
/// <summary>
/// 服务运行端口
/// </summary>
public int[] Port { get; set; } = {5001};
public int[] Port { get; set; }

/// <summary>
/// UnixSocketPath
/// </summary>
public string[] UnixSocketPath { get; set; }
}
}
}
11 changes: 2 additions & 9 deletions Danmu/Model/DbContext/BaseContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<AppSettings>(s);
Sql = settings.DanmuSql;
Sql = AppConfiguration.AppSettings.DanmuSql;
}
}
}
39 changes: 23 additions & 16 deletions Danmu/Program.cs
Original file line number Diff line number Diff line change
@@ -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<AppSettings>(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<AppSettings>();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
var ks = _appSettings.KestrelSettings;
#if LINUX
if (ks.UnixSocketPath.Length > 0)
foreach (var path in ks.UnixSocketPath)
Expand All @@ -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<DanmuContext>();
DbInitializer.Initialize(context, appSettings);
DbInitializer.Initialize(context, _appSettings);
}
catch (Exception ex)
{
Expand Down
4 changes: 3 additions & 1 deletion Danmu/Utils/Configuration/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ 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()
{
return _appConfiguration;
}
}
}
}
36 changes: 2 additions & 34 deletions Danmu/appsettings.json
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 29 additions & 0 deletions Danmu/appsettings.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 413e15f

Please sign in to comment.