Created
October 3, 2018 14:20
-
-
Save yreynhout/36297e668e5b790b3c1624cc47367b5f to your computer and use it in GitHub Desktop.
Little HAL
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" /> | |
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.1" /> | |
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> | |
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" /> | |
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | |
<PackageReference Include="System.IO.Pipelines" Version="4.5.1" /> | |
<!-- this one is on the CI feed --> | |
<PackageReference Include="SqlStreamStore.HAL" Version="1.0.0-rc2-build00035" /> | |
</ItemGroup> | |
</Project> |
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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.DependencyInjection; | |
using Newtonsoft.Json; | |
using Serilog; | |
using SqlStreamStore; | |
using SqlStreamStore.HAL; | |
using SqlStreamStore.Streams; | |
using MidFunc = System.Func< | |
Microsoft.AspNetCore.Http.HttpContext, | |
System.Func<System.Threading.Tasks.Task>, | |
System.Threading.Tasks.Task | |
>; | |
namespace Example | |
{ | |
class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Debug() | |
.Enrich.FromLogContext() | |
.WriteTo.Console() | |
.CreateLogger(); | |
using(var streamStore = new InMemoryStreamStore()) | |
using(var host = new WebHostBuilder() | |
.UseKestrel() | |
.UseStartup(new Startup(streamStore, new SqlStreamStoreMiddlewareOptions | |
{ | |
UseCanonicalUrls = false | |
})) | |
.UseSerilog() | |
.Build()) | |
{ | |
// Simulating some messages in the store here (only one stream) | |
var random = new Random(); | |
var id = new Guid("cbf68be34d9547eb9b4a390fd2aa417b"); | |
var messages = new NewStreamMessage[random.Next(1000, 2000)]; | |
for(var index = 0; index < messages.Length; index++) | |
{ | |
messages[index] = new NewStreamMessage( | |
Guid.NewGuid(), | |
"ItineraryPublished", | |
JsonConvert.SerializeObject(new { | |
ItineraryId = id, | |
Data = index | |
}) | |
); | |
} | |
await streamStore.AppendToStream(id.ToString("N"), ExpectedVersion.NoStream, messages); | |
// bootstrapping the server | |
var source = new CancellationTokenSource(); | |
var serverTask = host.RunAsync(source.Token); | |
// press enter to exit | |
Console.WriteLine("Running ..."); | |
Console.ReadLine(); | |
source.Cancel(); | |
await serverTask; | |
} | |
} | |
} | |
internal static class WebHostBuilderExtensions | |
{ | |
public static IWebHostBuilder UseStartup(this IWebHostBuilder builder, IStartup startup) | |
=> builder | |
.ConfigureServices(services => services.AddSingleton(startup)) | |
.UseSetting(WebHostDefaults.ApplicationKey, startup.GetType().AssemblyQualifiedName); | |
} | |
internal class Startup : IStartup | |
{ | |
private readonly IStreamStore _streamStore; | |
private readonly SqlStreamStoreMiddlewareOptions _options; | |
public Startup( | |
IStreamStore streamStore, | |
SqlStreamStoreMiddlewareOptions options) | |
{ | |
_streamStore = streamStore; | |
_options = options; | |
} | |
public IServiceProvider ConfigureServices(IServiceCollection services) => services | |
.AddResponseCompression(options => options.MimeTypes = new[] { "application/hal+json" }) | |
.BuildServiceProvider(); | |
public void Configure(IApplicationBuilder app) => app | |
.UseResponseCompression() | |
.Use(CatchAndDisplayErrors) | |
.UseSqlStreamStoreHal(_streamStore, _options); | |
private static MidFunc CatchAndDisplayErrors => async (context, next) => | |
{ | |
try | |
{ | |
await next(); | |
} | |
catch(Exception ex) | |
{ | |
Log.Warning(ex, "Error during request."); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I'm Thora Diana Hempel, how are you? I hope you are good to your family. Please kindly respond to my email([email protected])