aboutsummaryrefslogtreecommitdiff
path: root/Program.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2024-09-20 16:21:09 +1000
committerJake Mannens <jake@asger.xyz>2024-10-29 11:42:54 +1100
commite8e3c4cba8ffa0056e984c113cfbb75319e00022 (patch)
tree8336315e61f9e77207276d478b25fe5dc7c3d06c /Program.cs
parent0e21907c76dbefed11f382bcf949143f0716567f (diff)
v0.4-rc1v0.4-rc1
Diffstat (limited to 'Program.cs')
-rw-r--r--Program.cs32
1 files changed, 27 insertions, 5 deletions
diff --git a/Program.cs b/Program.cs
index ba53ccb..cc60d92 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,4 +1,4 @@
-using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
namespace PagerParser;
@@ -8,6 +8,7 @@ public class Program {
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<PagerContext>();
+ builder.Services.AddRazorPages();
// This is necessary if we don't want enum values sent as integers in JSON objects
builder.Services.AddControllers().AddJsonOptions(o => {
@@ -19,12 +20,16 @@ public class Program {
// Add our custom services
builder.Services.AddSingleton<IPagerMessageParserService, PagerMessageParserService>();
+ builder.Services.AddSingleton<RootPagerHandler>();
+ builder.Services.AddSingleton<IRootPagerHandler>(p => p.GetRequiredService<RootPagerHandler>());
+ builder.Services.AddSingleton<IHostedService>(p => p.GetRequiredService<RootPagerHandler>());
builder.Services.AddHostedService<PagerFetchService>();
var app = builder.Build();
// Create a temporary scope so that we can access services during startup
using var scope = app.Services.CreateScope();
+ var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var parser = scope.ServiceProvider.GetRequiredService<IPagerMessageParserService>();
@@ -35,26 +40,40 @@ public class Program {
var needsMigration = db.Database.GetPendingMigrations().Any();
db.Database.Migrate();
- if(config.GetValue<bool>("PagerParser:ReparseAllOnStartup")) {
+ int c = 0;
+ if(needsMigration || config.GetValue<bool>("PagerParser:ReparseAllOnStartup")) {
+ logger.LogInformation("Reparsing all messages...");
db.ParsedPagerMessages.RemoveRange(
db.ParsedPagerMessages.ToArray());
db.SaveChanges();
foreach(var message in db.PagerMessages) {
message.ParsedMessage = parser.TryParse(message.Message);
- if(message.ParsedMessage is not null)
+ if(message.ParsedMessage is not null) {
+ c++;
message.ParsedMessage.GpsPosition =
PositionCalculator.GetGpsPosition(message.ParsedMessage);
+ }
}
db.SaveChanges();
+ logger.LogInformation($"Successfully reparsed {c}/{db.PagerMessages.Count()} messages");
} else if(config.GetValue<bool>("PagerParser:ReparseFailedOnStartup")) {
+ logger.LogInformation("Reparsing non-parsed messages...");
foreach(var message in db.PagerMessages.Where(m => m.ParsedMessage == null)) {
message.ParsedMessage = parser.TryParse(message.Message);
- if(message.ParsedMessage is not null)
+ if(message.ParsedMessage is not null) {
+ c++;
if(message.ParsedMessage.GpsPosition is null)
message.ParsedMessage.GpsPosition =
PositionCalculator.GetGpsPosition(message.ParsedMessage);
+ }
}
db.SaveChanges();
+ logger.LogInformation($"Successfully reparsed {c}/{db.PagerMessages.Count()} messages");
+ }
+
+ if(!app.Environment.IsDevelopment()) {
+ app.UseExceptionHandler("/Error");
+ app.UseHsts();
}
// Start the server
@@ -64,8 +83,11 @@ public class Program {
.AllowAnyHeader());
app.UseHttpsRedirection();
app.UseHsts();
- app.UseStaticFiles();
app.MapControllers();
+ app.UseStaticFiles();
+ app.UseRouting();
+ app.UseAuthorization();
+ app.MapRazorPages();
app.Run();
}
}