Files
server/util/Server/Startup.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.6 KiB
C#
Raw Permalink Normal View History

using System.Globalization;
2022-01-18 18:50:59 +01:00
using Microsoft.AspNetCore.StaticFiles;
2017-09-08 11:45:20 -04:00
namespace Bit.Server;
2022-08-29 16:06:55 -04:00
public class Startup
{
private readonly List<string> _longCachedPaths = new List<string>
2018-08-07 12:49:00 -04:00
{
"/app/", "/locales/", "/fonts/", "/connectors/", "/scripts/"
};
2018-08-07 12:49:00 -04:00
private readonly List<string> _mediumCachedPaths = new List<string>
{
2018-08-07 12:49:00 -04:00
"/images/"
};
2019-07-11 15:03:17 -04:00
public Startup()
{
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
}
public void ConfigureServices(IServiceCollection services)
2020-01-13 11:14:50 -05:00
{
services.AddRouting();
}
public void Configure(
IApplicationBuilder app,
feat: non-root self hosted images for standard deployment (#5701) * Use IHttpMessageHandlerFactory For HTTP Communication Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * feat: allow custom app-id.json location for rootless Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * fix: new build context wont allow copying git context * feat: allow images to run as non-root user * fix: build failures caused by bad merge * build: we don't need to copy the `.git` dir * Revert "build: we don't need to copy the `.git` dir" This reverts commit 32c2f6236a894534de09ffe847ffff064a7174bd. * Use `IHttpClientFactory` in more places * update build workflow * fix: compatibility with the existin run.sh script * fix: compatibility with existing run.sh script * Add SelfHosted GlobalSettings for Setup * Fix my build error * Add other services * Add IConfiguration * fix: missing gosu command for rootful mode * fix: try using .net core certificate handling * fix: add `SSL_CERT_DIR` to remaining images * Remove X509ChainCustomization activation code * Revert "Use IHttpMessageHandlerFactory For HTTP Communication" This reverts commit c93be6d52b12599040d3c3d8a7b3bc854c6c6802. * Revert "fix: build failures caused by bad merge" This reverts commit 3e4639489b6b6c06b5a977a069002fe0c0eb2057. * Revert "Use `IHttpClientFactory` in more places" This reverts commit 284501a4932b819b093406e0bcdf76def22b6eea. * remove unused code * re-add error log for installation id * remove missing error message in log * build: remove duplicate docker+qemu setup steps Co-authored-by: Opeyemi <Alaoopeyemi101@gmail.com> * build: optimize for simpler builds over caching * build: restore previous method for getting the GIT_HASH * fix: add missing build args to remaining images * fix: rm extraneous source revision id arg * fmt: apply consistent spacing and rm redundant WORKDIR directive * build: update migrator to use simpler build; apply consistent spacing * fix: merge conflicts; simplify changes * fix: add publish branch check back --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: Opeyemi <Alaoopeyemi101@gmail.com>
2025-05-30 10:29:47 -07:00
IConfiguration configuration,
ILogger<Startup> logger)
2022-08-29 16:06:55 -04:00
{
if (configuration.GetValue<bool?>("serveUnknown") ?? false)
{
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "application/octet-stream"
});
2020-01-10 08:33:13 -05:00
app.UseRouting();
app.UseEndpoints(endpoints =>
{
2020-01-10 08:33:13 -05:00
endpoints.MapGet("/alive",
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
2022-08-29 16:06:55 -04:00
});
}
else if (configuration.GetValue<bool?>("webVault") ?? false)
2022-08-29 16:06:55 -04:00
{
feat: non-root self hosted images for standard deployment (#5701) * Use IHttpMessageHandlerFactory For HTTP Communication Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * feat: allow custom app-id.json location for rootless Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * fix: new build context wont allow copying git context * feat: allow images to run as non-root user * fix: build failures caused by bad merge * build: we don't need to copy the `.git` dir * Revert "build: we don't need to copy the `.git` dir" This reverts commit 32c2f6236a894534de09ffe847ffff064a7174bd. * Use `IHttpClientFactory` in more places * update build workflow * fix: compatibility with the existin run.sh script * fix: compatibility with existing run.sh script * Add SelfHosted GlobalSettings for Setup * Fix my build error * Add other services * Add IConfiguration * fix: missing gosu command for rootful mode * fix: try using .net core certificate handling * fix: add `SSL_CERT_DIR` to remaining images * Remove X509ChainCustomization activation code * Revert "Use IHttpMessageHandlerFactory For HTTP Communication" This reverts commit c93be6d52b12599040d3c3d8a7b3bc854c6c6802. * Revert "fix: build failures caused by bad merge" This reverts commit 3e4639489b6b6c06b5a977a069002fe0c0eb2057. * Revert "Use `IHttpClientFactory` in more places" This reverts commit 284501a4932b819b093406e0bcdf76def22b6eea. * remove unused code * re-add error log for installation id * remove missing error message in log * build: remove duplicate docker+qemu setup steps Co-authored-by: Opeyemi <Alaoopeyemi101@gmail.com> * build: optimize for simpler builds over caching * build: restore previous method for getting the GIT_HASH * fix: add missing build args to remaining images * fix: rm extraneous source revision id arg * fmt: apply consistent spacing and rm redundant WORKDIR directive * build: update migrator to use simpler build; apply consistent spacing * fix: merge conflicts; simplify changes * fix: add publish branch check back --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: Opeyemi <Alaoopeyemi101@gmail.com>
2025-05-30 10:29:47 -07:00
var appIdLocation = configuration.GetValue<string>("appIdLocation");
if (!string.IsNullOrEmpty(appIdLocation))
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/app-id.json", async context =>
{
var appId = await File.ReadAllTextAsync(appIdLocation);
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(appId);
});
});
}
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 12:49:00 -04:00
var options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);
app.UseStaticFiles(new StaticFileOptions
2022-08-29 16:06:55 -04:00
{
2018-08-07 12:49:00 -04:00
ContentTypeProvider = provider,
OnPrepareResponse = ctx =>
{
if (!ctx.Context.Request.Path.HasValue ||
ctx.Context.Response.Headers.ContainsKey("Cache-Control"))
2022-08-29 16:06:55 -04:00
{
2018-08-07 12:49:00 -04:00
return;
2022-08-29 16:06:55 -04:00
}
2018-08-07 12:49:00 -04:00
var path = ctx.Context.Request.Path.Value;
if (_longCachedPaths.Any(ext => path.StartsWith(ext)))
2022-08-29 16:06:55 -04:00
{
2018-08-07 12:49:00 -04:00
// 14 days
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=1209600");
2022-08-29 16:06:55 -04:00
}
if (_mediumCachedPaths.Any(ext => path.StartsWith(ext)))
2018-08-07 12:49:00 -04:00
{
// 7 days
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=604800");
}
2022-08-29 16:06:55 -04:00
}
2018-08-07 12:49:00 -04:00
});
}
2018-08-07 15:04:11 -04:00
else
2022-08-29 16:06:55 -04:00
{
2018-08-07 15:04:11 -04:00
app.UseFileServer();
2020-01-10 08:33:13 -05:00
app.UseRouting();
app.UseEndpoints(endpoints =>
2018-08-07 15:04:11 -04:00
{
2020-01-10 08:33:13 -05:00
endpoints.MapGet("/alive",
async context => await context.Response.WriteAsync(System.DateTime.UtcNow.ToString()));
});
}
}
}