From c4be786d05135529c51039c3045bf3ae3aeb5249 Mon Sep 17 00:00:00 2001 From: WhiteTom Date: Sun, 23 Jul 2023 15:46:38 +0200 Subject: [PATCH 1/2] Alow redirection of root --- .../Domain/ShortenerSettings.cs | 1 + .../Functions/UrlRedirectRoot.cs | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/Cloud5mins.ShortenerTools.Functions/Functions/UrlRedirectRoot.cs diff --git a/src/Cloud5mins.ShortenerTools.Core/Domain/ShortenerSettings.cs b/src/Cloud5mins.ShortenerTools.Core/Domain/ShortenerSettings.cs index b702f8d4..967fbd39 100644 --- a/src/Cloud5mins.ShortenerTools.Core/Domain/ShortenerSettings.cs +++ b/src/Cloud5mins.ShortenerTools.Core/Domain/ShortenerSettings.cs @@ -3,6 +3,7 @@ namespace Cloud5mins.ShortenerTools.Core.Domain public class ShortenerSettings { public string DefaultRedirectUrl { get; set; } + public string RootRedirectUrl { get; set; } public string CustomDomain { get; set; } public string DataStorage { get; set; } } diff --git a/src/Cloud5mins.ShortenerTools.Functions/Functions/UrlRedirectRoot.cs b/src/Cloud5mins.ShortenerTools.Functions/Functions/UrlRedirectRoot.cs new file mode 100644 index 00000000..a2d7a6d4 --- /dev/null +++ b/src/Cloud5mins.ShortenerTools.Functions/Functions/UrlRedirectRoot.cs @@ -0,0 +1,37 @@ +using Cloud5mins.ShortenerTools.Core.Domain; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Http; +using Microsoft.Extensions.Logging; +using System.Linq; +using System.Net; +using System.Security.Claims; +using System.Threading; +using System.Threading.Tasks; + +namespace Cloud5mins.ShortenerTools.Functions +{ + public class UrlRedirectRoot + { + private readonly ILogger _logger; + private readonly ShortenerSettings _settings; + + public UrlRedirectRoot(ILoggerFactory loggerFactory, ShortenerSettings settings) + { + _logger = loggerFactory.CreateLogger(); + _settings = settings; + } + + [Function("UrlRedirectRoot")] + public async Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "/")] + HttpRequestData req, + ExecutionContext context) + { + string redirectUrl = _settings.RootRedirectUrl ?? _settings.DefaultRedirectUrl ?? "https://azure.com"; + var res = req.CreateResponse(HttpStatusCode.Redirect); + res.Headers.Add("Location", redirectUrl); + return res; + + } + } +} From 0967baf72cc0b56e2b414941df24214811bb9cd9 Mon Sep 17 00:00:00 2001 From: WhiteTom Date: Sun, 23 Jul 2023 16:11:23 +0200 Subject: [PATCH 2/2] add root redirect to deployment --- src/deployment/azureDeploy.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/deployment/azureDeploy.json b/src/deployment/azureDeploy.json index d14321e7..aa90ec1c 100644 --- a/src/deployment/azureDeploy.json +++ b/src/deployment/azureDeploy.json @@ -23,6 +23,13 @@ "description": "Default URL used when key passed by the user is not found." } }, + "rootRedirectUrl": { + "defaultValue": "https://azure.com", + "type": "string", + "metadata": { + "description": "URL used someone access the root (/)." + } + }, "GitHubURL": { "type": "string", "defaultValue": "https://github.com/microsoft/AzUrlShortener.git", @@ -117,6 +124,14 @@ { "name": "defaultRedirectUrl", "value": "[parameters('defaultRedirectUrl')]" + }, + { + "name": "rootRedirectUrl", + "value": "[parameters('rootRedirectUrl')]" + }, + { + "name": "AzureWebJobsDisableHomepage", + "value": "true" } ] },