Skip to content

Commit

Permalink
feat: Add system data collection API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
twpol committed Jan 20, 2024
1 parent a0f9857 commit f0fadee
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/bin
/data
/obj
40 changes: 40 additions & 0 deletions API/Collect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;

namespace Open_Rails_Telemetry.API
{
[Route("api/[controller]")]
[ApiController]
public class Collect : ControllerBase
{
readonly IConfiguration Configuration;
readonly string DataPathCollectSystem;

public Collect(IConfiguration configuration)
{
Configuration = configuration;
DataPathCollectSystem = Path.Combine(Configuration["DataPath"], "collect", "system");
if (!Directory.Exists(DataPathCollectSystem)) Directory.CreateDirectory(DataPathCollectSystem);
}

[HttpPost("System")]
[Consumes("application/json")]
public async Task<IActionResult> PostSystem([FromBody] JsonElement data)
{
// File's name has the date for analysis, and a random key to prevent collisions
var date = DateTime.UtcNow.Date;
var randomKey = Guid.NewGuid().ToString();
var file = new FileInfo(Path.Combine(DataPathCollectSystem, $"{date:yyyy-MM-dd}_{randomKey}.json"));
// Write the JSON into the file
await using (var stream = file.Create())
{
await JsonSerializer.SerializeAsync(stream, data);
}
// Wipe out the file's timestamps so we cannot correlate them with e.g. access logs
file.CreationTimeUtc = date;
file.LastWriteTimeUtc = date;
file.LastAccessTimeUtc = date;
return Ok();
}
}
}
2 changes: 2 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddRazorPages();

var app = builder.Build();
Expand All @@ -20,6 +21,7 @@

app.UseAuthorization();

app.MapControllers();
app.MapRazorPages();

app.Run();
3 changes: 2 additions & 1 deletion appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"DataPath": "data"
}
3 changes: 2 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"DataPath": "/var/local/dotnet/telemetry.openrails.org"
}

0 comments on commit f0fadee

Please sign in to comment.