Skip to content

Commit

Permalink
Added sitemap and it's caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Feb 23, 2024
1 parent 1c06aba commit 0483e62
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/BUTR.CrashReportServer/Controllers/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
using BUTR.CrashReportServer.Models;
using BUTR.CrashReportServer.Models.API;
using BUTR.CrashReportServer.Models.Database;
using BUTR.CrashReportServer.Models.Sitemaps;
using BUTR.CrashReportServer.Options;
using BUTR.CrashReportServer.Services;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using System;
using System.Collections.Generic;
Expand All @@ -33,12 +36,14 @@ public sealed record GetNewCrashReportsBody
}

private readonly ILogger _logger;
private readonly ReportOptions _options;
private readonly AppDbContext _dbContext;
private readonly GZipCompressor _gZipCompressor;

public ReportController(ILogger<ReportController> logger, AppDbContext dbContext, GZipCompressor gZipCompressor)
public ReportController(ILogger<ReportController> logger, IOptionsSnapshot<ReportOptions> options, AppDbContext dbContext, GZipCompressor gZipCompressor)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_options = options.Value ?? throw new ArgumentNullException(nameof(options));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_gZipCompressor = gZipCompressor ?? throw new ArgumentNullException(nameof(gZipCompressor));
}
Expand Down Expand Up @@ -180,4 +185,25 @@ public ActionResult<IEnumerable<FileMetadata>> GetNewCrashReportsDates([FromBody
.Where(x => x.Created.Ticks > body.DateTime.Ticks)
.Select(x => new FileMetadata(x.FileId, x.CrashReportId, x.Version, x.Created.ToUniversalTime())));
}

[AllowAnonymous]
[HttpGet("sitemap.xml")]
[Produces("application/xml")]
[ProducesResponseType(typeof(Urlset), StatusCodes.Status200OK, "application/xml")]
[ProducesResponseType(typeof(void), StatusCodes.Status500InternalServerError, "application/problem+xml")]
[ResponseCache(Duration = 60 * 60 * 4)]
public IActionResult Sitemap(CancellationToken ct)
{
var sitemap = new Urlset
{
Url = _dbContext.Set<IdEntity>().Select(x => new { x.FileId, x.Created }).Select(x => new Url
{
Location = $"{_options.BaseUri}/{x.FileId}",
TimeStamp = x.Created,
Priority = 0.5,
ChangeFrequency = ChangeFrequency.Never,
}).ToList(),
};
return Ok(sitemap);
}
}
29 changes: 29 additions & 0 deletions src/BUTR.CrashReportServer/Models/Sitemaps/ChangeFrequency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Xml.Serialization;

namespace BUTR.CrashReportServer.Models.Sitemaps;

[Serializable]
public enum ChangeFrequency
{
[XmlEnum(Name = "always")]
Always,

[XmlEnum(Name = "hourly")]
Hourly,

[XmlEnum(Name = "daily")]
Daily,

[XmlEnum(Name = "weekly")]
Weekly,

[XmlEnum(Name = "monthly")]
Monthly,

[XmlEnum(Name = "yearly")]
Yearly,

[XmlEnum(Name = "never")]
Never
}
27 changes: 27 additions & 0 deletions src/BUTR.CrashReportServer/Models/Sitemaps/Url.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Xml.Serialization;

namespace BUTR.CrashReportServer.Models.Sitemaps;

[XmlRoot(ElementName="url", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public class Url
{
[XmlElement(ElementName="loc", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public string Location { get; set; }

[XmlIgnore]
public DateTime TimeStamp { get; set; }

[XmlElement(ElementName="lastmod", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public string LastMod
{
get => TimeStamp.ToString("yyyy-MM-ddTHH:mm:sszzz");
set => TimeStamp = DateTime.Parse(value);
}

[XmlElement(ElementName="changefreq", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public ChangeFrequency ChangeFrequency { get; set; }

[XmlElement(ElementName="priority", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public double Priority { get; set; }
}
11 changes: 11 additions & 0 deletions src/BUTR.CrashReportServer/Models/Sitemaps/Urlset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Xml.Serialization;

namespace BUTR.CrashReportServer.Models.Sitemaps;

[XmlRoot(ElementName="urlset", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public class Urlset {

[XmlElement(ElementName="url", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public List<Url> Url { get; set; }
}
5 changes: 5 additions & 0 deletions src/BUTR.CrashReportServer/Options/CrashUploadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ public record CrashUploadOptions
public int MinContentLength { get; set; }
public int MaxContentLength { get; set; }

public string? BaseUri { get; set; }
}

public record ReportOptions
{
public string? BaseUri { get; set; }
}
2 changes: 1 addition & 1 deletion src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override async Task ExecuteAsync(CancellationToken ct)
var dbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<AppDbContext>>();
var dbContextPool = new ObjectPool<AppDbContext>(dbContextFactory.CreateDbContextAsync);

var options = new ParallelOptions { CancellationToken = ct, MaxDegreeOfParallelism = 4 };
var options = new ParallelOptions { CancellationToken = ct };
await Parallel.ForEachAsync(Enumerable.Range(0, 4), options, async (_, ct2) =>
{
var dbContext = await dbContextPool.GetAsync(ct2);
Expand Down
8 changes: 7 additions & 1 deletion src/BUTR.CrashReportServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -40,6 +41,7 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<AuthOptions>(_configuration.GetSection("Auth"));
services.Configure<StorageOptions>(_configuration.GetSection("Storage"));
services.Configure<CrashUploadOptions>(_configuration.GetSection("CrashUpload"));
services.Configure<ReportOptions>(_configuration.GetSection("Report"));

services.AddTransient<RandomNumberGenerator>(_ => RandomNumberGenerator.Create());
services.AddSingleton<HexGenerator>();
Expand Down Expand Up @@ -102,13 +104,15 @@ public void ConfigureServices(IServiceCollection services)
opts.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
}).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();
services.Configure<JsonSerializerOptions>(opts =>
{
opts.PropertyNameCaseInsensitive = true;
opts.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opts.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});

services.AddResponseCaching();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand All @@ -122,6 +126,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", _appName));

app.UseRouting();

app.UseResponseCaching();

app.UseAuthentication();
app.UseAuthorization();
Expand Down
4 changes: 4 additions & 0 deletions src/BUTR.CrashReportServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"Main": ""
},

"Report": {
"BaseUri": "https://crash.butr.dev/report"
},

"CrashUpload": {
"MinContentLength": 512,
"MaxContentLength": 8388608,
Expand Down

0 comments on commit 0483e62

Please sign in to comment.