API updates for functional code
This commit is contained in:
@@ -1,25 +1,51 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using house_plant_api.Context;
|
||||
using house_plant_api.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// 1. Register EF Core with DbContextFactory
|
||||
builder.Services.AddDbContextFactory<AppDbContext>(options =>
|
||||
options.UseSqlite("Data Source=devices.db"));
|
||||
|
||||
// 2. Register the BluetoothService (can be Singleton or Transient)
|
||||
builder.Services.AddSingleton<BluetoothService>();
|
||||
|
||||
// 3. Register the hosted service
|
||||
builder.Services.AddHostedService<DevicePollingService>();
|
||||
|
||||
// 4. Add controllers
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// 1. Register CORS services
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll", policyBuilder =>
|
||||
{
|
||||
policyBuilder
|
||||
.AllowAnyOrigin() // Allows requests from any domain
|
||||
.AllowAnyHeader() // Allows any request headers
|
||||
.AllowAnyMethod(); // Allows any HTTP method (GET, POST, PUT, etc.)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
// 3. Use CORS with the named policy
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
// Ensure DB is created or migrated
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
var dbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<AppDbContext>>();
|
||||
using var dbContext = dbContextFactory.CreateDbContext();
|
||||
dbContext.Database.EnsureCreated(); // or dbContext.Database.Migrate() if using migrations
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user