mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 22:26:56 +08:00
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using MegghysAPI.Attributes;
|
|
using MegghysAPI.Components;
|
|
using Microsoft.FluentUI.AspNetCore.Components;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
builder.Services.AddFluentUIComponents();
|
|
builder.Services.AddControllers();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAntiforgery();
|
|
|
|
app.MapStaticAssets();
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAntiforgery();
|
|
|
|
app.MapControllers();
|
|
|
|
// 加载所有有 AutoInitAttribute 的类
|
|
// 获取当前Assembly
|
|
var inits = new List<MethodInfo>();
|
|
Assembly.GetExecutingAssembly()
|
|
.GetTypes()
|
|
.ForEach(t => t.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
|
|
.Where(m => m.GetCustomAttribute<AutoInitAttribute>() is { }).ForEach(m => inits.Add(m)));
|
|
inits = [.. inits.OrderBy(m => m.GetCustomAttribute<AutoInitAttribute>().Order)];
|
|
inits.ForEach(m =>
|
|
{
|
|
var sw = new Stopwatch();
|
|
sw.Start();
|
|
var attr = m.GetCustomAttribute<AutoInitAttribute>();
|
|
if (attr.LogMessage is not null)
|
|
Logs.Info(attr.LogMessage);
|
|
if (attr.Async)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
m.Invoke(null, null);
|
|
attr.PostInit?.Invoke();
|
|
Logs.Info($"[{sw.ElapsedMilliseconds} ms] Async <{m.DeclaringType.Name}.{m.Name}> => Inited.");
|
|
});
|
|
}
|
|
else
|
|
{
|
|
m.Invoke(null, null);
|
|
attr.PostInit?.Invoke();
|
|
Logs.Info($"[{sw.ElapsedMilliseconds} ms] <{m.DeclaringType.Name}.{m.Name}> => Inited.");
|
|
}
|
|
});
|
|
|
|
app.Run();
|