72 lines
2.5 KiB
C#
72 lines
2.5 KiB
C#
using AI.Api.WebCore;
|
||
using AI.Common.Services;
|
||
using AI.Common.Services.Interface;
|
||
using SqlSugar;
|
||
using DbType = SqlSugar.DbType;
|
||
|
||
namespace AI.Api
|
||
{
|
||
public class Program
|
||
{
|
||
public static void Main(string[] args)
|
||
{
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
var configuration = builder.Configuration;
|
||
// Add services to the container.
|
||
|
||
builder.Services.AddControllers(options =>
|
||
{
|
||
// 全局异常捕获,无需在代码中 写 try catch
|
||
options.Filters.Add<GlobalExceptionFilter>();
|
||
// 全局模型赋值默认值 和 统一返回格式处理
|
||
options.Filters.Add<ModelActionFilter>();
|
||
});
|
||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwaggerGen();
|
||
builder.Services.AddSingleton(new Common.Helpers.JwtHelper(configuration));
|
||
builder.Services.AddAuth(configuration["Jwt:Issuer"],
|
||
configuration["Jwt:Audience"],
|
||
configuration["Jwt:SecretKey"]);
|
||
builder.Services.AddSqlsugar(builder.Environment.EnvironmentName, new ConnectionConfig
|
||
{
|
||
DbType = DbType.MySql,
|
||
ConfigId = "walle",
|
||
ConnectionString = configuration.GetConnectionString("walle"),
|
||
IsAutoCloseConnection = true
|
||
},
|
||
new ConnectionConfig
|
||
{
|
||
DbType = DbType.MySql,
|
||
ConfigId = "usercenter",
|
||
ConnectionString = configuration.GetConnectionString("usercenter"),
|
||
IsAutoCloseConnection = true
|
||
});
|
||
|
||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||
builder.Services.AddScoped<IQuestionLogService, QuestionLogService>();
|
||
builder.Services.AddScoped<IPromptService, PromptService>();
|
||
|
||
|
||
var app = builder.Build();
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI();
|
||
}
|
||
|
||
//调用中间件:UseAuthentication(认证),必须在所有需要身份认证的中间件前调用,比如 UseAuthorization(授权)。
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
|
||
app.MapControllers();
|
||
|
||
app.Run();
|
||
}
|
||
}
|
||
}
|