-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
90 lines (73 loc) · 2.76 KB
/
Copy pathProgram.cs
File metadata and controls
90 lines (73 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Microsoft.EntityFrameworkCore;
using ParkingProject;
using ParkingProject.Data;
using ParkingProject.Services;
using Confluent.Kafka; // Wichtig!
var builder = WebApplication.CreateBuilder(args);
// builder.Services.AddDbContext<ParkingLotDB>(opt => opt.UseInMemoryDatabase("ParkingLot"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
// Wirft eine Exception, wenn "PostgresConnection" fehlt -> Das ist gut so!
var connectionString = builder.Configuration.GetConnectionString("PostgresConnection")
?? throw new InvalidOperationException("Connection string 'PostgresConnection' not found.");
builder.Services.AddDbContext<ParkingLotDB>(options =>
options.UseNpgsql(connectionString));
// Registriert die Controller
builder.Services.AddControllers();
// Registiert Mock-Up und den Kafka-Service
builder.Services.AddSingleton<MockSpotGenerator>();
builder.Services.AddSingleton<KafkaProducerService>();
//Serviceergänzung für DI
builder.Services.AddScoped<IParkingLotService, ParkingLotService>();
// Fügt nur die NSwag-Services für OpenAPI hinzu
builder.Services.AddOpenApiDocument(config =>
{
config.DocumentName = "ParkingSpotAPI";
config.Title = "ParkingSpotAPI v1";
config.Version = "v1";
});
builder.Services.AddHostedService<AlertConsumerService>();
// 1. Konfiguration für die Verbindung zum Docker-Kafka
var producerConfig = new ProducerConfig
{
BootstrapServers = "kafka:29092"
};
builder.Services.AddSingleton<IProducer<Null, string>>(sp =>
{
var config = new ProducerConfig { BootstrapServers = "kafka:29092" };
// 2. Hier den Typ im Builder ändern
return new ProducerBuilder<Null, string>(config).Build();
});
var app = builder.Build();
// Swagger Konfiguration
if (app.Environment.IsDevelopment())
{
app.UseOpenApi();
app.UseSwaggerUi(config =>
{
config.DocumentTitle = "ParkingSpotAPI";
config.Path = "/swagger";
config.DocumentPath = "/swagger/{documentName}/swagger.json";
config.DocExpansion = "list";
});
}
// Datenbank Initialisierung & Seeding (Alles in einem Block!)
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ParkingLotDB>();
// Wendet Migrationen an ODER erstellt die DB, falls sie fehlt
// Wenn du noch keine Migrationen hast, nutze: context.Database.EnsureCreated();
context.Database.Migrate();
// Füllt die Testdaten ein
DbInitializer.Initialize(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Ein Fehler ist beim Initialisieren der Datenbank aufgetreten.");
}
}
app.MapControllers();
app.Run();