summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-10-29 22:18:08 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-10-29 22:18:08 -0400
commit13baaf8920f4a80ac3c0cd41a16b9afb1b993048 (patch)
tree3f8e976fff0d5092b03a29862bae8f8814e7d9df /src/SMAPI.Web
parentc3cd9a3120cd74cb1dfc4455d3ecca203b163013 (diff)
downloadSMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.tar.gz
SMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.tar.bz2
SMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.zip
add smapi.io shortcut URLs (#375)
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r--src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs61
-rw-r--r--src/SMAPI.Web/Startup.cs4
2 files changed, 65 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs
new file mode 100644
index 00000000..0719e311
--- /dev/null
+++ b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Net;
+using System.Text.RegularExpressions;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Rewrite;
+
+namespace StardewModdingAPI.Web.Framework.RewriteRules
+{
+ /// <summary>Redirect requests to an external URL if they match a condition.</summary>
+ internal class RedirectToUrlRule : IRule
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>A predicate which indicates when the rule should be applied.</summary>
+ private readonly Func<HttpRequest, bool> ShouldRewrite;
+
+ /// <summary>The new URL to which to redirect.</summary>
+ private readonly string NewUrl;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param>
+ /// <param name="url">The new URL to which to redirect.</param>
+ public RedirectToUrlRule(Func<HttpRequest, bool> shouldRewrite, string url)
+ {
+ this.ShouldRewrite = shouldRewrite ?? (req => true);
+ this.NewUrl = url;
+ }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="pathRegex">A case-insensitive regex to match against the path.</param>
+ /// <param name="url">The external URL.</param>
+ public RedirectToUrlRule(string pathRegex, string url)
+ {
+ Regex regex = new Regex(pathRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
+ this.ShouldRewrite = req => req.Path.HasValue && regex.IsMatch(req.Path.Value);
+ this.NewUrl = url;
+ }
+
+ /// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
+ /// <param name="context">The rewrite context.</param>
+ public void ApplyRule(RewriteContext context)
+ {
+ HttpRequest request = context.HttpContext.Request;
+
+ // check condition
+ if (!this.ShouldRewrite(request))
+ return;
+
+ // redirect request
+ HttpResponse response = context.HttpContext.Response;
+ response.StatusCode = (int)HttpStatusCode.Redirect;
+ response.Headers["Location"] = this.NewUrl;
+ context.Result = RuleResult.EndResponse;
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 860354f1..bc491128 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -79,6 +79,10 @@ namespace StardewModdingAPI.Web
&& (req.Host.Host.StartsWith("api.") || req.Host.Host.StartsWith("log."))
&& !req.Path.StartsWithSegments("/content")
))
+
+ // shortcut redirects
+ .Add(new RedirectToUrlRule("^/docs$", "https://stardewvalleywiki.com/Modding:Index"))
+ .Add(new RedirectToUrlRule("^/install$", "https://stardewvalleywiki.com/Modding:Installing_SMAPI"))
)
.UseStaticFiles() // wwwroot folder
.UseMvc();