From b5437f4010f9cda2e4daf4222c30942115b77c30 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Jun 2018 13:18:31 +0200 Subject: [PATCH] Options for surrogate keys. --- src/Squidex/AppServices.cs | 3 +++ .../Contents/ContentsController.cs | 25 ++++++++++++++++--- .../Contents/ContentsControllerOptions.cs | 16 ++++++++++++ src/Squidex/WebStartup.cs | 2 ++ src/Squidex/appsettings.json | 14 +++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/Squidex/Areas/Api/Controllers/Contents/ContentsControllerOptions.cs diff --git a/src/Squidex/AppServices.cs b/src/Squidex/AppServices.cs index ff21cc224..300255c57 100644 --- a/src/Squidex/AppServices.cs +++ b/src/Squidex/AppServices.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Squidex.Areas.Api.Config.Swagger; +using Squidex.Areas.Api.Controllers.Contents; using Squidex.Areas.IdentityServer.Config; using Squidex.Config; using Squidex.Config.Authentication; @@ -43,6 +44,8 @@ namespace Squidex services.Configure( config.GetSection("mode")); + services.Configure( + config.GetSection("contentsController")); services.Configure( config.GetSection("urls")); services.Configure( diff --git a/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs b/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs index 657bcc45b..d99b7de22 100644 --- a/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs +++ b/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using NodaTime; using NodaTime.Text; using NSwag.Annotations; @@ -29,15 +30,18 @@ namespace Squidex.Areas.Api.Controllers.Contents [SwaggerIgnore] public sealed class ContentsController : ApiController { + private readonly IOptions controllerOptions; private readonly IContentQueryService contentQuery; private readonly IGraphQLService graphQl; public ContentsController(ICommandBus commandBus, IContentQueryService contentQuery, - IGraphQLService graphQl) + IGraphQLService graphQl, + IOptions controllerOptions) : base(commandBus) { this.contentQuery = contentQuery; + this.controllerOptions = controllerOptions; this.graphQl = graphQl; } @@ -121,7 +125,12 @@ namespace Squidex.Areas.Api.Controllers.Contents Items = result.Take(200).Select(x => ContentDto.FromContent(x, context)).ToArray() }; - Response.Headers["Surrogate-Key"] = string.Join(" ", response.Items.Select(x => x.Id)); + var options = controllerOptions.Value; + + if (options.EnableSurrogateKeys && response.Items.Length <= options.MaxItemsForSurrogateKeys) + { + Response.Headers["Surrogate-Key"] = string.Join(" ", response.Items.Select(x => x.Id)); + } return Ok(response); } @@ -151,7 +160,11 @@ namespace Squidex.Areas.Api.Controllers.Contents var response = ContentDto.FromContent(content, context); Response.Headers["ETag"] = content.Version.ToString(); - Response.Headers["Surrogate-Key"] = content.Id.ToString(); + + if (controllerOptions.Value.EnableSurrogateKeys) + { + Response.Headers["Surrogate-Key"] = content.Id.ToString(); + } return Ok(response); } @@ -183,7 +196,11 @@ namespace Squidex.Areas.Api.Controllers.Contents var response = ContentDto.FromContent(content, context); Response.Headers["ETag"] = content.Version.ToString(); - Response.Headers["Surrogate-Key"] = content.Id.ToString(); + + if (controllerOptions.Value.EnableSurrogateKeys) + { + Response.Headers["Surrogate-Key"] = content.Id.ToString(); + } return Ok(response.Data); } diff --git a/src/Squidex/Areas/Api/Controllers/Contents/ContentsControllerOptions.cs b/src/Squidex/Areas/Api/Controllers/Contents/ContentsControllerOptions.cs new file mode 100644 index 000000000..95d210a18 --- /dev/null +++ b/src/Squidex/Areas/Api/Controllers/Contents/ContentsControllerOptions.cs @@ -0,0 +1,16 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +namespace Squidex.Areas.Api.Controllers.Contents +{ + public sealed class ContentsControllerOptions + { + public bool EnableSurrogateKeys { get; set; } + + public int MaxItemsForSurrogateKeys { get; set; } + } +} diff --git a/src/Squidex/WebStartup.cs b/src/Squidex/WebStartup.cs index 3e6baa3e3..dc51a11e6 100644 --- a/src/Squidex/WebStartup.cs +++ b/src/Squidex/WebStartup.cs @@ -46,6 +46,8 @@ namespace Squidex app.ApplicationServices.RunMigrate(); app.ApplicationServices.RunRunnables(); + app.UseDeveloperExceptionPage(); + app.UseMyLocalCache(); app.UseMyCors(); app.UseMyForwardingRules(); diff --git a/src/Squidex/appsettings.json b/src/Squidex/appsettings.json index b23f0e36c..b82f2ca23 100644 --- a/src/Squidex/appsettings.json +++ b/src/Squidex/appsettings.json @@ -49,6 +49,20 @@ } }, + "contentsController": { + /* + * Enable surrogate keys as headers. + * + * Nginx Has problems with long headers. It might make sense to disable this feature if you do not use a CDN. + */ + "enableSurrogateKeys": true, + + /* + * Restrict the surrogate keys to results that have less than 200 items. + */ + "maxItemsForSurrogateKeys": 200 + }, + "logging": { /* * Setting the flag to true, enables well formatteds json logs.