diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 13:15:05 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 13:15:05 -0400 |
commit | 6638701d0221e82daa42581eddb3cf051d1f8de4 (patch) | |
tree | 89d82cc7e5fd29a8990358afb85d4de5c1d6d6f2 /src | |
parent | 790a62920b15f1f948724f5b2a70a937829355dd (diff) | |
download | SMAPI-6638701d0221e82daa42581eddb3cf051d1f8de4.tar.gz SMAPI-6638701d0221e82daa42581eddb3cf051d1f8de4.tar.bz2 SMAPI-6638701d0221e82daa42581eddb3cf051d1f8de4.zip |
fix config not being injected from Amazon Beanstalk env props
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Web/Framework/BeanstalkEnvPropsConfigProvider.cs | 54 | ||||
-rw-r--r-- | src/SMAPI.Web/Startup.cs | 3 |
2 files changed, 55 insertions, 2 deletions
diff --git a/src/SMAPI.Web/Framework/BeanstalkEnvPropsConfigProvider.cs b/src/SMAPI.Web/Framework/BeanstalkEnvPropsConfigProvider.cs new file mode 100644 index 00000000..b39a3b61 --- /dev/null +++ b/src/SMAPI.Web/Framework/BeanstalkEnvPropsConfigProvider.cs @@ -0,0 +1,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 + { + /********* + ** Properties + *********/ + /// <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]; + } + } + } + } +} diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index b27ff9a5..860354f1 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -31,10 +31,9 @@ namespace StardewModdingAPI.Web { this.Configuration = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) - .AddEnvironmentVariables() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables() + .Add(new BeanstalkEnvPropsConfigProvider()) //.AddEnvironmentVariables() .Build(); } |