-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
170 lines (150 loc) · 5.93 KB
/
Program.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
using Mediachase.Commerce.Catalog;
using Serilog;
namespace CmsCommerce;
using CmsCommerce.Extensions;
using EPiServer.Cms.Shell;
using EPiServer.Cms.UI.AspNetIdentity;
using EPiServer.Scheduler;
using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;
// PROGRAM CLASS
// public abstract class Program
// {
// public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
//
// private static IHostBuilder CreateHostBuilder(string[] args)
// {
// var builder = Host.CreateDefaultBuilder(args);
// builder.ConfigureCmsDefaults();
// builder.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
// return builder;
// }
// }
public abstract class Program
{
public static void Main(string[] args)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var isDevelopment = environment == Environments.Development;
if (isDevelopment)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("App_Data/log.log", rollingInterval: RollingInterval.Day)
.CreateLogger();
}
CreateHostBuilder(args, isDevelopment).Build().Run();
}
private static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
{
if (isDevelopment)
{
return Host.CreateDefaultBuilder(args)
.ConfigureCmsDefaults()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
else
{
return Host.CreateDefaultBuilder(args)
.ConfigureCmsDefaults()
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}
}
/// <summary>
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// </summary>
// STARTUP CLASS
public class Startup
{
private readonly IWebHostEnvironment _webHostingEnvironment;
public Startup(IWebHostEnvironment webHostingEnvironment)
{
_webHostingEnvironment = webHostingEnvironment;
}
public void ConfigureServices(IServiceCollection services)
{
if (_webHostingEnvironment.IsDevelopment())
{
AppDomain.CurrentDomain.SetData("DataDirectory",
Path.Combine(_webHostingEnvironment.ContentRootPath, "App_Data"));
services.Configure<SchedulerOptions>(options => options.Enabled = false);
}
// Add cors for local development
services.AddCors(options =>
{
options.AddPolicy(name: "Local",
builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("https://localhost")
.AllowCredentials();
});
});
// Correlates to Caching in appsettings.json
services.Configure<CatalogOptions>(o =>
{
o.Cache.UseCache = true;
o.Cache.ContentVersionCacheExpiration = TimeSpan.FromMinutes(05);
o.Cache.CollectionCacheExpiration = TimeSpan.FromMinutes(05);
o.Cache.EntryCacheExpiration = TimeSpan.FromMinutes(05);
o.Cache.NodeCacheExpiration = TimeSpan.FromMinutes(05);
});
services
.AddLogging()
.AddCmsAspNetIdentity<ApplicationUser>()
.AddCms()
.AddCommerce()
.AddCmsTagHelpers()
.AddAlloy()
.AddAdminUserRegistration()
.AddEmbeddedLocalization<Startup>();
// Opti cookie security policy
services.ConfigureApplicationCookie(c => c.Cookie.SecurePolicy = _webHostingEnvironment.IsDevelopment()
? Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest
: Microsoft.AspNetCore.Http.CookieSecurePolicy.Always);
services.AddDetection(); // Required by Wangkanai.Detection
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSerilogRequestLogging(); // Serilog
app.UseDetection(); // Required by Wangkanai.Detection
app.UseSession(); // Session
app.UseHttpsRedirection(); // For security - Adds middleware for redirecting HTTP Requests to HTTPS
app.UseHsts(); //Strict-Transport-Security header. For security
app.UseStatusCodePagesWithReExecute("/error/{0}"); // For error pages
app.UseCookiePolicy(); // For security
app.UseCors("Local"); // Add cors for local development
app.UseStaticFiles(); // For serving static files and default file names
app.UseRouting(); // For routing requests
app.UseAuthentication(); // For authentication - These must be in this order
app.UseAuthorization(); // For authorization
//app.UseEndpoints(endpoints => { endpoints.MapContent(); });
// Endpoint config from Foundation
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "Default", pattern: "{controller}/{action}/{id?}");
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapContent();
});
}
}
// This is the same as the above method, but with a different syntax.
// public static IHostBuilder CreateHostBuilder2(string[] args) =>
// Host.CreateDefaultBuilder(args)
// .ConfigureCmsDefaults()
// .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });