-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (71 loc) · 2.68 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (71 loc) · 2.68 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
91
92
93
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using SatelliteTracker.Backend.Middleware;
using SatelliteTracker.Backend.Repositories;
using SatelliteTracker.Backend.Repositories.Interfaces;
using SatelliteTracker.Backend.Services;
using SatelliteTracker.Backend.Services.Interfaces;
using SatelliteTracker.Backend.Services.Gps;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "SatelliteTracker API", Version = "v1" });
});
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// Ïîäêëþ÷åíèå áä
builder.Services.AddScoped<ISatelliteDataRepository, SatelliteDataRepository>();
// Mock äàííûå. Èñïîëüçóåòñÿ îòäåëüíî îò äàííûõ èç output.nmea èëè COM ïîðòà
//builder.Services.AddSingleton<ISatelliteDataRepository, MockSatelliteDataRepository>();
// Ðåãèñòðàöèÿ ñåðâèñîâ
builder.Services.Configure<GpsSettings>(builder.Configuration.GetSection("GpsSettings"));
builder.Services.AddScoped<INmeaParserService, NmeaParserService>();
builder.Services.AddSingleton<WebSocketConnectionManager>();
builder.Services.AddHostedService<GpsDataBackgroundService>();
var gpsSettings = builder.Configuration.GetSection("GpsSettings").Get<GpsSettings>();
if (gpsSettings.SimulationMode)
{
builder.Services.AddSingleton<IGpsReader, FileGpsReader>();
}
else
{
builder.Services.AddSingleton<IGpsReader, SimulatedGpsReader>();
}
// Health Checks
builder.Services.AddHealthChecks();
//.AddDbContextCheck<AppDbContext>();
var app = builder.Build();
app.UseStaticFiles();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseWebSockets();
app.UseMiddleware<WebSocketMiddleware>();
Console.WriteLine($"ENVIRONMENT: {builder.Environment.EnvironmentName}");
app.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(
System.Text.Json.JsonSerializer.Serialize(new
{
status = report.Status.ToString(),
checks = report.Entries.Select(e => new
{
name = e.Key,
status = e.Value.Status.ToString(),
description = e.Value.Description
})
}));
}
});
app.Run();