2018-08-07 18:49:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-08-25 16:59:27 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2017-08-15 18:03:55 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-08-25 16:59:27 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-08-15 18:03:55 +02:00
|
|
|
|
|
2017-09-08 17:45:20 +02:00
|
|
|
|
namespace Bit.Server
|
2017-08-15 18:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
private readonly List<string> _longCachedPaths = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"/app/", "/locales/", "/fonts/", "/connectors/", "/scripts/"
|
|
|
|
|
};
|
|
|
|
|
private readonly List<string> _mediumCachedPaths = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"/images/"
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-15 18:03:55 +02:00
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{ }
|
|
|
|
|
|
2017-08-25 16:59:27 +02:00
|
|
|
|
public void Configure(
|
|
|
|
|
IApplicationBuilder app,
|
|
|
|
|
ILoggerFactory loggerFactory,
|
2018-05-21 19:31:47 +02:00
|
|
|
|
IConfiguration configuration)
|
2017-08-15 18:03:55 +02:00
|
|
|
|
{
|
2017-08-25 16:59:27 +02:00
|
|
|
|
loggerFactory
|
|
|
|
|
.AddConsole()
|
|
|
|
|
.AddDebug();
|
|
|
|
|
|
2018-08-07 21:04:11 +02:00
|
|
|
|
if(configuration.GetValue<bool?>("serveUnknown") ?? false)
|
2017-08-25 16:59:27 +02:00
|
|
|
|
{
|
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
ServeUnknownFileTypes = true,
|
|
|
|
|
DefaultContentType = "application/octet-stream"
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-07 21:04:11 +02:00
|
|
|
|
else if(configuration.GetValue<bool?>("webVault") ?? false)
|
2017-08-25 16:59:27 +02:00
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
var options = new DefaultFilesOptions();
|
|
|
|
|
options.DefaultFileNames.Clear();
|
|
|
|
|
options.DefaultFileNames.Add("index.html");
|
|
|
|
|
app.UseDefaultFiles(options);
|
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
OnPrepareResponse = ctx =>
|
|
|
|
|
{
|
2018-08-07 21:04:11 +02:00
|
|
|
|
if(!ctx.Context.Request.Path.HasValue ||
|
|
|
|
|
ctx.Context.Response.Headers.ContainsKey("Cache-Control"))
|
2018-08-07 18:49:00 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var path = ctx.Context.Request.Path.Value;
|
|
|
|
|
if(_longCachedPaths.Any(ext => path.StartsWith(ext)))
|
|
|
|
|
{
|
|
|
|
|
// 14 days
|
|
|
|
|
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=1209600");
|
|
|
|
|
}
|
|
|
|
|
if(_mediumCachedPaths.Any(ext => path.StartsWith(ext)))
|
|
|
|
|
{
|
|
|
|
|
// 7 days
|
|
|
|
|
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=604800");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-25 16:59:27 +02:00
|
|
|
|
}
|
2018-08-07 21:04:11 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseFileServer();
|
|
|
|
|
}
|
2017-08-15 18:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|