Walle.Api/AI.Api/Program.cs

72 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}