/
Startup.cs
121 lines (104 loc) · 5.12 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using DotVVM.Framework.Binding;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Routing;
using DotVVM.Samples.BasicSamples.ViewModels.ComplexSamples.Auth;
using DotVVM.Samples.BasicSamples.ViewModels.FeatureSamples.StaticCommand;
using DotVVM.Samples.Common.ViewModels.FeatureSamples.DependencyInjection;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Prometheus;
namespace DotVVM.Samples.BasicSamples
{
public class Startup
{
public class BindingTestResolvers
{
}
public void ConfigureServices(IServiceCollection services)
{
MeterAdapter.StartListening(new MeterAdapterOptions {
ResolveHistogramBuckets = instrument => {
return DotvvmMetrics.TryGetRecommendedBuckets(instrument) ?? MeterAdapterOptions.DefaultHistogramBuckets;
}
});
services.AddAuthentication("Scheme1")
.AddCookie("Scheme1", o => {
o.LoginPath = new PathString("/ComplexSamples/Auth/Login");
o.Events = new CookieAuthenticationEvents {
OnRedirectToReturnUrl = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
OnRedirectToAccessDenied = c => DotvvmAuthenticationHelper.ApplyStatusCodeResponse(c.HttpContext, 403),
OnRedirectToLogin = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
OnRedirectToLogout = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri)
};
});
services.AddAuthentication("Scheme2")
.AddCookie("Scheme2", o => {
o.LoginPath = new PathString("/ComplexSamples/SPARedirect/login");
o.Events = new CookieAuthenticationEvents {
OnRedirectToReturnUrl = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
OnRedirectToAccessDenied = c => DotvvmAuthenticationHelper.ApplyStatusCodeResponse(c.HttpContext, 403),
OnRedirectToLogin = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
OnRedirectToLogout = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri)
};
});
services.AddAuthentication("Scheme3")
.AddCookie("Scheme3");
services.AddHealthChecks();
services.AddLocalization(o => o.ResourcesPath = "Resources");
services.AddDotVVM<DotvvmServiceConfigurator>();
services.Configure<BindingCompilationOptions>(o => {
o.TransformerClasses.Add(new BindingTestResolvers());
});
services.AddSingleton<IGreetingComputationService, HelloGreetingComputationService>();
services.AddScoped<ViewModelScopedDependency>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseAuthentication();
var config = app.UseDotVVM<DotvvmStartup>(GetApplicationPath(env), modifyConfiguration: c => {
c.RouteTable.Add("AuthorizedPresenter", "ComplexSamples/Auth/AuthorizedPresenter", provider => new AuthorizedPresenter());
if (c.ExperimentalFeatures.ExplicitAssemblyLoading.Enabled)
{
c.Markup.AddAssembly(typeof(Startup).Assembly.FullName);
c.Markup.AddAssembly("System.Security.Claims");
}
});
#if AssertConfiguration
// this compilation symbol is set by CI server
config.AssertConfigurationIsValid();
#endif
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapHealthChecks("/health");
});
app.UseStaticFiles();
app.UseEndpoints(endpoints => {
endpoints.MapDotvvmHotReload();
endpoints.MapMetrics(); // prometheus metrics on /metrics
});
}
private string GetApplicationPath(IWebHostEnvironment env)
{
if (File.Exists(Path.Combine(env.ContentRootPath, "Views/Default.dothtml")))
{
return env.ContentRootPath;
}
var common = Path.Combine(Path.GetDirectoryName(env.ContentRootPath), "Common");
if (File.Exists(Path.Combine(common, "Views/Default.dothtml")))
{
return common;
}
throw new DirectoryNotFoundException("Cannot find the 'Common' directory nor the 'Views' directory in the application root.");
}
}
}