-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartupConfigure.cs
171 lines (154 loc) · 5.95 KB
/
StartupConfigure.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AccountsData.Models.DataModels;
using Amazon.S3;
using CanonicalEmails;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.CookiePolicy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
using SameSiteMode = Microsoft.AspNetCore.Http.SameSiteMode;
namespace VLO_BOARDS
{
public partial class Startup
{
/// This method configures the app
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AmazonS3Client minioClient, MinioConfig minioConfig)
{
var forwardOptions = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
// Needed because of mixing http and https.
RequireHeaderSymmetry = false,
};
// Accept X-Forwarded-* headers from all sources.
forwardOptions.KnownNetworks.Clear();
forwardOptions.KnownProxies.Clear();
app.UseForwardedHeaders(forwardOptions);
if (env.IsDevelopment())
{
app.UseCookiePolicy(new CookiePolicyOptions
{
MinimumSameSitePolicy = SameSiteMode.None
});
}
else
{
app.UseCookiePolicy(new CookiePolicyOptions
{
Secure = CookieSecurePolicy.Always,
HttpOnly = HttpOnlyPolicy.Always,
MinimumSameSitePolicy = SameSiteMode.Lax
});
}
IS4Utils.InitializeDatabase(app, new Config(env));
EnsureBucketsExits(minioClient, minioConfig).Wait();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "VLO Boards API v1");
});
app.UseReDoc(c =>
{
c.DocumentTitle = "Dokumentacja API Vlo Boards, dostępna również pod /swagger/";
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
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();
}
Normalizer.ConfigureDefaults(new NormalizerSettings
{
RemoveDots = true,
RemoveTags = true,
LowerCase = true,
NormalizeHost = true
});
app.UseRouting();
app.UseIdentityServer();
app.UseCors("DefaultExternalOrigins");
app.UseAuthorization();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
if (ctx.Context.Request.Path.StartsWithSegments("/static"))
{
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(365)
};
}
else
{
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(0),
NoCache = true,
NoStore = true
};
}
}
});
app.UseRateLimiter();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "/api/{area:exists}/{controller}/").RequireRateLimiting(slidingPolicy).RequireCors("DefaultExternalOrigins");
endpoints.MapControllerRoute(
name: "default",
pattern: "/api/{controller}/{action=Index}/{id?}").RequireRateLimiting(slidingPolicy).RequireCors("DefaultExternalOrigins");
if (!env.IsDevelopment())
{
endpoints.MapFallbackToFile("index.html");
}
});
if (env.IsDevelopment())
{
app.UseSpa(a =>
{
a.UseProxyToSpaDevelopmentServer("http://localhost:3000");
});
}
else
{
app.UseSentryTracing();
}
}
public static async Task EnsureBucketsExits(AmazonS3Client client, MinioConfig minioConfig)
{
var buckets = await client.ListBucketsAsync();
var bucketNames = new List<string>();
foreach (var bucket in buckets.Buckets)
{
bucketNames.Add(bucket.BucketName);
}
if (!bucketNames.Contains(minioConfig.BucketName))
{
await client.PutBucketAsync(minioConfig.BucketName);
}
if (!bucketNames.Contains(minioConfig.VideoBucketName))
{
await client.PutBucketAsync(minioConfig.VideoBucketName);
}
}
}
}