Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904;CA1724</WarningsNotAsErrors>
<WarningsNotAsErrors>CA1021;CA1062;CA1724;SA1414;</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -31,11 +31,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All"/>
<PackageReference Include="Roslynator.Analyzers" Version="4.0.2" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All"/>
<PackageReference Include="Roslynator.Analyzers" Version="4.15.0" PrivateAssets="All"/>
<!-- It appears as though there might be a performance issue in versions of Microsoft.VisualStudio.Threading.Analyzers past this at least through 17.0.64 -->
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="16.9.60" PrivateAssets="All"/>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.14.15" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/Statiq.Web.Examples/Statiq.Web.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Statiq.Web.Aws/Statiq.Web.Aws.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageTags>Statiq Static StaticContent StaticSite Blog BlogEngine AmazonWebServices AWS</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

<Choose>
Expand Down
76 changes: 36 additions & 40 deletions src/Statiq.Web.Azure/DeploySearchIndex.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Azure;
using Microsoft.Extensions.Logging;
using Polly;
using Statiq.Common;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;

namespace Statiq.Web.Azure
{
Expand Down Expand Up @@ -41,7 +40,7 @@ public DeploySearchIndex(
Config<string> searchServiceName,
Config<string> indexName,
Config<string> apiKey,
Config<IEnumerable<Field>> fields)
Config<IEnumerable<SearchField>> fields)
: base(
new Dictionary<string, IConfig>
{
Expand All @@ -59,70 +58,67 @@ protected override async Task<IEnumerable<IDocument>> ExecuteConfigAsync(IDocume
string searchServiceName = values.GetString(SearchServiceName) ?? throw new ExecutionException("Invalid search service name");
string indexName = values.GetString(IndexName) ?? throw new ExecutionException("Invalid search index name");
string apiKey = values.GetString(ApiKey) ?? throw new ExecutionException("Invalid search API key");
IList<Field> fields = values.GetList<Field>(Fields)?.ToList() ?? throw new ExecutionException("Invalid search fields");
IList<SearchField> fields = values.GetList<SearchField>(Fields)?.ToList() ?? throw new ExecutionException("Invalid search fields");

SearchServiceClient client = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));

// Delete the index if it currently exists (recreating is the easiest way to update it)
CorsOptions corsOptions = null;
if (await client.Indexes.ExistsAsync(indexName, null, context.CancellationToken))
if (!Uri.TryCreate(searchServiceName, UriKind.Absolute, out Uri searchServiceUri))
{
// Get the CORS options because we'll need to recreate those
Microsoft.Azure.Search.Models.Index existingIndex = await client.Indexes.GetAsync(indexName, null, context.CancellationToken);
corsOptions = existingIndex.CorsOptions;

// Delete the existing index
context.LogDebug($"Deleting existing search index {indexName}");
await client.Indexes.DeleteAsync(indexName, null, null, context.CancellationToken);
throw new ExecutionException("Invalid search service name");
}

SearchIndexClient searchIndexClient = new SearchIndexClient(searchServiceUri, new AzureKeyCredential(apiKey));

// Create the index
Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index
SearchIndex index = new SearchIndex(indexName)
{
Name = indexName,
Fields = fields,
CorsOptions = corsOptions
};
context.LogDebug($"Creating search index {indexName}");
await client.Indexes.CreateAsync(index, null, context.CancellationToken);
await searchIndexClient.CreateOrUpdateIndexAsync(index, true, true, context.CancellationToken);

// Upload the documents to the search index in batches
context.LogDebug($"Uploading {context.Inputs.Length} documents to search index {indexName}...");
ISearchIndexClient indexClient = client.Indexes.GetClient(indexName);
SearchClient indexClient = searchIndexClient.GetSearchClient(indexName);
int start = 0;
do
{
// Create the dynamic search documents and batch
IndexAction<Microsoft.Azure.Search.Models.Document>[] indexActions = context.Inputs
IndexDocumentsAction<SearchDocument>[] indexActions = context.Inputs
.Skip(start)
.Take(BatchSize)
.Select(doc =>
{
Microsoft.Azure.Search.Models.Document searchDocument = new Microsoft.Azure.Search.Models.Document();
foreach (Field field in fields)
SearchDocument searchDocument = new SearchDocument();
foreach (SearchField field in fields)
{
if (doc.ContainsKey(field.Name))
{
searchDocument[field.Name] = doc.Get(field.Name);
}
}
return IndexAction.Upload(searchDocument);

return IndexDocumentsAction.Upload(searchDocument);
})
.ToArray();
IndexBatch<Microsoft.Azure.Search.Models.Document> indexBatch = IndexBatch.New(indexActions);
IndexDocumentsBatch<SearchDocument> indexBatch = IndexDocumentsBatch.Create(indexActions);

// Upload the batch with exponential retry for failures
await Policy
.Handle<IndexBatchException>()
.WaitAndRetryAsync(
5,
attempt =>
{
context.LogWarning($"Failure while uploading batch {(start / BatchSize) + 1}, retry number {attempt}");
return TimeSpan.FromSeconds(Math.Pow(2, attempt));
},
(ex, _) => indexBatch = ((IndexBatchException)ex).FindFailedActionsToRetry(indexBatch, fields.Single(x => x.IsKey == true).Name))
.ExecuteAsync(async ct => await indexClient.Documents.IndexAsync(indexBatch, null, ct), context.CancellationToken);
.Handle<RequestFailedException>()
.OrResult<IndexDocumentsResult>(r => r.Results.Any(result => !result.Succeeded))
.WaitAndRetryAsync(
5,
attempt =>
{
context.LogWarning($"Failure while uploading batch {(start / BatchSize) + 1}, retry number {attempt}");
return TimeSpan.FromSeconds(Math.Pow(2, attempt));
},
(ex, _) =>
{
IEnumerable<string> failedResults = ex.Result.Results.Where(r => !r.Succeeded).Select(result => result.Key);
IEnumerable<IndexDocumentsAction<SearchDocument>> failedActions = indexActions.Where(action => action.Document.Keys.Any(key => failedResults.Contains(key)));
indexBatch = IndexDocumentsBatch.Create(failedActions.ToArray());
})
.ExecuteAsync(async ct => await indexClient.IndexDocumentsAsync(indexBatch, null, ct), context.CancellationToken);

context.LogDebug($"Uploaded {start + indexActions.Length} documents to search index {indexName}");
start += 1000;
Expand Down
4 changes: 2 additions & 2 deletions src/Statiq.Web.Azure/Statiq.Web.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<PackageTags>Statiq Static StaticContent StaticSite Blog BlogEngine Azure AppService</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
<PackageReference Include="Polly" Version="7.1.1" />
<PackageReference Include="Azure.Search.Documents" Version="11.2.1" />
<PackageReference Include="Polly" Version="8.6.5" />
</ItemGroup>

<Choose>
Expand Down
4 changes: 2 additions & 2 deletions src/Statiq.Web.GitHub/Statiq.Web.GitHub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</Choose>

<ItemGroup>
<PackageReference Include="Octokit" Version="0.46.0" />
<PackageReference Include="Polly" Version="7.1.1" />
<PackageReference Include="Octokit" Version="14.0.0" />
<PackageReference Include="Polly" Version="8.6.5" />
</ItemGroup>

</Project>
57 changes: 32 additions & 25 deletions src/Statiq.Web.Hosting/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Statiq.Common;
using Statiq.Web.Hosting.LiveReload;
using Statiq.Web.Hosting.Middleware;
using Microsoft.AspNetCore.Hosting.Server;

namespace Statiq.Web.Hosting
{
/// <summary>
/// An HTTP server that can serve static files from a specified directory on disk.
/// </summary>
public class Server : IWebHost
public class Server : IHost
{
private readonly IReadOnlyDictionary<string, string> _contentTypes;
private readonly IReadOnlyDictionary<string, string> _customHeaders;
private readonly IWebHost _host;
private readonly IHost _host;
private readonly LiveReloadServer _liveReloadServer;

public bool Extensionless { get; }
Expand All @@ -40,7 +42,8 @@ public class Server : IWebHost
/// </summary>
public string VirtualDirectory { get; }

public IFeatureCollection ServerFeatures => _host.ServerFeatures;
// IWebHost exposed ServerFeatures directly; IHost does not. Resolve the server and return its features if available.
public IFeatureCollection ServerFeatures => _host.Services.GetService<IServer>()?.Features;

public IServiceProvider Services => _host.Services;

Expand All @@ -50,7 +53,7 @@ public class Server : IWebHost
/// <param name="localPath">The local path to serve files from.</param>
/// <param name="port">The port the server will serve HTTP requests on.</param>
public Server(string localPath, int port = 5080)
: this(localPath, port, true, null, true, null)
: this(localPath, port, true, null, true, null, null, null)
{
}

Expand Down Expand Up @@ -113,29 +116,33 @@ public Server(

string currentDirectory = Directory.GetCurrentDirectory();

_host = new WebHostBuilder()
.UseContentRoot(currentDirectory)
.UseWebRoot(Path.Combine(currentDirectory, "wwwroot"))
.ConfigureLogging(loggingBuilder =>
_host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
if (loggerProviders is object)
{
foreach (ILoggerProvider loggerProvider in loggerProviders)
webHostBuilder
.UseContentRoot(currentDirectory)
.UseWebRoot(Path.Combine(currentDirectory, "wwwroot"))
.ConfigureLogging(loggingBuilder =>
{
if (loggerProvider is object)
if (loggerProviders != null)
{
loggingBuilder.AddProvider(
new ChangeLevelLoggerProvider(
loggerProvider,
level => level == LogLevel.Information ? LogLevel.Debug : level));
foreach (ILoggerProvider loggerProvider in loggerProviders)
{
if (loggerProvider != null)
{
loggingBuilder.AddProvider(
new ChangeLevelLoggerProvider(
loggerProvider,
level => level == LogLevel.Information ? LogLevel.Debug : level));
}
}
}
}
}
})
.UseKestrel()
.ConfigureKestrel(x => x.ListenAnyIP(port))
.ConfigureServices(ConfigureServices)
.Configure(ConfigureApp);
})
.UseKestrel()
.ConfigureKestrel(x => x.ListenAnyIP(port))
.ConfigureServices(ConfigureServices)
.Configure(ConfigureApp)
.Build();
}

Expand All @@ -158,7 +165,7 @@ private void ConfigureServices(IServiceCollection services)
options.ForwardedHeaders = ForwardedHeaders.All;

// Only loopback proxies are allowed by default, clear that restriction
options.KnownNetworks.Clear();
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
});
}
Expand All @@ -172,7 +179,7 @@ private void ConfigureApp(IApplicationBuilder app)
IWebHostEnvironment host = app.ApplicationServices.GetService<IWebHostEnvironment>();
host.WebRootFileProvider = compositeFileProvider;

if (_liveReloadServer is object)
if (_liveReloadServer != null)
{
// Inject LiveReload script tags to HTML documents, needs to run first as it overrides output stream
app.UseScriptInjection($"{VirtualDirectory ?? string.Empty}/livereload.js?host=localhost&port={Port}");
Expand Down Expand Up @@ -237,7 +244,7 @@ private void ConfigureApp(IApplicationBuilder app)

public async Task TriggerReloadAsync()
{
if (_liveReloadServer is object)
if (_liveReloadServer != null)
{
await _liveReloadServer.SendReloadMessageAsync();
}
Expand Down
4 changes: 1 addition & 3 deletions src/Statiq.Web.Hosting/Statiq.Web.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="wwwroot\livereload.js" />
Expand Down
3 changes: 2 additions & 1 deletion src/Statiq.Web.Netlify/Statiq.Web.Netlify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NetlifySharp" Version="1.1.0" />
<PackageReference Include="NetlifySharp" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

<Choose>
Expand Down
2 changes: 1 addition & 1 deletion src/Statiq.Web/IExecutionContextXrefExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static string GetXrefLink(this IExecutionContext context, string xref, bo
.SelectMany(x => context.Outputs.FromPipeline(x).Select(y => (PipelineName: x, Document: y)))
.Select(x => (x.Document.GetString(WebKeys.Xref), x))
.Where(x => x.Item1 is object)
.GroupBy(x => x.Item1, x => x.Item2, StringComparer.OrdinalIgnoreCase)
.GroupBy(x => x.Item1, x => x.x, StringComparer.OrdinalIgnoreCase)
.ToDictionary(x => x.Key, x => (ICollection<(string, IDocument)>)x.ToArray(), StringComparer.OrdinalIgnoreCase);
}
}
5 changes: 4 additions & 1 deletion src/Statiq.Web/Statiq.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Buildalyzer.Workspaces" Version="4.1.2" />
<PackageReference Include="Buildalyzer.Workspaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Build" Version="18.0.2" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="18.0.2" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="18.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Statiq.Web/Templates/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace Statiq.Web
{
#pragma warning disable CA1710 // Rename to TemplatesDictionary or TemplatesCollection
public class Templates : IReadOnlyList<Template>, IReadOnlyDictionary<string, Template>
#pragma warning restore CA1710 // Rename to TemplatesDictionary or TemplatesCollection
{
private readonly List<KeyValuePair<string, Template>> _templates = new List<KeyValuePair<string, Template>>();

Expand Down
4 changes: 2 additions & 2 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<HasRuntimeOutput>true</HasRuntimeOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="6.0.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public async Task DoesNotReturnsFileWithNonDefaultExtension()
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
body.ShouldBe(string.Empty);
}

#pragma warning disable ASPDEPR004 // WebHostBuilder Obsolete - needs to be replaced with WebApplicationFactory
#pragma warning disable ASPDEPR008 // WebHostBuilder Obsolete - needs to be replaced with WebApplicationFactory
private TestServer GetServer(DefaultExtensionsOptions options) =>
new TestServer(
new WebHostBuilder()
Expand Down
Loading
Loading