1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-28 17:57:37 +01:00
bitwarden-server/util/Server/Startup.cs

92 lines
3.2 KiB
C#
Raw Normal View History

2018-08-07 18:49:00 +02:00
using System.Collections.Generic;
2019-07-11 21:03:17 +02:00
using System.Globalization;
2018-08-07 18:49:00 +02:00
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2017-09-08 17:45:20 +02:00
namespace Bit.Server
{
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/"
};
2019-07-11 21:03:17 +02:00
public Startup()
{
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
}
public void ConfigureServices(IServiceCollection services)
2020-01-13 17:14:50 +01:00
{
services.AddRouting();
}
public void Configure(
IApplicationBuilder app,
IConfiguration configuration)
{
if (configuration.GetValue<bool?>("serveUnknown") ?? false)
{
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "application/octet-stream"
});
2020-01-10 14:33:13 +01:00
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/alive",
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
});
}
else if (configuration.GetValue<bool?>("webVault") ?? false)
{
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 =>
{
if (!ctx.Context.Request.Path.HasValue ||
2018-08-07 21:04:11 +02:00
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)))
2018-08-07 18:49:00 +02:00
{
// 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
{
// 7 days
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=604800");
}
}
});
}
2018-08-07 21:04:11 +02:00
else
{
app.UseFileServer();
2020-01-10 14:33:13 +01:00
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/alive",
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
});
2018-08-07 21:04:11 +02:00
}
}
}
}