fix: database upgrade and added basic timer

This commit is contained in:
Michael Green
2023-03-11 01:04:03 +11:00
parent d484ee32cb
commit 13dc90883d
11 changed files with 157 additions and 25 deletions

View File

@@ -12,7 +12,7 @@ using Microsoft.AspNetCore.Mvc;
namespace gaseous_server.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
[Route("api/v1/[controller]/[action]")]
public class SignaturesController : ControllerBase
{
/// <summary>

View File

@@ -1,6 +1,12 @@
using System.Text.Json.Serialization;
using gaseous_server;
using gaseous_tools;
// set up db
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
db.InitDB();
// set up server
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@@ -13,6 +19,7 @@ builder.Services.AddControllers().AddJsonOptions(x =>
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHostedService<TimedHostedService>();
var app = builder.Build();
@@ -29,10 +36,6 @@ app.UseAuthorization();
app.MapControllers();
// set up db
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
db.InitDB();
// start the app
app.Run();

50
gaseous-server/Timer.cs Normal file
View File

@@ -0,0 +1,50 @@
using System;
namespace gaseous_server
{
// see: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&tabs=visual-studio-mac#timed-background-tasks-1
public class TimedHostedService : IHostedService, IDisposable
{
private int executionCount = 0;
private readonly ILogger<TimedHostedService> _logger;
private Timer _timer;
public TimedHostedService(ILogger<TimedHostedService> logger)
{
_logger = logger;
}
public Task StartAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(5));
return Task.CompletedTask;
}
private void DoWork(object state)
{
var count = Interlocked.Increment(ref executionCount);
_logger.LogInformation(
"Timed Hosted Service is working. Count: {Count}", count);
}
public Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}