Explorar el Código

Improved config injection

Lukas Angerer hace 5 años
padre
commit
3c9244f372

+ 0 - 15
CronAlarm/Config/AlertConfig.cs

@@ -1,15 +0,0 @@
-using Microsoft.Extensions.Configuration;
-
-namespace CronAlarm.Config
-{
-    public class AlertConfig
-    {
-        public Alerts Alerts { get; }
-
-        public AlertConfig(IConfiguration config)
-        {
-            Alerts = new Alerts();
-            config.GetSection("Alerts").Bind(Alerts);
-        }
-    }
-}

+ 7 - 0
CronAlarm/Config/CronFragment.cs

@@ -0,0 +1,7 @@
+namespace CronAlarm.Config
+{
+    public class CronFragment
+    {
+        public string FilePath { get; set; }
+    }
+}

+ 6 - 0
CronAlarm/CronAlarm.csproj

@@ -6,6 +6,12 @@
     <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
   </PropertyGroup>
 
+  <ItemGroup>
+    <None Include="wwwroot\cron-fragment.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
   <ItemGroup>
     <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
   </ItemGroup>

+ 11 - 0
CronAlarm/CronFragmentHandler.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace CronAlarm
+{
+    public class CronFragmentHandler : ICrontFragmentHandler
+    {
+    }
+}

+ 11 - 0
CronAlarm/ICrontFragmentHandler.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace CronAlarm
+{
+    public interface ICrontFragmentHandler
+    {
+    }
+}

+ 1 - 1
CronAlarm/Pages/AlertSelection.razor

@@ -1,4 +1,4 @@
-@foreach (var group in Config.Alerts.Groups)
+@foreach (var group in Config.Groups)
 {
     <div class="row">
         <div class="col-12">

+ 1 - 1
CronAlarm/Pages/AlertSelection.razor.cs

@@ -10,7 +10,7 @@ namespace CronAlarm.Pages
     public partial class AlertSelection : IComponent
     {
         [Inject]
-        private AlertConfig Config { get; set; }
+        private Alerts Config { get; set; }
 
         private IDictionary<string, int> SelectedOptions { get; set; } = new Dictionary<string, int>();
 

+ 18 - 1
CronAlarm/Program.cs

@@ -1,5 +1,6 @@
 using CronAlarm.Config;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
 using Ninject;
 using Ninject.Web.AspNetCore;
 using Ninject.Web.AspNetCore.Hosting;
@@ -32,7 +33,23 @@ namespace CronAlarm
 
             kernel.Load(typeof(AspNetCoreHostConfiguration).Assembly);
 
-            kernel.Bind<AlertConfig>().ToSelf();
+            kernel.Bind<ICrontFragmentHandler>().To<CronFragmentHandler>();
+
+            kernel.Bind<Alerts>().ToMethod(ctx =>
+            {
+                var config = ctx.Kernel.Get<IConfiguration>();
+                var section = new Alerts();
+                config.GetSection("Alerts").Bind(section);
+                return section;
+            });
+
+            kernel.Bind<CronFragment>().ToMethod(ctx =>
+            {
+                var config = ctx.Kernel.Get<IConfiguration>();
+                var section = new CronFragment();
+                config.GetSection("CronFragment").Bind(section);
+                return section;
+            });
 
             return kernel;
         }

+ 3 - 0
CronAlarm/appsettings.json

@@ -7,6 +7,9 @@
     }
   },
   "AllowedHosts": "*",
+  "CronFragment": {
+    "FilePath": "./wwwroot/cron-fragment.txt"
+  },
   "Alerts": {
     "Command": "/home/pi/mpc-alarm-cron.sh",
     "Groups": [

+ 3 - 0
CronAlarm/wwwroot/cron-fragment.txt

@@ -0,0 +1,3 @@
+30 08 * * MON-FRI       /home/pi/mpc-alarm-cron.sh
+20 08 * * SAT           /home/pi/mpc-alarm-cron.sh
+00 09 * * SUN           /home/pi/mpc-alarm-cron.sh