This repository was archived by the owner on Oct 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathStartup.cs
151 lines (112 loc) · 5.03 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
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http.Extensions;
using Serilog;
using RazorPartialToString.Services;
using Microsoft.AspNetCore.HttpOverrides;
using Npgsql.Logging;
using System.Linq;
namespace budoco
{
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
bd_util.log("Startup");
Configuration = configuration;
bd_util.log(Configuration["Budoco:DebugWhatEnvIsThis"]);
//NpgsqlLogManager.Provider = new ConsoleLoggingProvider(NpgsqlLogLevel.Debug, false, false);
NpgsqlLogManager.Provider = new budoco_pg.bd_pg_log_provider();
bd_db.update_db_schema(env.ContentRootPath);
// If there are pending outgoing emails try to send them.
bd_email.spawn_email_sending_thread();
bd_email.spawn_email_receiving_thread();
bd_email.spawn_registration_request_expiration_thread();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
bd_util.log("ConfigureServices");
// services.AddDistributedMemoryCache();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
// options.CheckConsentNeeded = context => false; // Default is true, make it false
//options.CheckConsentNeeded = false;
// So that if we click a budoco link in email, it works
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddHttpContextAccessor();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(1);
options.Cookie.IsEssential = true;
options.Cookie.SameSite = SameSiteMode.Lax;
});
services.AddRazorPages();
services.AddTransient<IRazorPartialToStringRenderer, RazorPartialToStringRenderer>();
// if nginx is not on same machine
// services.Configure<ForwardedHeadersOptions>(options =>
// {
// options.KnownProxies.Add(IPAddress.Parse("10.0.0.100"));
// });
// for screenshots
//services.AddCors();
// services.Configure<ApiBehaviorOptions>(options =>
// {
// options.SuppressModelStateInvalidFilter = true;
// });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
bd_util.log("Configure");
//app.UseCors();
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0
// for nginx
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
//if (env.IsDevelopment())
//{
if (bd_config.get(bd_config.UseDeveloperExceptionPage) == 1)
{
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.UseSession();
// for redirecting https to http - commented out because nginx is doing this for us and it's annoying in dev
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
//app.UseAuthorization();
app.UseSerilogRequestLogging();
// corey added a step in the pipeline
app.Use(async (context, next) =>
{
bd_util.log("Startup.cs URL: " + context.Request.GetDisplayUrl());
bool allowed = bd_util.check_user_permissions(context);
if (allowed)
{
bd_db.query_count = 0; // just to see how many queries we do on busy pages. Not threadsafe, but doesn't matter.
await next.Invoke();
}
});
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}