diff options
author | vnugent <public@vaughnnugent.com> | 2024-05-22 15:28:54 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-05-22 15:28:54 -0400 |
commit | 1350c983c371fdd6a93596c8474345f9168284e1 (patch) | |
tree | 7c6f1815d9a01bec4a97c2f53c3ec0f608222b45 /lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs | |
parent | 5ad520e078414ca26f6144da0aab4e975347a121 (diff) |
Squashed commit of the following:
commit 27fb5382d80d9bcfb4c65974bbae20c5e7b8ccbc
Author: vnugent <public@vaughnnugent.com>
Date: Wed May 22 00:57:34 2024 -0400
feat: Vault environment vars
commit 69f13e43dfdd8069459800ccc3039f45fc884814
Author: vnugent <public@vaughnnugent.com>
Date: Wed May 15 22:04:43 2024 -0400
fix: #3 Defer vault loading until a secret actually needs it
commit c848787d4830a73e9ba93898897282be2f3752f2
Author: vnugent <public@vaughnnugent.com>
Date: Wed May 15 22:02:02 2024 -0400
package updates
commit 21c6c85f540740ac29536a7091346a731aa85148
Author: vnugent <public@vaughnnugent.com>
Date: Wed May 15 22:01:16 2024 -0400
fix: #3 Error raised when managed password type disposed
commit 8e77289041349b16536497f48f0c0a4ec6fe30f5
Author: vnugent <public@vaughnnugent.com>
Date: Thu May 2 15:44:42 2024 -0400
feat: #2 Middleware helpers, proj cleanup, fix sync secrets, vault client
commit e0a5c85297516188e57b54d9b530b2482cb03eb0
Merge: a977dab 5ad520e
Author: vnugent <public@vaughnnugent.com>
Date: Sat Apr 27 17:44:09 2024 -0400
Merge branch 'master' into develop
commit a977dabef1dec915e00f755cb3ee3363aa9985f1
Author: vnugent <public@vaughnnugent.com>
Date: Sat Apr 27 17:26:35 2024 -0400
chore: package updates
commit a2e2c3c4152d000b8df25c3c3fee14d491aab2c6
Merge: f03b727 87bfa83
Author: vnugent <public@vaughnnugent.com>
Date: Sat Apr 20 12:11:45 2024 -0400
Merge branch 'master' into develop
commit f03b727d8f8e52f1dbd6293ea5c5a492c6d8e2da
Author: vnugent <public@vaughnnugent.com>
Date: Sat Apr 20 12:02:07 2024 -0400
chore: Package updates
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs')
-rw-r--r-- | lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs b/lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs new file mode 100644 index 0000000..6a0d848 --- /dev/null +++ b/lib/VNLib.Plugins.Extensions.Loading/src/Routing/MiddlewareHelpers.cs @@ -0,0 +1,70 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Extensions.Loading +* File: MiddlewareHelpers.cs +* +* MiddlewareHelpers.cs is part of VNLib.Plugins.Extensions.Loading which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Extensions.Loading is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Extensions.Loading is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +using VNLib.Utils.Extensions; +using VNLib.Plugins.Essentials.Middleware; + +namespace VNLib.Plugins.Extensions.Loading.Routing +{ + /// <summary> + /// Provides helper extensions for http middleware + /// </summary> + public static class MiddlewareHelpers + { + private static readonly ConditionalWeakTable<PluginBase, List<IHttpMiddleware>> _pluginMiddlewareList = new(); + + /// <summary> + /// Exports a single middlware instance to the collection for the plugin. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="plugin"></param> + /// <param name="instances">A params array of middleware instances to export to the plugin</param> + /// <remarks> + /// WARNING: Adding middleware arrays explicitly to the plugin service pool will override + /// this function. All instances must be exposed though this function + /// </remarks> + public static void ExportMiddleware<T>(this PluginBase plugin, params T[] instances) where T : IHttpMiddleware + { + /* + * The runtime accepts an enumeration of IHttpMiddleware instances, so + * a list can just be exported as an enumerable instance + */ + static List<IHttpMiddleware> OnCreate(PluginBase plugin) + { + List<IHttpMiddleware> collection = new(1); + plugin.ExportService<IEnumerable<IHttpMiddleware>>(collection); + return collection; + } + + //Get the endpoint collection for the current plugin + List<IHttpMiddleware> middlewares = _pluginMiddlewareList.GetValue(plugin, OnCreate); + + //Add the endpoint to the collection + instances.ForEach(mw => middlewares.Add(mw)); + } + } +} |