Files
MoviconHub/MoviconWebApi/Program.cs
2025-09-15 17:59:48 +08:00

88 lines
3.2 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 FastEndpoints;
using FastEndpoints.Security;
using FastEndpoints.Swagger;
namespace MoviconWebApi
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//builder.Services.AddAuthorization();
// 配置FreeSql
var connectionString = builder.Configuration.GetConnectionString("SqlServer");
var freeSql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer, connectionString)
.UseAutoSyncStructure(true) // 开发环境下自动同步实体结构到数据库
.UseMonitorCommand(cmd => Console.WriteLine($"SQL{cmd.CommandText}")) // 监控SQL语句开发环境用
.Build();
// 注册FreeSql为单例服务
builder.Services.AddSingleton<IFreeSql>(freeSql);
// 配置JWT认证
builder.Services.AddAuthenticationJwtBearer(o => o.SigningKey = builder.Configuration["JwtSigningKey"]);
builder.Services.AddAuthorization();
builder.Services.AddFastEndpoints();
builder.Services.SwaggerDocument(o =>
{
o.DocumentSettings = s =>
{
s.Title = "Movicon Web API";
s.Version = "v1";
s.Description = "MoviconHub 数据访问API";
};
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
//builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
//注意FastEndpoints有自己的Swagger配置不需要使用原生的UseSwagger和UseSwaggerUI
//if (app.Environment.IsDevelopment())
//{
// app.UseSwagger();
// app.UseSwaggerUI();
//}
app.UseAuthentication()
.UseAuthorization()
.UseFastEndpoints(c =>
{
c.Serializer.Options.PropertyNamingPolicy = null; // 保持属性名不变
c.Endpoints.RoutePrefix = "api"; // 设置API路由前缀
})
.UseSwaggerGen();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
//app.MapGet("/weatherforecast", (HttpContext httpContext) =>
//{
// var forecast = Enumerable.Range(1, 5).Select(index =>
// new WeatherForecast
// {
// Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
// TemperatureC = Random.Shared.Next(-20, 55),
// Summary = summaries[Random.Shared.Next(summaries.Length)]
// })
// .ToArray();
// return forecast;
//})
//.WithName("GetWeatherForecast")
//.WithOpenApi();
app.Run();
}
}
}