Skip to content

Commit

Permalink
[add] request handling pipeline with handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
i4004 committed Apr 6, 2024
1 parent 2838bf6 commit 7cd772d
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/Simplify.Web/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Simplify.Web.Bootstrapper;
using Simplify.Web.Core2;
using Simplify.Web.Middleware;

namespace Simplify.Web;
Expand Down Expand Up @@ -98,9 +98,9 @@ public static IApplicationBuilder UseSimplifyWebNonTerminalWithoutRegistrations(
private static void RegisterAsNonTerminal(this IApplicationBuilder builder) =>
builder.Use(async (context, next) =>
{
var result = await SimplifyWebRequestMiddleware.InvokeAsNonTerminal(context);
await SimplifyWebRequestMiddleware.InvokeAsNonTerminal(context);

if (result == RequestHandlingStatus.Unhandled)
if (context.Response.StatusCode == (int)HttpStatusCode.NotFound)
await next.Invoke();
});

Expand Down
16 changes: 16 additions & 0 deletions src/Simplify.Web/Core2/PageComposition/IPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using Simplify.Web.Core2.Http;

namespace Simplify.Web.Core2.PageComposition;

/// <summary>
/// Represent web-page processor.
/// </summary>
public interface IPageRenderer
{
/// <summary>
/// Processes (build web-page and send to client, process current page state) the current web-page.
/// </summary>
/// <param name="context">The context.</param>
Task Render(IHttpContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Simplify.Web.Core2.Http;
using Simplify.Web.Core2.PageComposition;

namespace Simplify.Web.Core2.RequestHandling.Handlers;

public class PageRenderingHandler(IPageRenderer renderer) : IRequestHandler

Check warning on line 7 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler'

Check warning on line 7 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler.PageRenderingHandler(IPageRenderer)'

Check warning on line 7 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler'

Check warning on line 7 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler.PageRenderingHandler(IPageRenderer)'
{
public async Task Execute(IHttpContext context, RequestHandler next)

Check warning on line 9 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler.Execute(IHttpContext, RequestHandler)'

Check warning on line 9 in src/Simplify.Web/Core2/RequestHandling/Handlers/PageRenderingHandler.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'PageRenderingHandler.Execute(IHttpContext, RequestHandler)'
{
await renderer.Render(context);
await next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using Simplify.Web.Core2.Http;
using Simplify.Web.Core2.StaticFiles;

namespace Simplify.Web.Core2.RequestHandling.Handlers;

public class StaticFilesHandler(IStaticFileRequestHandler handler) : IRequestHandler
{
public async Task Execute(IHttpContext context, RequestHandler next)
{
if (handler.IsStaticFileRoutePath(context))
await handler.Execute(context);
else
await next();
}
}
9 changes: 9 additions & 0 deletions src/Simplify.Web/Core2/RequestHandling/IRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;
using Simplify.Web.Core2.Http;

namespace Simplify.Web.Core2.RequestHandling;

public interface IRequestHandler
{
Task Execute(IHttpContext context, RequestHandler next);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Simplify.Web.Core2.RequestHandling;

public interface IRequestHandlingPipeline
{
Task<RequestHandlingStatus> Execute(IHttpContext context);
Task Execute(IHttpContext context);
}
5 changes: 5 additions & 0 deletions src/Simplify.Web/Core2/RequestHandling/RequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Threading.Tasks;

namespace Simplify.Web.Core2.RequestHandling;

public delegate Task RequestHandler();
17 changes: 0 additions & 17 deletions src/Simplify.Web/Core2/RequestHandlingStatus.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Simplify.Web/Core2/ScopeRequestProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public static class ScopeRequestProcessExtensions
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="context">The context.</param>
public static Task<RequestHandlingStatus> ProcessRequest(this ILifetimeScope scope, IHttpContext context) =>
public static Task ProcessRequest(this ILifetimeScope scope, IHttpContext context) =>
scope.Resolver.Resolve<IRequestHandlingPipeline>().Execute(context);
}
24 changes: 24 additions & 0 deletions src/Simplify.Web/Core2/StaticFiles/IStaticFileRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading.Tasks;
using Simplify.Web.Core2.Http;

namespace Simplify.Web.Core2.StaticFiles;

/// <summary>
/// Represent static files request handler.
/// </summary>
public interface IStaticFileRequestHandler
{
/// <summary>
/// Determines whether current route path is for static file.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
bool IsStaticFileRoutePath(IHttpContext context);

/// <summary>
/// Processes the HTTP request for static files.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
Task Execute(IHttpContext context);
}
12 changes: 5 additions & 7 deletions src/Simplify.Web/Middleware/SimplifyWebRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ public static class SimplifyWebRequestMiddleware
/// </summary>
public static event TraceEventHandler? OnTrace;

public static Task<RequestHandlingStatus> InvokeAsTerminal(Microsoft.AspNetCore.Http.HttpContext context) => Invoke(context, true);
public static Task InvokeAsTerminal(HttpContext context) => Invoke(context, true);

public static Task<RequestHandlingStatus> InvokeAsNonTerminal(Microsoft.AspNetCore.Http.HttpContext context) => Invoke(context, false);
public static Task InvokeAsNonTerminal(HttpContext context) => Invoke(context, false);

/// <summary>
/// Process an individual request.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="isTerminalMiddleware">if set to <c>true</c> [is terminal middleware].</param>
public static async Task<RequestHandlingStatus> Invoke(Microsoft.AspNetCore.Http.HttpContext context, bool isTerminalMiddleware)
public static async Task Invoke(HttpContext context, bool isTerminalMiddleware)
{
using var scope = BootstrapperFactory.ContainerProvider.BeginLifetimeScope();

var localContext = new Core2.Http.HttpContext(context, isTerminalMiddleware);

try
{
return await scope.StartMeasurements()
await scope.StartMeasurements()
.Trace(context, OnTrace)
.SetupProviders(localContext)
.ProcessRequest(localContext);
Expand All @@ -63,8 +63,6 @@ public static async Task<RequestHandlingStatus> Invoke(Microsoft.AspNetCore.Http
}

await context.WriteErrorResponse(scope, e);

return RequestHandlingStatus.Handled;
}
}

Expand All @@ -78,7 +76,7 @@ internal static bool ProcessOnException(Exception e)
return true;
}

private static async Task WriteErrorResponse(this Microsoft.AspNetCore.Http.HttpContext context, ILifetimeScope scope, Exception e)
private static async Task WriteErrorResponse(this HttpContext context, ILifetimeScope scope, Exception e)
{
var webContext = scope.Resolver.Resolve<IWebContextProvider>().Get();

Expand Down

0 comments on commit 7cd772d

Please sign in to comment.