2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Globalization;
|
2022-01-18 18:50:59 +01:00
|
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2017-08-15 18:03:55 +02:00
|
|
|
|
|
2017-09-08 17:45:20 +02:00
|
|
|
|
namespace Bit.Server;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2017-08-15 18:03:55 +02:00
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
private readonly List<string> _longCachedPaths = new List<string>
|
2018-08-07 18:49:00 +02:00
|
|
|
|
{
|
|
|
|
|
"/app/", "/locales/", "/fonts/", "/connectors/", "/scripts/"
|
2022-08-29 21:53:48 +02:00
|
|
|
|
};
|
2018-08-07 18:49:00 +02:00
|
|
|
|
private readonly List<string> _mediumCachedPaths = new List<string>
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
"/images/"
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-11 21:03:17 +02:00
|
|
|
|
public Startup()
|
|
|
|
|
{
|
|
|
|
|
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:03:55 +02:00
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
2020-01-13 17:14:50 +01:00
|
|
|
|
{
|
|
|
|
|
services.AddRouting();
|
|
|
|
|
}
|
2017-08-15 18:03:55 +02:00
|
|
|
|
|
2017-08-25 16:59:27 +02:00
|
|
|
|
public void Configure(
|
|
|
|
|
IApplicationBuilder app,
|
2018-05-21 19:31:47 +02:00
|
|
|
|
IConfiguration configuration)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (configuration.GetValue<bool?>("serveUnknown") ?? false)
|
2017-08-15 18:03:55 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
2017-08-25 16:59:27 +02:00
|
|
|
|
{
|
|
|
|
|
ServeUnknownFileTypes = true,
|
|
|
|
|
DefaultContentType = "application/octet-stream"
|
|
|
|
|
});
|
2020-01-10 14:33:13 +01:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
2017-08-25 16:59:27 +02:00
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
endpoints.MapGet("/alive",
|
|
|
|
|
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
else if (configuration.GetValue<bool?>("webVault") ?? false)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-18 18:50:59 +01:00
|
|
|
|
// TODO: This should be removed when asp.net natively support avif
|
|
|
|
|
var provider = new FileExtensionContentTypeProvider { Mappings = { [".avif"] = "image/avif" } };
|
|
|
|
|
|
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
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
ContentTypeProvider = provider,
|
|
|
|
|
OnPrepareResponse = ctx =>
|
|
|
|
|
{
|
|
|
|
|
if (!ctx.Context.Request.Path.HasValue ||
|
|
|
|
|
ctx.Context.Response.Headers.ContainsKey("Cache-Control"))
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
return;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2018-08-07 18:49:00 +02:00
|
|
|
|
var path = ctx.Context.Request.Path.Value;
|
|
|
|
|
if (_longCachedPaths.Any(ext => path.StartsWith(ext)))
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2018-08-07 18:49:00 +02:00
|
|
|
|
// 14 days
|
|
|
|
|
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=1209600");
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (_mediumCachedPaths.Any(ext => path.StartsWith(ext)))
|
2018-08-07 18:49:00 +02:00
|
|
|
|
{
|
|
|
|
|
// 7 days
|
|
|
|
|
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=604800");
|
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2018-08-07 18:49:00 +02:00
|
|
|
|
});
|
2017-08-25 16:59:27 +02:00
|
|
|
|
}
|
2018-08-07 21:04:11 +02:00
|
|
|
|
else
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2018-08-07 21:04:11 +02:00
|
|
|
|
app.UseFileServer();
|
2020-01-10 14:33:13 +01:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
2018-08-07 21:04:11 +02:00
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
endpoints.MapGet("/alive",
|
|
|
|
|
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
|
|
|
|
|
});
|
2017-08-15 18:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|