diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-25 17:39:51 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-25 17:39:51 -0400 |
commit | 6dff9779a349945d502dee67d5d4dd8e63b4f753 (patch) | |
tree | 7621f85a392329cb0a782355feff252b6d34e2d6 /src/StardewModdingAPI.Web | |
parent | b105c97dda01441d503d31e8b8ac0b3fd35fef14 (diff) | |
download | SMAPI-6dff9779a349945d502dee67d5d4dd8e63b4f753.tar.gz SMAPI-6dff9779a349945d502dee67d5d4dd8e63b4f753.tar.bz2 SMAPI-6dff9779a349945d502dee67d5d4dd8e63b4f753.zip |
use POST for SMAPI update checks to avoid issues with long queries (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web')
-rw-r--r-- | src/StardewModdingAPI.Web/Controllers/ModsController.cs | 17 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/VersionConstraint.cs | 15 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Startup.cs | 19 |
3 files changed, 33 insertions, 18 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs index 566577e4..f29de45a 100644 --- a/src/StardewModdingAPI.Web/Controllers/ModsController.cs +++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs @@ -14,6 +14,7 @@ namespace StardewModdingAPI.Web.Controllers { /// <summary>Provides an API to perform mod update checks.</summary> [Produces("application/json")] + [Route("api/{version:semanticVersion}/[controller]")] internal class ModsController : Controller { /********* @@ -80,15 +81,27 @@ namespace StardewModdingAPI.Web.Controllers [HttpGet] public async Task<IDictionary<string, ModInfoModel>> GetAsync(string modKeys) { + string[] modKeysArray = modKeys?.Split(',').Select(p => p.Trim()).ToArray(); + if (modKeysArray == null || !modKeysArray.Any()) + return new Dictionary<string, ModInfoModel>(); + + return await this.PostAsync(new ModSearchModel(modKeysArray)); + } + + /// <summary>Fetch version metadata for the given mods.</summary> + /// <param name="search">The mod search criteria.</param> + [HttpPost] + public async Task<IDictionary<string, ModInfoModel>> PostAsync([FromBody] ModSearchModel search) + { // sort & filter keys - string[] modKeysArray = (modKeys?.Split(',').Select(p => p.Trim()).ToArray() ?? new string[0]) + string[] modKeys = (search?.ModKeys?.ToArray() ?? new string[0]) .Distinct(StringComparer.CurrentCultureIgnoreCase) .OrderBy(p => p, StringComparer.CurrentCultureIgnoreCase) .ToArray(); // fetch mod info IDictionary<string, ModInfoModel> result = new Dictionary<string, ModInfoModel>(StringComparer.CurrentCultureIgnoreCase); - foreach (string modKey in modKeysArray) + foreach (string modKey in modKeys) { // parse mod key if (!this.TryParseModKey(modKey, out string vendorKey, out string modID)) diff --git a/src/StardewModdingAPI.Web/Framework/VersionConstraint.cs b/src/StardewModdingAPI.Web/Framework/VersionConstraint.cs new file mode 100644 index 00000000..be9c0918 --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/VersionConstraint.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Routing.Constraints; + +namespace StardewModdingAPI.Web.Framework +{ + /// <summary>Constrains a route value to a valid semantic version.</summary> + internal class VersionConstraint : RegexRouteConstraint + { + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public VersionConstraint() + : base(@"^v(?>(?<major>0|[1-9]\d*))\.(?>(?<minor>0|[1-9]\d*))(?>(?:\.(?<patch>0|[1-9]\d*))?)(?:-(?<prerelease>(?>[a-z0-9]+[\-\.]?)+))?$") { } + } +} diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs index d5b828b7..eaf14983 100644 --- a/src/StardewModdingAPI.Web/Startup.cs +++ b/src/StardewModdingAPI.Web/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Rewrite; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -42,6 +43,7 @@ namespace StardewModdingAPI.Web { services .Configure<ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck")) + .Configure<RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint))) .AddMemoryCache() .AddMvc() .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider())) @@ -62,22 +64,7 @@ namespace StardewModdingAPI.Web loggerFactory.AddDebug(); app .UseRewriter(new RewriteOptions().Add(new RewriteSubdomainRule())) // convert subdomain.smapi.io => smapi.io/subdomain for routing - .UseMvc(route => - { - route.MapRoute( - name: "API", - template: "api/{version}/{controller}/{action?}", - defaults: new - { - action = "GetAsync" - }, - constraints: new - { - // version regex from SMAPI's SemanticVersion implementation - version = @"^v(?>(?<major>0|[1-9]\d*))\.(?>(?<minor>0|[1-9]\d*))(?>(?:\.(?<patch>0|[1-9]\d*))?)(?:-(?<prerelease>(?>[a-z0-9]+[\-\.]?)+))?$" - } - ); - }); + .UseMvc(); } } } |