📄
Program.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System; using System.IO; using System.Net.Mime; using System.Threading; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using MSearch.Domain; using MSearch.SearchProviders.GitHub; using MSearch.SearchProviders.HackerNews; using MSearch.SearchProviders.MicrosoftLearn; using MSearch.SearchProviders.MozillaDeveloperNetwork; using MSearch.SearchProviders.OpenStreetMap; using MSearch.SearchProviders.Reddit; using MSearch.SearchProviders.Spotify; using MSearch.SearchProviders.StackExchange; using MSearch.SearchProviders.TheMovieDb; using MSearch.SearchProviders.Wikipedia; using MSearch.SearchProviders.YouTube; using MSearch.Web.Components; var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); builder .Services.AddSearchService() .AddGitHubSearchProvider() .AddHackerNewsSearchProvider() .AddMicrosoftLearnSearchProvider() .AddMozillaDeveloperNetworkSearchProvider() .AddOpenStreetMapSearchProvider() .AddRedditSearchProvider() .AddSpotifySearchProvider() .AddStackExchangeSearchProvider("stackoverflow") .AddTheMovieDbSearchProvider() .AddWikipediaSearchProvider("en") .AddYouTubeSearchProvider(); builder.Services.AddHttpContextAccessor(); builder.Services.AddRazorComponents(); var app = builder.Build(); app.UseStaticFiles(); app.MapGet( "/", async ( [FromServices] IServiceProvider serviceProvider, [FromServices] ILoggerFactory loggerFactory, [FromServices] SearchService searchService, HttpResponse response, [FromQuery(Name = "q")] string? query = null, CancellationToken cancellationToken = default ) => { response.Headers.ContentType = MediaTypeNames.Text.Html; await using var bodyWriter = new StreamWriter(response.Body); await bodyWriter.WriteAsync( """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <base href="/" /> <title>MSearch</title> <link rel="stylesheet" href="app.css" /> <link rel="search" type="application/opensearchdescription+xml" href="opensearch" title="MSearch"> </head> <body> <main> """.AsMemory(), cancellationToken ); await using var htmlRenderer = new StaticHtmlRenderer(serviceProvider, loggerFactory); await htmlRenderer.Dispatcher.InvokeAsync(async () => { { var output = htmlRenderer.BeginRenderingComponent(new LayoutComponent(), ParameterView.Empty); await bodyWriter.WriteAsync(output.ToHtmlString().AsMemory(), cancellationToken); } if (string.IsNullOrWhiteSpace(query)) { return; } await foreach (var result in searchService.Search(query, cancellationToken)) { var output = htmlRenderer.BeginRenderingComponent( new SearchResultComponent(result), ParameterView.Empty ); await bodyWriter.WriteAsync(output.ToHtmlString().AsMemory(), cancellationToken); } }); } ); app.MapGet("/opensearch", () => new RazorComponentResult<OpenSearchDescriptor>()); app.MapDefaultEndpoints(); app.Run();