-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
131 lines (116 loc) · 5.37 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
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Categorise.Areas.Identity;
using Categorise.Data;
using Categorise.Services;
using Npgsql;
using System.Linq;
using System.Threading.Tasks;
namespace Categorise
{
/// <summary>
/// Startup class.
/// </summary>
public class Startup
{
private static string _connectionString;
/// <summary>
/// Startup constructor.
/// </summary>
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
CurrentEnvironment = environment;
}
/// <summary>
/// Startup configuration.
/// </summary>
public IConfiguration Configuration { get; }
private IWebHostEnvironment CurrentEnvironment;
/// <summary>
/// Called by the runtime. Use this method to add services to the container.
/// </summary>
public void ConfigureServices(IServiceCollection services)
{
if (CurrentEnvironment.IsDevelopment())
{
var builder = new NpgsqlConnectionStringBuilder(
Configuration.GetConnectionString("CategoriseContext"));
builder.Username = Configuration["DB_USER"];
builder.Password = Configuration["DB_PASSWORD"];
_connectionString = builder.ConnectionString;
}
else if (CurrentEnvironment.IsProduction())
{
_connectionString = ParseConnectionUri(Configuration["DATABASE_URL"]);
}
services.AddDbContext<CategoriseContext>(options =>
options.UseNpgsql(_connectionString));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<CategoriseContext>();
services.Configure<IdentityOptions>(options =>
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddScoped<IAccountService, AccountService>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<ITransactionService, TransactionService>();
services.AddScoped<IVendorService, VendorService>();
services.AddDatabaseDeveloperPageExceptionFilter();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, CategoriseContext context, IWebHostEnvironment env)
{
CreateDefaultConfig(context);
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();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
var blockRegistration = context.ConfigSettings.Where(c => c.Name == "AllowRegistrations").FirstOrDefault().Value == "false";
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
if (blockRegistration)
{
endpoints.MapGet("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true)));
endpoints.MapPost("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true)));
}
});
}
private static string ParseConnectionUri(string connectionUri)
{
var splitString = connectionUri.Split(new string[] { "postgres://", ":", "@", "/" }, StringSplitOptions.None);
var connectionString = $"Host={splitString[3]};Database={splitString[5]};Port={splitString[4]};Username={splitString[1]};Password={splitString[2]};SSL Mode=Require;Trust Server Certificate=true";
return connectionString;
}
private void CreateDefaultConfig(CategoriseContext context)
{
ConfigSettingService configSettingService = new ConfigSettingService(context);
// Create required configs (safely).
configSettingService.CreateConfigSetting("AllowRegistrations", "false", true);
}
}
}