diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-23 20:16:52 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-23 20:16:52 -0400 |
commit | 36a04a6e77eac74be660dc9848b6045834479f4f (patch) | |
tree | 1176a8920a7656f720e0b36ebc4fe45b26cc202a /src/StardewModdingAPI.Web/Startup.cs | |
parent | f0e2117f70455bd9883321ae5b0bf40562f2d5de (diff) | |
parent | 57111a6e8fa6a23bb56f515b50f8b7ea5924d49f (diff) | |
download | SMAPI-36a04a6e77eac74be660dc9848b6045834479f4f.tar.gz SMAPI-36a04a6e77eac74be660dc9848b6045834479f4f.tar.bz2 SMAPI-36a04a6e77eac74be660dc9848b6045834479f4f.zip |
Merge branch 'feature/update-check-api' into develop
Diffstat (limited to 'src/StardewModdingAPI.Web/Startup.cs')
-rw-r--r-- | src/StardewModdingAPI.Web/Startup.cs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs new file mode 100644 index 00000000..d5b828b7 --- /dev/null +++ b/src/StardewModdingAPI.Web/Startup.cs @@ -0,0 +1,83 @@ +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; +using StardewModdingAPI.Web.Framework.ConfigModels; + +namespace StardewModdingAPI.Web +{ + /// <summary>The web app startup configuration.</summary> + internal class Startup + { + /********* + ** Accessors + *********/ + /// <summary>The web app configuration.</summary> + public IConfigurationRoot Configuration { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="env">The hosting environment.</param> + public Startup(IHostingEnvironment env) + { + this.Configuration = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddEnvironmentVariables() + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddEnvironmentVariables() + .Build(); + } + + /// <summary>The method called by the runtime to add services to the container.</summary> + /// <param name="services">The service injection container.</param> + public void ConfigureServices(IServiceCollection services) + { + services + .Configure<ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck")) + .AddMemoryCache() + .AddMvc() + .ConfigureApplicationPartManager(manager => manager.FeatureProviders.Add(new InternalControllerFeatureProvider())) + .AddJsonOptions(options => + { + options.SerializerSettings.Formatting = Formatting.Indented; + options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; + }); + } + + /// <summary>The method called by the runtime to configure the HTTP request pipeline.</summary> + /// <param name="app">The application builder.</param> + /// <param name="env">The hosting environment.</param> + /// <param name="loggerFactory">The logger factory.</param> + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + { + loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); + 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]+[\-\.]?)+))?$" + } + ); + }); + } + } +} |