-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update theme and scan for dead links endpoint
- Loading branch information
1 parent
902d59a
commit 6714aff
Showing
19 changed files
with
396 additions
and
116 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
22 changes: 16 additions & 6 deletions
22
src/GordonBeemingCom.Database/Tables/AcceptedExternalUrls.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,21 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Runtime.InteropServices.JavaScript; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace GordonBeemingCom.Database.Tables; | ||
|
||
public partial class AcceptedExternalUrls | ||
{ | ||
[Key] | ||
[StringLength(40)] | ||
public required string UrlHash { get; set; } | ||
[Key] [StringLength(40)] public required string UrlHash { get; set; } | ||
|
||
[StringLength(2048)] | ||
public required string Url { get; set; } | ||
[Required] [StringLength(2048)] public required string Url { get; set; } | ||
|
||
public DateTime DateTimeStamp { get; set; } | ||
[Required] public DateTime DateTimeStamp { get; set; } | ||
|
||
public DateTime? CancelledDate { get; set; } | ||
|
||
[StringLength(50)] public string? DisableReason { get; set; } | ||
|
||
[Required] public required DateTime LastCheckedDate { get; set; } | ||
[Required] public required int ErrorCount { get; set; } | ||
[Required] public required int HttpStatusCode { get; set; } | ||
|
||
// ReSharper disable once EntityFramework.ModelValidation.UnlimitedStringLength | ||
[Required] public required string Headers { get; set; } = default!; | ||
|
||
[Required] public bool IsSuccessStatusCode { get; set; } | ||
} |
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,77 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using GordonBeemingCom.Shared.Models; | ||
using GordonBeemingCom.Shared.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NuGet.Protocol; | ||
|
||
namespace GordonBeemingCom.Editor.Endpoints; | ||
|
||
public static class ExternalUrlsServiceExtensions | ||
{ | ||
public static void RegisterUpdateDeadLinksEndpoint(this WebApplication app) | ||
{ | ||
app.MapPost("/api/test", ()=> Results.Ok("Hello World")) | ||
.AllowAnonymous(); | ||
app.MapPost("/api/update-dead-links", UpdateDeadLinks.EndPoint) | ||
.AllowAnonymous(); | ||
} | ||
} | ||
|
||
public sealed class UpdateDeadLinks | ||
{ | ||
public static async Task<IResult> EndPoint(IExternalUrlsService externalUrlsService, ILogger<UpdateDeadLinks> logger, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
var httpClient = httpClientFactory.CreateClient("link-checker"); | ||
var activeLinks = await externalUrlsService.GetActiveLinks(100); | ||
var linksUpdated = 0; | ||
foreach (var link in activeLinks) | ||
{ | ||
try | ||
{ | ||
// Use HEAD request to retrieve headers only | ||
var request = new HttpRequestMessage(HttpMethod.Head, link.Url); | ||
// Some sites... like unsplashed block bots, so we're pretend to be the Googlebot ??... surprisingly this works | ||
// https://unsplash.com/@gordonbeeming | ||
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Googlebot", "1.0")); | ||
var response = await httpClient.SendAsync(request); | ||
|
||
if (response.StatusCode == HttpStatusCode.Forbidden || | ||
response.StatusCode == HttpStatusCode.MethodNotAllowed || | ||
response.StatusCode == HttpStatusCode.TooManyRequests) | ||
{ | ||
request = new HttpRequestMessage(HttpMethod.Get, link.Url); | ||
response = await httpClient.SendAsync(request); | ||
} | ||
|
||
using var enumerator = response.Headers.GetEnumerator(); | ||
await externalUrlsService.UpdateLinkDetails(new ExternalLinkDetails | ||
{ | ||
UrlHash = link.UrlHash, | ||
Url = link.Url, | ||
Headers = GetList(enumerator).ToList(), | ||
IsSuccessStatusCode = response.IsSuccessStatusCode, | ||
HttpStatusCode = (int)response.StatusCode, | ||
}); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogError(e, "Error updating link [{Hash}] {Link}", link.UrlHash, link.Url); | ||
} | ||
|
||
linksUpdated++; | ||
} | ||
|
||
return Results.Ok(linksUpdated); | ||
} | ||
|
||
private static IEnumerable<KeyValuePair<string, List<string>>> GetList( | ||
IEnumerator<KeyValuePair<string, IEnumerable<string>>> enumerator) | ||
{ | ||
while (enumerator.MoveNext()) | ||
{ | ||
yield return new KeyValuePair<string, List<string>>(enumerator.Current.Key, enumerator.Current.Value.ToList()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ h1:focus { | |
} | ||
|
||
a, .btn-link { | ||
color: #CC4141; | ||
color: #00BBFF; | ||
} | ||
|
||
.btn-primary { | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,7 @@ | ||
namespace GordonBeemingCom.Shared.Models; | ||
|
||
public sealed class ExternalLink | ||
{ | ||
public string UrlHash { get; set; } = default!; | ||
public string Url { get; set; } = default!; | ||
} |
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,10 @@ | ||
namespace GordonBeemingCom.Shared.Models; | ||
|
||
public sealed record ExternalLinkDetails | ||
{ | ||
public string UrlHash { get; set; } = default!; | ||
public string Url { get; set; } = default!; | ||
public List<KeyValuePair<string, List<string>>> Headers { get; set; } = default!; | ||
public bool IsSuccessStatusCode { get; set; } = default!; | ||
public int HttpStatusCode { get; set; } = default!; | ||
} |
Oops, something went wrong.