Skip to content

Commit

Permalink
feature: added support for aliases with api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpop-zengenti committed Apr 30, 2024
1 parent 99c8ccc commit deca5b6
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public RouteInfo Create(
var queryString = BuildQueryString(originUri);

// Handle API requests
bool isApiRequest = Constants.Paths.ApiPrefixes.Any(prefix => path.StartsWithCaseInsensitive(prefix)) &&
!path.StartsWithCaseInsensitive("/api/publishing/request-handler") &&
!path.StartsWithCaseInsensitive("/api/preview-toolbar/blocks");
if (isApiRequest)
bool isContensisApiRequest =
Constants.Paths.ApiPrefixes.Any(prefix => path.StartsWithCaseInsensitive(prefix)) &&
!path.StartsWithCaseInsensitive("/api/publishing/request-handler") &&
!path.StartsWithCaseInsensitive("/api/preview-toolbar/blocks") &&
_blockClusterConfig.AliasesWithApiRoutes?.ContainsCaseInsensitive(_requestContext.Alias) != true;
if (isContensisApiRequest)
{
var apiHost = $"api-{_requestContext.Alias}.cloud.contensis.com";
var apiUrl = $"https://{apiHost}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Zengenti.Contensis.RequestHandler.Application.Services;

public class RouteService : IRouteService
{
private readonly BlockClusterConfig _blockClusterConfig;
private readonly INodeService _nodeService;
private readonly IPublishingService _publishingService;
private readonly IRouteInfoFactory _routeInfoFactory;
Expand All @@ -19,13 +20,15 @@ public class RouteService : IRouteService
private readonly ILogger _logger;

public RouteService(
BlockClusterConfig blockClusterConfig,
INodeService nodeService,
IPublishingService publishingService,
IRouteInfoFactory routeInfoFactory,
IRequestContext requestContext,
ICacheKeyService cacheKeyService,
ILogger<RouteService> logger)
{
_blockClusterConfig = blockClusterConfig;
_nodeService = nodeService;
_publishingService = publishingService;
_routeInfoFactory = routeInfoFactory;
Expand Down Expand Up @@ -177,19 +180,31 @@ public async Task<RouteInfo> GetRouteForRequest(Uri originUri, Headers headers)
return returnInfo;
}

private static bool ShouldPerformNodeLookup(string path)
private bool ShouldPerformNodeLookup(string path)
{
// Don't like this hard-coded path, maybe move to config?
// We can negate anything that is a rewritten static path
var pathIsRewritten = StaticPath.Parse(path)?.IsRewritten;

bool doLookup = path.ToLowerInvariant() != "/favicon.ico" &&
!path.StartsWithCaseInsensitive("/contensis-preview-toolbar/") &&
!pathIsRewritten.GetValueOrDefault() &&
!Constants.Paths.ApiPrefixes.Any(path.StartsWithCaseInsensitive) &&
!Constants.Paths.PassThroughPrefixes.Any(path.StartsWithCaseInsensitive);
var pathIsRewritten = StaticPath.Parse(path)?.IsRewritten ?? false;
if (pathIsRewritten)
{
return false;
}

if (path.ToLowerInvariant() == "/favicon.ico" ||
path.StartsWithCaseInsensitive("/contensis-preview-toolbar/") ||
Constants.Paths.PassThroughPrefixes.Any(path.StartsWithCaseInsensitive))
{
return false;
}

if (Constants.Paths.ApiPrefixes.Any(path.StartsWithCaseInsensitive) &&
_blockClusterConfig.AliasesWithApiRoutes?.ContainsCaseInsensitive(_requestContext.Alias) != true)
{
return false;
}

return doLookup;
return true;
}

private void CheckAndSetProjectHeaders(Headers headers)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
namespace Zengenti.Contensis.RequestHandler.Domain.ValueTypes;

public record BlockClusterConfig(string? BlockClusterIngressIp = null, string? BlockAddressSuffix = null);
public record BlockClusterConfig(
string? BlockClusterIngressIp = null,
string? BlockAddressSuffix = null,
string[]? AliasesWithApiRoutes = null);
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public void GivenARequestPathDoesNotExistsAsANode()
var requestContext = Substitute.For<IRequestContext>();
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var routeInfoFactory = new RouteInfoFactory(requestContext, new BlockClusterConfig());
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory = new RouteInfoFactory(requestContext, blockClusterConfig);
var rendererService = SpecHelper.CreatePublishingService(routeInfoFactory);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
rendererService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public void GivenARequestPathExistsAsANode()
var requestContext = Substitute.For<IRequestContext>();
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory =
new RouteInfoFactory(requestContext, new BlockClusterConfig());
new RouteInfoFactory(requestContext, blockClusterConfig);
var publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void GivenARequestPathExistsAsANode()
requestContext.ProjectUuid.Returns(_projectUuid);
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var routeInfoFactory = new RouteInfoFactory(requestContext, new BlockClusterConfig());
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory = new RouteInfoFactory(requestContext, blockClusterConfig);
_publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);

_node = new Node
Expand All @@ -42,6 +43,7 @@ public void GivenARequestPathExistsAsANode()
_nodeService.GetByPath(Path).Returns(_node);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
_publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void GivenARequestPathExistsAsANode()
requestContext.ProjectUuid.Returns(_projectUuid);
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var routeInfoFactory = new RouteInfoFactory(requestContext, new BlockClusterConfig());
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory = new RouteInfoFactory(requestContext, blockClusterConfig);
_publishingService = SpecHelper.CreatePublishingService(routeInfoFactory, enableFullUriRouting: true);

_node = new Node
Expand All @@ -42,6 +43,7 @@ public void GivenARequestPathExistsAsANode()
_nodeService.GetByPath(Path).Returns(_node);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
_publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Extensions.Logging;
using NSubstitute;
using TestStack.BDDfy;
using Zengenti.Contensis.RequestHandler.Application.Services;
using Zengenti.Contensis.RequestHandler.Domain.Interfaces;
using Zengenti.Contensis.RequestHandler.Domain.ValueTypes;

namespace Zengenti.Contensis.RequestHandler.LocalDevelopment.Unit.Specs.Services.RouteServiceSpecs;

public class PathIsAliasWithApiRouteCall
{
private const string Host = "http://www.mysite.com";
private const string Path = "/api/delivery/website/";
private readonly Uri _originUri = new(Host + Path);
private readonly Headers _headers = new();

private RouteService _sut;
private INodeService _nodeService;
private RouteInfo _result;

public void GivenARequestPathIsAnApiCall()
{
_nodeService = Substitute.For<INodeService>();

var logger = Substitute.For<ILogger<RouteService>>();
var requestContext = SpecHelper.CreateRequestContext();
var cacheKeyService = Substitute.For<ICacheKeyService>();
var blockClusterConfig = new BlockClusterConfig(
AliasesWithApiRoutes: new[]
{
"test"
});
var routeInfoFactory = new RouteInfoFactory(
requestContext,
blockClusterConfig);
var publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
publishingService,
routeInfoFactory,
requestContext,
cacheKeyService,
logger);
}

public async Task WhenTheRouteIsRequested()
{
_result = await _sut.GetRouteForRequest(_originUri, _headers);
}

public void ThenNodeLookupIsPerformed()
{
_nodeService.ReceivedWithAnyArgs().GetByPath(Path);
}

[Test]
public void Run()
{
this.BDDfy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public void GivenARequestPathIsAnApiCall()
var logger = Substitute.For<ILogger<RouteService>>();
var requestContext = SpecHelper.CreateRequestContext();
var cacheKeyService = Substitute.For<ICacheKeyService>();
var routeInfoFactory = new RouteInfoFactory(requestContext, new BlockClusterConfig());
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory = new RouteInfoFactory(requestContext, blockClusterConfig);
var publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public void GivenARequestPathIsARewrittenStaticResourcePath()
requestContext.ProjectUuid.Returns(_projectUuid);
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory =
new RouteInfoFactory(requestContext, new BlockClusterConfig());
new RouteInfoFactory(requestContext, blockClusterConfig);
var publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);
var block = publishingService.GetBlockById("blogs");

_requestPath =
$"/{Constants.Paths.StaticPathUniquePrefix}{RouteInfo.GetUrlFriendlyHash(_projectUuid)}{Constants.Paths.StaticPathUniquePrefix}{block.Uuid}/static/images/header.png";

_sut = new RouteService(
blockClusterConfig,
_nodeService,
publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public void GivenARequestPathIsARewrittenStaticResourcePathWithAQueryString()
requestContext.ProjectUuid.Returns(_projectUuid);
var cacheKeyService = Substitute.For<ICacheKeyService>();
var logger = Substitute.For<ILogger<RouteService>>();
var routeInfoFactory = new RouteInfoFactory(requestContext, new BlockClusterConfig());
var blockClusterConfig = new BlockClusterConfig();
var routeInfoFactory = new RouteInfoFactory(requestContext, blockClusterConfig);
var publishingService = SpecHelper.CreatePublishingService(routeInfoFactory);
var block = publishingService.GetBlockById("blogs");

Expand All @@ -35,6 +36,7 @@ public void GivenARequestPathIsARewrittenStaticResourcePathWithAQueryString()
_originUri = new Uri("http://www.origin.com" + _requestPath);

_sut = new RouteService(
blockClusterConfig,
_nodeService,
publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void GivenARequestPathIsAnExcludedPath()
var routeInfoFactory = Substitute.For<IRouteInfoFactory>();

_sut = new RouteService(
new BlockClusterConfig(),
_nodeService,
publishingService,
routeInfoFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void GivenARequestPathIsAnExcludedPath()
var routeInfoFactory = Substitute.For<IRouteInfoFactory>();

_sut = new RouteService(
new BlockClusterConfig(),
_nodeService,
publishingService,
routeInfoFactory,
Expand Down

0 comments on commit deca5b6

Please sign in to comment.