/
Program.cs
76 lines (67 loc) · 2.94 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Lantin.Model;
using MudBlazor.Services;
using MudBlazor;
using Lantin.Services;
using Microsoft.AspNetCore.Components.Authorization;
var builder = WebApplication.CreateBuilder(args);
Func<IServiceProvider, IFreeSql> freeSqlFactory = _ =>
{
IFreeSql freeSql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql,
$"Data Source={builder.Configuration.GetSection("MySql:Host").Value};" +
$"Port={builder.Configuration.GetSection("MySql:Port").Value};" +
$"User ID={builder.Configuration.GetSection("MySql:UserName").Value};" +
$"Password={builder.Configuration.GetSection("MySql:PassWord").Value}; " +
$"Initial Catalog={builder.Configuration.GetSection("MySql:DBName").Value};" +
$"Charset=utf8; SslMode=none;Min pool size=1")
.UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}")) //监听SQL语句
.Build();
return freeSql;
};
var webSiteConfigFactory = new WebSiteConfigService();
builder.Services.AddSingleton<IFreeSql>(freeSqlFactory);
builder.Services.AddSingleton<WebSiteConfigService>(webSiteConfigFactory);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddAuthenticationCore();
builder.Services.AddScoped<CustomAuthenticationStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(option =>
option.GetRequiredService<CustomAuthenticationStateProvider>());
builder.Services.AddMudServices(config =>
{
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopRight;
config.SnackbarConfiguration.PreventDuplicates = false;
config.SnackbarConfiguration.NewestOnTop = false;
config.SnackbarConfiguration.ShowCloseIcon = true;
config.SnackbarConfiguration.VisibleStateDuration = 2000;
config.SnackbarConfiguration.HideTransitionDuration = 100;
config.SnackbarConfiguration.ShowTransitionDuration = 100;
config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
});
builder.Services.AddMudMarkdownServices();
var app = builder.Build();
using (IServiceScope serviceScope = app.Services.CreateScope())
{
var freeSql = serviceScope.ServiceProvider.GetRequiredService<IFreeSql>();
// freeSql.CodeFirst.SyncStructure(typeof(Article));
// freeSql.CodeFirst.SyncStructure(typeof(Comment));
// freeSql.CodeFirst.SyncStructure(typeof(User));
// freeSql.CodeFirst.SyncStructure(typeof(Categorize));
// freeSql.CodeFirst.SyncStructure(typeof(Config));
// freeSql.CodeFirst.SyncStructure(typeof(UpLoad));
var webSiteConfig = serviceScope.ServiceProvider.GetRequiredService<WebSiteConfigService>();
webSiteConfig.Init(freeSql);
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();