Permalink
Switch branches/tags
Nothing to show
Find file Copy path
146 lines (125 sloc) 5.11 KB
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Miniblog.Core.Services;
using WebEssentials.AspNetCore.OutputCaching;
using WebMarkupMin.AspNetCore2;
using WebMarkupMin.Core;
using WilderMinds.MetaWeblog;
using IWmmLogger = WebMarkupMin.Core.Loggers.ILogger;
using MetaWeblogService = Miniblog.Core.Services.MetaWeblogService;
using WmmNullLogger = WebMarkupMin.Core.Loggers.NullLogger;
namespace Miniblog.Core
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(a => a.AddServerHeader = false);
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.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IUserServices, BlogUserServices>();
services.AddSingleton<IBlogService, FileBlogService>();
services.Configure<BlogSettings>(Configuration.GetSection("blog"));
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMetaWeblog<MetaWeblogService>();
// Progressive Web Apps https://github.com/madskristensen/WebEssentials.AspNetCore.ServiceWorker
services.AddProgressiveWebApp(new WebEssentials.AspNetCore.Pwa.PwaOptions
{
OfflineRoute = "/shared/offline/"
});
// Output caching (https://github.com/madskristensen/WebEssentials.AspNetCore.OutputCaching)
services.AddOutputCaching(options =>
{
options.Profiles["default"] = new OutputCacheProfile
{
Duration = 3600
};
});
// Cookie authentication.
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/login/";
options.LogoutPath = "/logout/";
});
// HTML minification (https://github.com/Taritsyn/WebMarkupMin)
services
.AddWebMarkupMin(options =>
{
options.AllowMinificationInDevelopmentEnvironment = true;
options.DisablePoweredByHttpHeaders = true;
})
.AddHtmlMinification(options =>
{
options.MinificationSettings.RemoveOptionalEndTags = false;
options.MinificationSettings.WhitespaceMinificationMode = WhitespaceMinificationMode.Safe;
});
services.AddSingleton<IWmmLogger, WmmNullLogger>(); // Used by HTML minifier
// Bundling, minification and Sass transpilation (https://github.com/ligershark/WebOptimizer)
services.AddWebOptimizer(pipeline =>
{
pipeline.MinifyJsFiles();
pipeline.CompileScssFiles()
.InlineImages(1);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Shared/Error");
app.UseHsts();
}
app.Use((context, next) =>
{
context.Response.Headers["X-Content-Type-Options"] = "nosniff";
return next();
});
app.UseStatusCodePagesWithReExecute("/Shared/Error");
app.UseWebOptimizer();
app.UseStaticFilesWithCache();
if (Configuration.GetValue<bool>("forcessl"))
{
app.UseHttpsRedirection();
}
app.UseMetaWeblog("/metaweblog");
app.UseAuthentication();
app.UseOutputCaching();
app.UseWebMarkupMin();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Blog}/{action=Index}/{id?}");
});
}
}
}