-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathFortunesRawMiddleware.cs
45 lines (35 loc) · 1.3 KB
/
FortunesRawMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text.Encodings.Web;
using Benchmarks.Configuration;
using Benchmarks.Data;
namespace Benchmarks.Middleware;
public class FortunesRawMiddleware
{
private static readonly PathString _path = new(Scenarios.GetPath(s => s.DbFortunesRaw));
private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;
public FortunesRawMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
var db = httpContext.RequestServices.GetService<RawDb>();
var rows = await db.LoadFortunesRows();
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder);
return;
}
await _next(httpContext);
}
}
public static class FortunesRawMiddlewareExtensions
{
public static IApplicationBuilder UseFortunesRaw(this IApplicationBuilder builder)
{
return builder.UseMiddleware<FortunesRawMiddleware>();
}
}