|
1 | 1 | using System.Text.Json; |
| 2 | +using Docs.Web; |
2 | 3 |
|
3 | 4 | var builder = WebApplication.CreateBuilder(args); |
4 | 5 |
|
|
7 | 8 | // Add response compression |
8 | 9 | builder.Services.AddResponseCompression(); |
9 | 10 |
|
| 11 | +// Content negotiation middleware |
| 12 | +builder.Services.AddTransient<ContentNegotiationMiddleware>(); |
| 13 | + |
10 | 14 | var app = builder.Build(); |
11 | 15 |
|
12 | 16 | app.MapDefaultEndpoints(); |
|
52 | 56 | app.UseResponseCompression(); |
53 | 57 |
|
54 | 58 | // Content negotiation: serve .md file when Accept: text/markdown |
55 | | -app.Use(async (context, next) => |
56 | | -{ |
57 | | - var accept = context.Request.Headers.Accept.ToString(); |
58 | | - if (accept.Contains("text/markdown", StringComparison.OrdinalIgnoreCase)) |
59 | | - { |
60 | | - var webHostEnvironment = context.RequestServices.GetRequiredService<IWebHostEnvironment>(); |
61 | | - var requestPath = context.Request.Path.Value?.TrimEnd('/') ?? ""; |
62 | | - |
63 | | - // Try the exact path with .md extension, then index.md inside the directory |
64 | | - var candidates = new[] |
65 | | - { |
66 | | - Path.Combine(webHostEnvironment.WebRootPath, requestPath.TrimStart('/') + ".md"), |
67 | | - Path.Combine(webHostEnvironment.WebRootPath, requestPath.TrimStart('/'), "index.md") |
68 | | - }; |
69 | | - |
70 | | - foreach (var mdPath in candidates) |
71 | | - { |
72 | | - if (File.Exists(mdPath)) |
73 | | - { |
74 | | - context.Response.ContentType = "text/markdown; charset=utf-8"; |
75 | | - context.Response.Headers["content-signal"] = "ai-train=yes, search=yes, ai-input=yes"; |
76 | | - await context.Response.SendFileAsync(mdPath); |
77 | | - return; |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - await next(); |
83 | | -}); |
| 59 | +app.UseMiddleware<ContentNegotiationMiddleware>(); |
84 | 60 |
|
85 | 61 | // Add trailing slash redirect middleware (replicate nginx behavior) |
86 | 62 | app.Use(async (context, next) => |
|
0 commit comments