-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
128 lines (107 loc) · 5.71 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using pwr_msi.Models;
using pwr_msi.Serialization;
using pwr_msi.Services;
namespace pwr_msi {
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
private IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
var appConfig = AppConfig.FromConfiguration(Configuration);
services.AddSingleton(appConfig);
services.AddScoped<AuthService, AuthService>();
services.AddScoped<AccountEmailService, AccountEmailService>();
services.AddScoped<AdminCommonService, AdminCommonService>();
services.AddScoped<MenuService, MenuService>();
services.AddScoped<OrderDetailsService, OrderDetailsService>();
services.AddScoped<OrderTaskService, OrderTaskService>();
services.AddScoped<PaymentService, PaymentService>();
services.AddScoped<S3Service, S3Service>();
services.AddLocalization(setupAction: options => options.ResourcesPath = "Resources");
services.AddHttpClient();
services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization().AddNewtonsoftJson(s => {
// s.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
s.SerializerSettings.DateParseHandling = DateParseHandling.None;
s.SerializerSettings.FloatParseHandling = FloatParseHandling.Decimal;
s.SerializerSettings.Converters.Add(
new StringEnumConverter(new UpperSnakeCaseNamingStrategy(), allowIntegerValues: false)
);
s.SerializerSettings.Converters.Add(
new ZonedDateTimeJsonConverter()
);
s.SerializerSettings.Converters.Add(
new NullableZonedDateTimeJsonConverter()
);
});
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration: configuration => { configuration.RootPath = "ClientApp/dist"; });
services.AddDbContext<MsiDbContext>(optionsAction: options =>
options
.UseNpgsql(appConfig.DbConnectionString,
npgsqlOptionsAction: o => o.UseNodaTime().EnableRetryOnFailure().UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery))
);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(configureOptions: o => {
o.Authority = appConfig.ServerAddress;
o.Audience = appConfig.ServerAddress;
o.SaveToken = true;
o.RequireHttpsMetadata = false;
o.TokenValidationParameters = appConfig.JwtValidationParameters;
o.Configuration = new OpenIdConnectConfiguration {Issuer = appConfig.ServerAddress};
});
services.AddStackExchangeRedisCache(options => {
options.Configuration = appConfig.RedisConnectionString;
options.InstanceName = appConfig.RedisInstanceName;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) {
if (Configuration.GetValue<string>(key: "JWT_KEY", defaultValue: null) == null)
logger.LogWarning(message: "No JWT_KEY provided, using random value!");
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler(errorHandlingPath: "/Error");
var supportedCultures = new[] {"en-US", "pl"};
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture(defaultCulture: supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseStaticFiles();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseMiddleware<AuthMiddleware>();
app.UseEndpoints(configure: endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action=Index}/{id?}");
});
app.UseSpa(configuration: spa => {
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment()) spa.UseAngularCliServer(npmScript: "start");
});
}
}
}