using System.Net; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Rewrite; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using SMAPI.Web.LegacyRedirects.Framework; using StardewModdingAPI.Toolkit.Serialization; namespace SMAPI.Web.LegacyRedirects { /// The web app startup configuration. public class Startup { /********* ** Public methods *********/ /// The method called by the runtime to add services to the container. /// The service injection container. public void ConfigureServices(IServiceCollection services) { services .AddControllers() .AddNewtonsoftJson(options => Startup.ConfigureJsonNet(options.SerializerSettings)); } /// The method called by the runtime to configure the HTTP request pipeline. /// The application builder. /// The hosting environment. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) app.UseDeveloperExceptionPage(); app .UseRewriter(this.GetRedirectRules()) .UseRouting() .UseAuthorization() .UseEndpoints(endpoints => endpoints.MapControllers()); } /// Configure a Json.NET serializer. /// The serializer settings to edit. internal static void ConfigureJsonNet(JsonSerializerSettings settings) { foreach (JsonConverter converter in new JsonHelper().JsonSettings.Converters) settings.Converters.Add(converter); settings.Formatting = Formatting.Indented; settings.NullValueHandling = NullValueHandling.Ignore; } /********* ** Private methods *********/ /// Get the redirect rules to apply. private RewriteOptions GetRedirectRules() { var redirects = new RewriteOptions(); redirects.Add( new LambdaRewriteRule((context, request, response) => { string host = request.Host.Host; // map API requests to proxy // This is needed because the low-level HTTP client SMAPI uses for Linux/Mac compatibility doesn't support redirects. if (host == "api.smapi.io") { request.Path = $"/api{request.Path}"; return; } // redirect other requests to Azure string newRoot = host switch { "api.smapi.io" => "smapi.io/api", "json.smapi.io" => "smapi.io/json", "log.smapi.io" => "smapi.io/log", "mods.smapi.io" => "smapi.io/mods", _ => "smapi.io" }; response.StatusCode = (int)HttpStatusCode.PermanentRedirect; response.Headers["Location"] = $"{(request.IsHttps ? "https" : "http")}://{newRoot}{request.PathBase}{request.Path}{request.QueryString}"; context.Result = RuleResult.EndResponse; }) ); return redirects; } } }