89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Website.Data;
|
|
|
|
namespace Website
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
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)
|
|
{
|
|
services.Configure<CookiePolicyOptions>(options =>
|
|
{
|
|
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
|
|
options.CheckConsentNeeded = context => true;
|
|
options.MinimumSameSitePolicy = SameSiteMode.None;
|
|
});
|
|
|
|
services.AddSingleton(Configuration);
|
|
RegisterRepositories(services);
|
|
|
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
|
|
|
|
services.AddMvc(options => options.EnableEndpointRouting = false)
|
|
#if DEBUG
|
|
.AddRazorRuntimeCompilation()
|
|
#endif
|
|
;
|
|
}
|
|
|
|
private void RegisterRepositories(IServiceCollection services) =>
|
|
services.AddSingleton<IDatabaseProvider, MySQLDatabaseProvider>()
|
|
.AddSingleton<IBlogApi, BlogApi>()
|
|
.AddSingleton<UserRepository, UserRepository>()
|
|
.AddSingleton(new GitServerApi(new HttpClient(), Configuration["gitDomain"], Configuration["gitToken"]))
|
|
.AddHttpClient<IBlogApi, BlogApi>(client => client.BaseAddress = new Uri(Configuration["blogApiEndpoint"]))
|
|
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {
|
|
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
|
|
});
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Error/ServerError");
|
|
// 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.UseStatusCodePagesWithReExecute("/Error/PageNotFound")
|
|
.UseHttpsRedirection()
|
|
.UseStaticFiles()
|
|
.UseAuthentication()
|
|
.UseMvc(routes => {
|
|
routes.MapRoute(
|
|
name: "blogPages",
|
|
template: "blog/{action}/{page:int}",
|
|
defaults: new {controller = "Blog", action = "Page", page = 1});
|
|
routes.MapRoute(
|
|
name: "blogView",
|
|
template: "blog/view/{url}",
|
|
defaults: new {controller = "Blog", action = "Entry"});
|
|
routes.MapRoute(
|
|
name: "default",
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
});
|
|
}
|
|
}
|
|
}
|