52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using PetWash.Api.Data;
|
||
|
|
using PetWash.Api.Services;
|
||
|
|
|
||
|
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
|
||
|
|
// 添加数据库
|
||
|
|
builder.Services.AddDbContext<PetWashDbContext>(options =>
|
||
|
|
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection") ?? "Data Source=petwash.db"));
|
||
|
|
|
||
|
|
// 添加服务
|
||
|
|
builder.Services.AddSingleton<MqttService>();
|
||
|
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<MqttService>());
|
||
|
|
builder.Services.AddScoped<OrderService>();
|
||
|
|
|
||
|
|
// 添加CORS
|
||
|
|
builder.Services.AddCors(options =>
|
||
|
|
{
|
||
|
|
options.AddDefaultPolicy(policy =>
|
||
|
|
{
|
||
|
|
policy.AllowAnyOrigin()
|
||
|
|
.AllowAnyMethod()
|
||
|
|
.AllowAnyHeader();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.Services.AddControllers();
|
||
|
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
|
builder.Services.AddSwaggerGen();
|
||
|
|
|
||
|
|
var app = builder.Build();
|
||
|
|
|
||
|
|
// 初始化数据库
|
||
|
|
using (var scope = app.Services.CreateScope())
|
||
|
|
{
|
||
|
|
var db = scope.ServiceProvider.GetRequiredService<PetWashDbContext>();
|
||
|
|
db.Database.EnsureCreated();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (app.Environment.IsDevelopment())
|
||
|
|
{
|
||
|
|
app.UseSwagger();
|
||
|
|
app.UseSwaggerUI();
|
||
|
|
}
|
||
|
|
|
||
|
|
app.UseCors();
|
||
|
|
app.UseHttpsRedirection();
|
||
|
|
app.UseAuthorization();
|
||
|
|
app.MapControllers();
|
||
|
|
|
||
|
|
app.Run();
|