-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStartup.cs
175 lines (154 loc) · 7 KB
/
Startup.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
namespace Blazor.Contentful_.Blog.Starter
{
using System.Collections.Generic;
using System.Globalization;
using Blazor.Contentful_.Blog.Starter.CacheBusting.Api;
using Blazor.Contentful_.Blog.Starter.CacheBusting.Manual;
using Blazor.Contentful_.Blog.Starter.ContentfulSdk.Api;
using Blazor.Contentful_.Blog.Starter.ContentfulSdk.Renderer;
using Blazor.Contentful_.Blog.Starter.ContentfulSdk.Sdk;
using Blazor.Contentful_.Blog.Starter.Data;
using Blazor.Contentful_.Blog.Starter.FeedGeneration.Api;
using Blazor.Contentful_.Blog.Starter.FeedGeneration.Generators;
using Blazor.Contentful_.Blog.Starter.Localization;
using Blazor.Contentful_.Blog.Starter.PageMetadataGeneration.Api;
using Blazor.Contentful_.Blog.Starter.PageMetadataGeneration.Genereators;
using Blazor.Contentful_.Blog.Starter.SitemapGeneration.Api;
using Blazor.Contentful_.Blog.Starter.SitemapGeneration.Generators;
using Contentful.AspNetCore;
using EventHorizon.Blazor.DocumentMetadata;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddTransient<PageMetadataGenerator, StaticPageMetadataGenerator>()
.AddDocumentMetadata(async (serviceProvider, registrator) =>
{
var localizer = serviceProvider.GetRequiredService<IStringLocalizer<LocalizationResource>>();
var generator = serviceProvider.GetRequiredService<PageMetadataGenerator>();
await generator.Generate(
registrator,
localizer
);
});
// Setup Sitemap Services
services.AddSingleton<SitemapGenerator, DynamicSitemapGenerator>();
// Setup RSS Feed Services
services.AddSingleton<RssFeedGenerator, ContentfulRssFeedGenerator>();
// I18n Services
// This registers the supported Locales for localization.
// As more platform locales are supported, Localization Resource files can be added in
// ~/Resources/Localization/LocalizationResource-**.resx.
services
.AddLocalization(options => options.ResourcesPath = "Resources")
.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
// Set Supported Locales
new CultureInfo("en-US"),
};
opts.DefaultRequestCulture = new RequestCulture("en-US");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
}
);
// Setup Site and Contentful Services
services.Configure<SiteConfig>(Configuration);
services.AddContentful(Configuration);
services.AddSingleton<ContentfulApi, SdkContentfulApi>();
services.AddSingleton<ContentfulHtmlRenderer>();
// Setup Cache Busting
services.AddSingleton<CacheBuster, ManualCacheBuster>()
// These are registered manually, as more services are added that have a cache,
// they should follow the patterns here.
.AddTransient<BustCache>(
services => services.GetRequiredService<ContentfulApi>()
).AddTransient<BustCache>(
services => services.GetRequiredService<SitemapGenerator>()
).AddTransient<BustCache>(
services => services.GetRequiredService<PageMetadataGenerator>()
);
}
private int IStringLocalizer<T>()
{
throw new System.NotImplementedException();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDocumentMetadata();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
// Here we register <domain>/sitemap.xml to return the generated Sitemap
endpoints.MapGet(
"/sitemap.xml",
async context =>
{
await context.Response.WriteAsync(
await context.RequestServices
.GetRequiredService<SitemapGenerator>()
.Generate()
);
}
);
// Here we register <domain>/feed.xml to return the generated RSS Feed
endpoints.MapGet(
"/feed.xml",
async context =>
{
await context.Response.WriteAsync(
await context.RequestServices
.GetRequiredService<RssFeedGenerator>()
.Generate()
);
}
);
// This is our cache busting endpoint that when called will trigger the CacheBuster service.
// This can be registered in Contentful, so any changes in Contentful will trigger clear the cache.
endpoints.MapPost(
"/webhook/cache-buster",
async context =>
{
await context.RequestServices
.GetRequiredService<CacheBuster>()
.BustCache();
await context.Response.WriteAsync("Ok");
}
);
endpoints.MapFallbackToPage("/_Host");
});
}
}
}