summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web.LegacyRedirects/Startup.cs
blob: 4af5157508ab01ce3758a79154f6e9087dbe3244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
{
    /// <summary>The web app startup configuration.</summary>
    public class Startup
    {
        /*********
        ** Public methods
        *********/
        /// <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
                .AddControllers()
                .AddNewtonsoftJson(options => Startup.ConfigureJsonNet(options.SerializerSettings));
        }

        /// <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>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();

            app
                .UseRewriter(this.GetRedirectRules())
                .UseRouting()
                .UseAuthorization()
                .UseEndpoints(endpoints => endpoints.MapControllers());
        }

        /// <summary>Configure a Json.NET serializer.</summary>
        /// <param name="settings">The serializer settings to edit.</param>
        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
        *********/
        /// <summary>Get the redirect rules to apply.</summary>
        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;
        }
    }
}