2024-10-09 14:45:09 +08:00
|
|
|
|
using System.Reflection;
|
2024-10-16 11:24:10 +08:00
|
|
|
|
using LFlow.Base.Interfaces;
|
|
|
|
|
|
using LFlow.Base.Utils;
|
2024-10-09 14:45:09 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Npgsql.Replication;
|
2024-10-10 17:05:35 +08:00
|
|
|
|
using Serilog;
|
|
|
|
|
|
using Serilog.Events;
|
2024-10-09 14:45:09 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LFlow.Base;
|
|
|
|
|
|
public static class Program
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
|
{
|
2024-10-10 17:05:35 +08:00
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
|
.CreateBootstrapLogger();
|
2024-10-09 14:45:09 +08:00
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2024-10-10 17:05:35 +08:00
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSerilog((services, lc) => lc
|
|
|
|
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
|
|
|
|
.ReadFrom.Services(services)
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
// .WriteTo.Console()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Log.Logger.Information("LoadSubService");
|
2024-10-09 14:45:09 +08:00
|
|
|
|
builder.Services.LoadSubService();
|
2024-10-10 17:05:35 +08:00
|
|
|
|
|
2024-10-09 14:45:09 +08:00
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
2024-10-10 17:05:35 +08:00
|
|
|
|
|
|
|
|
|
|
Log.Logger.Information("ConfigureSqlSugar");
|
2024-10-09 14:45:09 +08:00
|
|
|
|
builder.Services.ConfigureSqlSugar();
|
2024-10-10 17:05:35 +08:00
|
|
|
|
|
2024-10-09 14:45:09 +08:00
|
|
|
|
builder.Services.AddControllers();
|
2024-10-10 17:05:35 +08:00
|
|
|
|
|
2024-10-09 17:08:15 +08:00
|
|
|
|
builder.Services.AddSwaggerGen(u =>
|
|
|
|
|
|
{
|
|
|
|
|
|
u.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
Version = "Ver.1",
|
|
|
|
|
|
Title = "LFlow",
|
|
|
|
|
|
Description = "LFlow api test and document",
|
|
|
|
|
|
Contact = new Microsoft.OpenApi.Models.OpenApiContact
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "ling",
|
|
|
|
|
|
Email = "noemail@ling.chat"
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2024-10-09 14:45:09 +08:00
|
|
|
|
|
2024-10-16 11:24:49 +08:00
|
|
|
|
builder.Host.UseSerilog((context, services, config) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
config.ReadFrom.Configuration(context.Configuration)
|
|
|
|
|
|
.ReadFrom.Services(services)
|
|
|
|
|
|
.Enrich.FromLogContext();
|
|
|
|
|
|
// .WriteTo.Console();
|
|
|
|
|
|
}, true);
|
2024-10-10 17:05:35 +08:00
|
|
|
|
// init App.service
|
|
|
|
|
|
App.services = builder.Services;
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
app.UseSerilogRequestLogging(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Information;
|
|
|
|
|
|
});
|
2024-10-09 14:45:09 +08:00
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
// app.MapGet("/", () => "Hello World!");
|
|
|
|
|
|
// if (app.Environment.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
app.UseSwagger();
|
2024-10-09 17:08:15 +08:00
|
|
|
|
app.UseSwaggerUI(u =>
|
|
|
|
|
|
{
|
|
|
|
|
|
u.DocumentTitle = "LFlow";
|
|
|
|
|
|
});
|
2024-10-09 14:45:09 +08:00
|
|
|
|
}
|
2024-10-16 11:24:10 +08:00
|
|
|
|
// 在启动后调用Sqlsugar进行CodeFirst
|
|
|
|
|
|
// 挂载启动后事件
|
|
|
|
|
|
app.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStarted.Register(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Logger.Information("ApplicationStarted");
|
|
|
|
|
|
CodeFirst.InitTable();
|
|
|
|
|
|
});
|
2024-10-09 14:45:09 +08:00
|
|
|
|
app.Run();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void ConfigureSqlSugar(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<ISqlSugarClient>(s =>
|
|
|
|
|
|
{
|
|
|
|
|
|
SqlSugarScope sqlSugar = new(new ConnectionConfig()
|
|
|
|
|
|
{
|
|
|
|
|
|
DbType = SqlSugar.DbType.Sqlite,
|
2024-10-10 17:05:35 +08:00
|
|
|
|
ConnectionString = "DataSource=LFlow-dev.db",
|
2024-10-09 14:45:09 +08:00
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
|
},
|
2024-10-16 11:24:10 +08:00
|
|
|
|
db =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//单例参数配置,所有上下文生效
|
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//获取作IOC作用域对象
|
|
|
|
|
|
var appServive = s.GetService<IHttpContextAccessor>();
|
|
|
|
|
|
// var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
|
|
|
|
|
|
// Console.WriteLine("AOP" + obj.GetHashCode());
|
|
|
|
|
|
Console.WriteLine($"{appServive?.HttpContext?.Request.Path}:{sql}\r\n{pars}");
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
sqlSugar.DbMaintenance.CreateDatabase();
|
|
|
|
|
|
|
2024-10-09 14:45:09 +08:00
|
|
|
|
return sqlSugar;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从子文件夹中加载DLL
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void LoadSubService(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = Path.Combine(AppContext.BaseDirectory, "Services");
|
|
|
|
|
|
// Console.WriteLine(path);
|
|
|
|
|
|
var files = Directory.GetFiles(path, "*.dll");
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
{
|
2024-10-10 17:05:35 +08:00
|
|
|
|
Log.Logger.Information($"Load file -> {file}...");
|
2024-10-09 14:45:09 +08:00
|
|
|
|
var assembly = Assembly.LoadFile(file);
|
|
|
|
|
|
var types = assembly.GetTypes();
|
2024-10-16 11:24:10 +08:00
|
|
|
|
// bool isUseController = false;
|
2024-10-09 14:45:09 +08:00
|
|
|
|
foreach (var type in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Console.WriteLine(type);
|
|
|
|
|
|
if (type.IsClass && !type.IsAbstract)
|
|
|
|
|
|
{
|
|
|
|
|
|
var interfaces = type.GetInterfaces();
|
2024-10-16 11:24:10 +08:00
|
|
|
|
if (interfaces.Contains(typeof(IModule)))
|
2024-10-09 14:45:09 +08:00
|
|
|
|
{
|
2024-10-16 11:24:10 +08:00
|
|
|
|
Log.Logger.Information($"\tFound IModule -> {type.FullName}");
|
|
|
|
|
|
var module = Activator.CreateInstance(type) as IModule;
|
|
|
|
|
|
module.ConfigureModule(services);
|
2024-10-09 14:45:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-16 11:24:10 +08:00
|
|
|
|
// if (isUseController)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Log.Logger.Information($"\tAdd Controllers for {assembly.FullName}");
|
|
|
|
|
|
// // 添加Controller
|
|
|
|
|
|
// services.AddControllers().AddApplicationPart(assembly);
|
|
|
|
|
|
// }
|
2024-10-10 17:05:35 +08:00
|
|
|
|
Log.Logger.Information("done.\r\n");
|
2024-10-09 14:45:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(ex.ToString());
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-16 11:24:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-10-09 14:45:09 +08:00
|
|
|
|
}
|