This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStartup.cs
112 lines (98 loc) · 3.72 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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
namespace Sby
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public readonly KeyValuePair<string, StringValues> ServiceWorkerHeader =
new KeyValuePair<string, StringValues>("service-worker", new StringValues("script"));
public readonly ISet<string> ImmutableFileExtensions = new HashSet<string> {
".css",
".js",
".woff"
};
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHttpsRedirection();
app.UseHsts();
}
app.UseXContentTypeOptions();
app.UseXfo(options => options.SameOrigin());
app.UseXXssProtection(options => options.EnabledWithBlockMode());
app.UseXRobotsTag(options => options.NoIndex().NoFollow());
app.UseReferrerPolicy(opts => opts.SameOrigin());
app.UseCsp(options => options
.FontSources(action => action.Self())
.ObjectSources(action => action.None())
.ScriptSources(action => action.Self())
.StyleSources(action => action.Self().UnsafeInline())
.FrameAncestors(action => action.Self())
);
app.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = new List<string> { "index.html" }
});
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.EndsWith("/sw.js"))
{
if (!context.Request.Headers.Contains(ServiceWorkerHeader))
{
context.Response.StatusCode = 400;
await context.Response.CompleteAsync();
return;
}
context.Response.Headers.Append("Service-Worker-Allowed", "/");
context.Response.Headers.Append("Cache-Control", "no-store");
}
await next();
});
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = (context) =>
{
if (env.IsDevelopment())
{
return;
}
if (ImmutableFileExtensions.Contains(Path.GetExtension(context.File.Name)) && context.File.Name != "sw.js")
{
context.Context.Response.Headers.Append("Cache-Control", "public,max-age=31536000,immutable");
}
else if (context.File.Name == "index.html")
{
Console.WriteLine("Getting index!");
}
}
});
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
});
}
}
}