-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create sitemap.xml dynamically
- Loading branch information
1 parent
8fc2568
commit 86ac0f5
Showing
15 changed files
with
145 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
using LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.RateLimiting; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace LinkDotNet.Blog.Web.Controller; | ||
|
||
[EnableRateLimiting("ip")] | ||
[Route("sitemap.xml")] | ||
public sealed class SitemapController : ControllerBase | ||
{ | ||
private readonly ISitemapService sitemapService; | ||
private readonly IXmlWriter xmlWriter; | ||
private readonly IMemoryCache memoryCache; | ||
|
||
public SitemapController( | ||
ISitemapService sitemapService, | ||
IXmlWriter xmlWriter, | ||
IMemoryCache memoryCache) | ||
{ | ||
this.sitemapService = sitemapService; | ||
this.xmlWriter = xmlWriter; | ||
this.memoryCache = memoryCache; | ||
} | ||
|
||
[ResponseCache(Duration = 3600)] | ||
[HttpGet] | ||
public async Task<IActionResult> GetSitemap() | ||
{ | ||
var buffer = await memoryCache.GetOrCreateAsync("sitemap.xml", async e => | ||
{ | ||
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1); | ||
return await GetSitemapBuffer(); | ||
}) | ||
?? throw new InvalidOperationException("Buffer is null"); | ||
|
||
return File(buffer, "application/xml"); | ||
} | ||
|
||
private async Task<byte[]> GetSitemapBuffer() | ||
{ | ||
var baseUri = $"{Request.Scheme}://{Request.Host}{Request.PathBase}"; | ||
var sitemap = await sitemapService.CreateSitemapAsync(baseUri); | ||
var buffer = await xmlWriter.WriteToBuffer(sitemap); | ||
return buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
src/LinkDotNet.Blog.Web/Features/Admin/Sitemap/Services/XmlFileWriter.cs
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/LinkDotNet.Blog.Web/Features/Admin/Sitemap/Services/XmlWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
|
||
namespace LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services; | ||
|
||
public sealed class XmlWriter : IXmlWriter | ||
{ | ||
public async Task<byte[]> WriteToBuffer<T>(T objectToSave) | ||
{ | ||
await using var memoryStream = new MemoryStream(); | ||
await using var xmlWriter = System.Xml.XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true, Async = true }); | ||
var serializer = new XmlSerializer(typeof(T)); | ||
serializer.Serialize(xmlWriter, objectToSave); | ||
xmlWriter.Close(); | ||
return memoryStream.ToArray(); | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
src/LinkDotNet.Blog.Web/Features/Admin/Sitemap/SitemapPage.razor
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 21 additions & 39 deletions
60
tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Services/SitemapServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,41 @@ | ||
using System; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using LinkDotNet.Blog.Domain; | ||
using LinkDotNet.Blog.Infrastructure.Persistence; | ||
using LinkDotNet.Blog.TestUtilities; | ||
using LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services; | ||
using Microsoft.AspNetCore.Components; | ||
using TestContext = Xunit.TestContext; | ||
|
||
namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services; | ||
|
||
public sealed class SitemapServiceTests : IDisposable | ||
public sealed class SitemapServiceTests : SqlDatabaseTestBase<BlogPost> | ||
{ | ||
private const string OutputDirectory = "wwwroot"; | ||
private const string OutputFilename = $"{OutputDirectory}/sitemap.xml"; | ||
private readonly SitemapService sut; | ||
|
||
public SitemapServiceTests() | ||
{ | ||
var repositoryMock = Substitute.For<IRepository<BlogPost>>(); | ||
sut = new SitemapService(repositoryMock, Substitute.For<NavigationManager>(), new XmlFileWriter()); | ||
Directory.CreateDirectory("wwwroot"); | ||
} | ||
=> sut = new SitemapService(Repository); | ||
|
||
[Fact] | ||
public async Task ShouldSaveSitemapUrlInCorrectFormat() | ||
{ | ||
var urlSet = new SitemapUrlSet | ||
{ | ||
Urls = | ||
[ | ||
new SitemapUrl { Location = "here", } | ||
], | ||
}; | ||
await sut.SaveSitemapToFileAsync(urlSet); | ||
|
||
var lines = await File.ReadAllTextAsync(OutputFilename, TestContext.Current.CancellationToken); | ||
lines.ShouldBe( | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<urlset xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9""> | ||
<url> | ||
<loc>here</loc> | ||
</url> | ||
</urlset>"); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (File.Exists(OutputFilename)) | ||
{ | ||
File.Delete(OutputFilename); | ||
} | ||
|
||
if (Directory.Exists(OutputDirectory)) | ||
{ | ||
Directory.Delete(OutputDirectory, true); | ||
} | ||
var publishedBlogPost = new BlogPostBuilder() | ||
.WithTitle("Title 1") | ||
.WithUpdatedDate(new DateTime(2024, 12, 24)) | ||
.IsPublished() | ||
.Build(); | ||
var unpublishedBlogPost = new BlogPostBuilder() | ||
.IsPublished(false) | ||
.Build(); | ||
await Repository.StoreAsync(publishedBlogPost); | ||
await Repository.StoreAsync(unpublishedBlogPost); | ||
|
||
var sitemap = await sut.CreateSitemapAsync("https://www.linkdotnet.blog"); | ||
|
||
sitemap.Urls.Count.ShouldBe(3); | ||
sitemap.Urls.ShouldContain(u => u.Location == "https://www.linkdotnet.blog/"); | ||
sitemap.Urls.ShouldContain(u => u.Location == "https://www.linkdotnet.blog/archive"); | ||
sitemap.Urls.ShouldContain(u => u.Location == "https://www.linkdotnet.blog/blogPost/" + publishedBlogPost.Id); | ||
} | ||
} |
Oops, something went wrong.