Revert "Add db for backup and memory happiness"

This reverts commit 9003d2c4dd.
This commit is contained in:
jheaps
2025-08-04 14:20:01 -06:00
parent 9003d2c4dd
commit f4e377c116
27 changed files with 236 additions and 2673 deletions
+154 -52
View File
@@ -4,66 +4,168 @@ using JoshHeaps.Net.Services.Implementations;
using JoshHeaps.Net.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
namespace JoshHeaps.Net;
// Add services to the container.
builder.Services.AddRazorPages();
var configuration = builder.Configuration;
builder.Services.AddControllers();
builder.Services.AddSignalR();
builder.Services.AddSingleton<IChessService, ChessService>();
builder.Services.AddSingleton<BackgroundTaskQueue>();
builder.Services.AddSingleton<ChessDbAccess>();
builder.Services.AddSingleton<StockfishManager>();
var cs = builder.Configuration.GetConnectionString("ChessDatabase");
builder.Services.AddPooledDbContextFactory<ChessDbContext>(o => o.UseSqlite(cs));
builder.Services.AddHostedService<GameCleanupService>();
if (!builder.Environment.IsDevelopment())
builder.Services.AddHostedService<AutoIpUpdateService>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
// need to declare the class so I can add the public static bool
public class Program
{
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<ChessDbContext>>();
await using var db = await factory.CreateDbContextAsync();
db.Database.Migrate();
}
public static bool CheckingForIpUpdates { get; private set; } = false;
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
public static void Main(string[] args)
{
ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
ctx.Context.Response.Headers.Append("Pragma", "no-cache");
ctx.Context.Response.Headers.Append("Expires", "0");
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var configuration = builder.Configuration;
Task updateIpTask;
if (!builder.Environment.IsDevelopment())
updateIpTask = Run(configuration);
builder.Services.AddControllers();
builder.Services.AddSignalR();
builder.Services.AddSingleton<IChessService, ChessService>();
builder.Services.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>();
var cs = builder.Configuration.GetConnectionString("ChessDatabase");
builder.Services.AddDbContext<ChessDbContext>(o => o.UseSqlite(cs));
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ChessDbContext>();
dbContext.Database.Migrate();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
ctx.Context.Response.Headers.Append("Pragma", "no-cache");
ctx.Context.Response.Headers.Append("Expires", "0");
}
});
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.MapHub<ChessHub>("/chessHub");
app.Run();
}
});
app.UseRouting();
static async Task Run(IConfiguration config)
{
CheckingForIpUpdates = true;
HttpClient httpClient = new();
AAAARecord dnsRecord = await GetDnsRecordAsync(config);
string lastKnownIp = dnsRecord.content;
TimeSpan checkInterval = TimeSpan.FromMinutes(1);
app.UseAuthorization();
while (true)
{
try
{
string currentIp = await GetPublicIpAsync(httpClient) ?? "";
app.MapRazorPages();
if (lastKnownIp != currentIp)
{
await UpdateDnsIpAsync(config, dnsRecord, currentIp);
app.MapControllers();
lastKnownIp = currentIp;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
app.MapHub<ChessHub>("/chessHub");
await Task.Delay(checkInterval);
}
}
app.Run();
static async Task<string> GetPublicIpAsync(HttpClient client)
{
try
{
return await client.GetStringAsync(@"https://api.ipify.org/");
}
catch
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to grab public ip");
return await GetPublicIpAsync(client);
}
}
static async Task<AAAARecord> GetDnsRecordAsync(IConfiguration config)
{
try
{
HttpClient cfClient = new();
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
var result = await cfClient.GetAsync(@$"https://api.cloudflare.com/client/v4/zones/{config["zoneId"]}/dns_records");
Console.WriteLine(await result.Content.ReadAsStringAsync());
var records = System.Text.Json.JsonSerializer.Deserialize<RecordList>(await result.Content.ReadAsStringAsync());
return records!.result[0];
}
catch
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to grab current ip");
return await GetDnsRecordAsync(config);
}
}
static async Task UpdateDnsIpAsync(IConfiguration config, AAAARecord record, string ip)
{
HttpClient cfClient = new();
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
object content = new
{
comment = "Update as needed",
content = ip,
name = "@",
proxied = true,
ttl = 3600,
type = "AAAA"
};
var result = await cfClient.PutAsJsonAsync(@$"https://api.cloudflare.com/client/v4/zones/{config["zoneId"]}/dns_records{record.id}", content);
if (!result.IsSuccessStatusCode)
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to update ip");
await UpdateDnsIpAsync(config, record, ip);
}
}
record AAAARecord(string comment, string content, string name, string id);
record RecordList(List<AAAARecord> result);
}