Skip to content

Instantly share code, notes, and snippets.

@yreynhout
Created October 3, 2018 14:20
Show Gist options
  • Save yreynhout/36297e668e5b790b3c1624cc47367b5f to your computer and use it in GitHub Desktop.
Save yreynhout/36297e668e5b790b3c1624cc47367b5f to your computer and use it in GitHub Desktop.
Little HAL
<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>
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.");
}
};
}
}
@thoradiana
Copy link

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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment