| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Microsoft.Extensions.Caching.Memory; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Http.Services; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 329 | 10 | | public class RouteTable(IMemoryCache cache, ILogger<RouteTable> logger) : IRouteTable |
| | | 11 | | { |
| | 2 | 12 | | private static readonly object Key = new(); |
| | | 13 | | |
| | 521 | 14 | | private ConcurrentDictionary<string, HttpRouteData> Routes => cache.GetOrCreate(Key, _ => new ConcurrentDictionary<s |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public void Add(string route) |
| | | 18 | | { |
| | 0 | 19 | | Add(new HttpRouteData(route)); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public void Add(HttpRouteData httpRouteData) |
| | | 24 | | { |
| | 62 | 25 | | var route = httpRouteData.Route; |
| | 62 | 26 | | if (route.Contains("//")) |
| | | 27 | | { |
| | 0 | 28 | | logger.LogWarning("Path cannot contain double slashes. Ignoring path: {Path}", route); |
| | 0 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | 62 | 32 | | var normalizedRoute = route.NormalizeRoute(); |
| | 62 | 33 | | Routes.TryAdd(normalizedRoute, httpRouteData); |
| | 62 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public void Remove(string route) |
| | | 38 | | { |
| | 6 | 39 | | var normalizedRoute = route.NormalizeRoute(); |
| | 6 | 40 | | Routes.TryRemove(normalizedRoute, out _); |
| | 6 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public void AddRange(IEnumerable<string> routes) |
| | | 45 | | { |
| | 0 | 46 | | foreach (var route in routes) Add(route); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public void RemoveRange(IEnumerable<string> routes) |
| | | 51 | | { |
| | 1652 | 52 | | foreach (var route in routes) Remove(route); |
| | 817 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | 450 | 56 | | public IEnumerator<HttpRouteData> GetEnumerator() => Routes.Values.GetEnumerator(); |
| | | 57 | | |
| | 0 | 58 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 59 | | } |