1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00
bitwarden-server/util/Server/Startup.cs

90 lines
3.0 KiB
C#
Raw Normal View History

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