summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-09-22 20:19:12 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-09-22 20:19:12 -0400
commit2406d4b0a6fd8b2d439438687b0101a6f8934ca5 (patch)
tree0a7e374d1ef8badcc89979d91c19a565325a4871
parentf8566067e0874428c53ffdb6cd12d3f91ad4c51c (diff)
downloadSMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.tar.gz
SMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.tar.bz2
SMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.zip
add subdomain rewrite rule (#336)
-rw-r--r--src/StardewModdingAPI.Web/Controllers/ModsController.cs2
-rw-r--r--src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs30
-rw-r--r--src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj1
-rw-r--r--src/StardewModdingAPI.Web/Startup.cs6
4 files changed, 37 insertions, 2 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs
index 876f5248..7f7afbb9 100644
--- a/src/StardewModdingAPI.Web/Controllers/ModsController.cs
+++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs
@@ -9,7 +9,7 @@ using StardewModdingAPI.Web.Models;
namespace StardewModdingAPI.Web.Controllers
{
/// <summary>Provides an API to perform mod update checks.</summary>
- [Route("v1.0/mods")]
+ [Route("api/v1.0/mods")]
[Produces("application/json")]
public class ModsController : Controller
{
diff --git a/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs
new file mode 100644
index 00000000..9b89cb65
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs
@@ -0,0 +1,30 @@
+using System;
+using Microsoft.AspNetCore.Rewrite;
+
+namespace StardewModdingAPI.Web.Framework
+{
+ /// <summary>Rewrite requests to prepend the subdomain portion (if any) to the path.</summary>
+ /// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" />.</remarks>
+ public class RewriteSubdomainRule : IRule
+ {
+ /// <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)
+ {
+ context.Result = RuleResult.ContinueRules;
+
+ // get host parts
+ string host = context.HttpContext.Request.Host.Host;
+ string[] parts = host.Split('.');
+
+ // validate
+ if (parts.Length < 2)
+ return;
+ if (parts.Length < 3 && !"localhost".Equals(parts[1], StringComparison.InvariantCultureIgnoreCase))
+ return;
+
+ // prepend to path
+ context.HttpContext.Request.Path = $"/{parts[0]}{context.HttpContext.Request.Path}";
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj
index 7dff3fb6..2af7c3df 100644
--- a/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj
+++ b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj
@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
+ <PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Pathoschild.Http.FluentClient" Version="3.1.0" />
diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs
index b35d072b..19dffb88 100644
--- a/src/StardewModdingAPI.Web/Startup.cs
+++ b/src/StardewModdingAPI.Web/Startup.cs
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
+using StardewModdingAPI.Web.Framework;
namespace StardewModdingAPI.Web
{
@@ -53,7 +55,9 @@ namespace StardewModdingAPI.Web
{
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
- app.UseMvc();
+ app
+ .UseRewriter(new RewriteOptions().Add(new RewriteSubdomainRule())) // convert subdomain.smapi.io => smapi.io/subdomain for routing
+ .UseMvc();
}
}
}