Skip to content

Commit

Permalink
Merge pull request SQLStreamStore#20 from thefringeninja/optional-red…
Browse files Browse the repository at this point in the history
…irects

Make 308 Redirects Optional
  • Loading branch information
thefringeninja committed Sep 20, 2018
2 parents 98d7022 + b3c654d commit a4605d3
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/SqlStreamStore.HAL.DevServer/DevServerStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
internal class DevServerStartup : IStartup
{
private readonly IStreamStore _streamStore;
private readonly SqlStreamStoreMiddlewareOptions _options;

public DevServerStartup(IStreamStore streamStore)
public DevServerStartup(
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options)
{
_streamStore = streamStore;
_options = options;
}

public IServiceProvider ConfigureServices(IServiceCollection services) => services
Expand All @@ -31,7 +35,7 @@ public DevServerStartup(IStreamStore streamStore)
.Use(VaryAccept)
.Use(CatchAndDisplayErrors)
.UseSqlStreamStoreBrowser()
.UseSqlStreamStoreHal(_streamStore);
.UseSqlStreamStoreHal(_streamStore, _options);

private static MidFunc CatchAndDisplayErrors => async (context, next) =>
{
Expand Down
6 changes: 5 additions & 1 deletion src/SqlStreamStore.HAL.DevServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class Program : IDisposable
private readonly IConfigurationRoot _configuration;

private bool Interactive => _configuration.GetValue<bool>("interactive");
private bool UseCanonicalUrls => _configuration.GetValue<bool>("canonical");

public static async Task<int> Main(string[] args)
{
Expand Down Expand Up @@ -47,7 +48,10 @@ private async Task<int> Run()
using(var streamStore = await SqlStreamStoreFactory.Create())
using(var host = new WebHostBuilder()
.UseKestrel()
.UseStartup(new DevServerStartup(streamStore))
.UseStartup(new DevServerStartup(streamStore, new SqlStreamStoreMiddlewareOptions
{
UseCanonicalUrls = UseCanonicalUrls
}))
.UseSerilog()
.Build())
{
Expand Down
2 changes: 1 addition & 1 deletion src/SqlStreamStore.HAL/AllStreamOptionsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class AllStreamOptionsMiddleware
{
public static IApplicationBuilder UseAllStreamOptions(this IApplicationBuilder builder, IStreamStore streamStore)
{
var allStream = new AllStreamResource(streamStore);
var allStream = new AllStreamResource(streamStore, false);
var allStreamMessages = new AllStreamMessageResource(streamStore);

return builder
Expand Down
2 changes: 1 addition & 1 deletion src/SqlStreamStore.HAL/AppendStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class AppendStreamMiddleware
{
public static IApplicationBuilder UseAppendStream(this IApplicationBuilder builder, IStreamStore streamStore)
{
var stream = new StreamResource(streamStore);
var stream = new StreamResource(streamStore, false);

return builder.MapWhen(IsStream, inner => inner.Use(AppendStream(stream)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/SqlStreamStore.HAL/DeleteStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class DeleteStreamMiddleware
{
public static IApplicationBuilder UseDeleteStream(this IApplicationBuilder builder, IStreamStore streamStore)
{
var streams = new StreamResource(streamStore);
var streams = new StreamResource(streamStore, false);
var streamMessages = new StreamMessageResource(streamStore);

return builder
Expand Down
7 changes: 5 additions & 2 deletions src/SqlStreamStore.HAL/ReadAllStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ namespace SqlStreamStore.HAL

internal static class ReadAllStreamMiddleware
{
public static IApplicationBuilder UseReadAllStream(this IApplicationBuilder builder, IStreamStore streamStore)
public static IApplicationBuilder UseReadAllStream(
this IApplicationBuilder builder,
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options)
{
var allStream = new AllStreamResource(streamStore);
var allStream = new AllStreamResource(streamStore, options.UseCanonicalUrls);
var allStreamMessages = new AllStreamMessageResource(streamStore);

return builder
Expand Down
7 changes: 5 additions & 2 deletions src/SqlStreamStore.HAL/ReadStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ namespace SqlStreamStore.HAL

internal static class ReadStreamMiddleware
{
public static IApplicationBuilder UseReadStream(this IApplicationBuilder builder, IStreamStore streamStore)
public static IApplicationBuilder UseReadStream(
this IApplicationBuilder builder,
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options)
{
var streams = new StreamResource(streamStore);
var streams = new StreamResource(streamStore, options.UseCanonicalUrls);
var streamMessages = new StreamMessageResource(streamStore);

return builder
Expand Down
6 changes: 4 additions & 2 deletions src/SqlStreamStore.HAL/Resources/AllStreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
internal class AllStreamResource : IResource
{
private readonly IStreamStore _streamStore;
private readonly bool _useCanonicalUrls;

public HttpMethod[] Allowed { get; } =
{
Expand All @@ -20,18 +21,19 @@ internal class AllStreamResource : IResource
HttpMethod.Options
};

public AllStreamResource(IStreamStore streamStore)
public AllStreamResource(IStreamStore streamStore, bool useCanonicalUrls)
{
if(streamStore == null)
throw new ArgumentNullException(nameof(streamStore));
_streamStore = streamStore;
_useCanonicalUrls = useCanonicalUrls;
}

public async Task<Response> Get(
ReadAllStreamOperation operation,
CancellationToken cancellationToken)
{
if(!operation.IsUriCanonical)
if(_useCanonicalUrls && !operation.IsUriCanonical)
{
return new Response(new HALResponse(null), 308)
{
Expand Down
4 changes: 3 additions & 1 deletion src/SqlStreamStore.HAL/Resources/StreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace SqlStreamStore.HAL.Resources
internal class StreamResource : IResource
{
private readonly IStreamStore _streamStore;
private readonly bool _useCanonicalUris;

public HttpMethod[] Allowed { get; } =
{
Expand All @@ -22,11 +23,12 @@ internal class StreamResource : IResource
HttpMethod.Delete
};

public StreamResource(IStreamStore streamStore)
public StreamResource(IStreamStore streamStore, bool useCanonicalUris)
{
if(streamStore == null)
throw new ArgumentNullException(nameof(streamStore));
_streamStore = streamStore;
_useCanonicalUris = useCanonicalUris;
}

public async Task<Response> Post(
Expand Down
33 changes: 14 additions & 19 deletions src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@

public static class SqlStreamStoreHalMiddleware
{
private static readonly string[] s_NoCache =
{
"max-age=0",
"no-cache",
"must-revalidate"
};

private static readonly string[] s_CacheForOneYear =
{
"max-age=31536000"
};

private static MidFunc CaseSensitiveQueryStrings => (context, next) =>
{
if(context.Request.QueryString != QueryString.Empty)
Expand Down Expand Up @@ -81,36 +69,43 @@ public static class SqlStreamStoreHalMiddleware

public static IApplicationBuilder UseSqlStreamStoreHal(
this IApplicationBuilder builder,
IStreamStore streamStore)
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options = default)
{
if(builder == null)
throw new ArgumentNullException(nameof(builder));
if(streamStore == null)
throw new ArgumentNullException(nameof(streamStore));

options = options ?? new SqlStreamStoreMiddlewareOptions();

return builder
.UseExceptionHandling()
.Use(CaseSensitiveQueryStrings)
.Use(AcceptHalJson)
.Use(HeadRequests)
.UseIndex()
.Map("/stream", UseAllStream(streamStore))
.Map("/streams", UseStream(streamStore));
.Map("/stream", UseAllStream(streamStore, options))
.Map("/streams", UseStream(streamStore, options));
}

private static Action<IApplicationBuilder> UseStream(IStreamStore streamStore)
private static Action<IApplicationBuilder> UseStream(
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options)
=> builder => builder
.MapWhen(IsOptions, inner => inner.UseStreamOptions(streamStore))
.UseStreamMetadata(streamStore)
.UseReadStream(streamStore)
.UseReadStream(streamStore, options)
.UseAppendStream(streamStore)
.UseDeleteStream(streamStore)
.Use(MethodsNotAllowed("TRACE", "PATCH"));

private static Action<IApplicationBuilder> UseAllStream(IStreamStore streamStore)
private static Action<IApplicationBuilder> UseAllStream(
IStreamStore streamStore,
SqlStreamStoreMiddlewareOptions options)
=> builder => builder
.MapWhen(IsOptions, inner => inner.UseAllStreamOptions(streamStore))
.UseReadAllStream(streamStore)
.UseReadAllStream(streamStore, options)
.Use(MethodsNotAllowed("POST", "PUT", "DELETE", "TRACE", "PATCH"));

private static bool IsOptions(HttpContext context) => context.IsOptions();
Expand Down
7 changes: 7 additions & 0 deletions src/SqlStreamStore.HAL/SqlStreamStoreMiddlewareOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SqlStreamStore.HAL
{
public class SqlStreamStoreMiddlewareOptions
{
public bool UseCanonicalUrls { get; set; } = true;
}
}
2 changes: 1 addition & 1 deletion src/SqlStreamStore.HAL/StreamOptionsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class StreamOptionsMiddleware
{
public static IApplicationBuilder UseStreamOptions(this IApplicationBuilder builder, IStreamStore streamStore)
{
var streams = new StreamResource(streamStore);
var streams = new StreamResource(streamStore, false);
var streamMessages = new StreamMessageResource(streamStore);
var streamsMetadata = new StreamMetadataResource(streamStore);

Expand Down

0 comments on commit a4605d3

Please sign in to comment.