summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/BeanstalkEnvPropsConfigProvider.cs
blob: fe27fe2f4a3ce6ec195ddb4d42e24c08f7c8e351 (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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace StardewModdingAPI.Web.Framework
{
    /// <summary>Reads configuration values from the AWS Beanstalk environment properties file (if present).</summary>
    /// <remarks>This is a workaround for AWS Beanstalk injection not working with .NET Core apps.</remarks>
    internal class BeanstalkEnvPropsConfigProvider : ConfigurationProvider, IConfigurationSource
    {
        /*********
        ** Fields
        *********/
        /// <summary>The absolute path to the container configuration file on an Amazon EC2 instance.</summary>
        private const string ContainerConfigPath = @"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration";


        /*********
        ** Public methods
        *********/
        /// <summary>Build the configuration provider for this source.</summary>
        /// <param name="builder">The configuration builder.</param>
        public IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            return new BeanstalkEnvPropsConfigProvider();
        }

        /// <summary>Load the environment properties.</summary>
        public override void Load()
        {
            this.Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            // get Beanstalk config file
            FileInfo file = new FileInfo(BeanstalkEnvPropsConfigProvider.ContainerConfigPath);
            if (!file.Exists)
                return;

            // parse JSON
            JObject jsonRoot = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(file.FullName));
            if (jsonRoot["iis"]?["env"] is JArray jsonProps)
            {
                foreach (string prop in jsonProps.Values<string>())
                {
                    string[] parts = prop.Split('=', 2); // key=value
                    if (parts.Length == 2)
                        this.Data[parts[0]] = parts[1];
                }
            }
        }
    }
}