| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Features; |
| | 4 | |
|
| | 5 | | namespace IceRpc; |
| | 6 | |
|
| | 7 | | /// <summary>Provides extension methods for <see cref="Router" />.</summary> |
| | 8 | | public static class RouterExtensions |
| | 9 | | { |
| | 10 | | /// <summary>Registers a route to a service that uses the service's default path as the route path. If there is an |
| | 11 | | /// existing route at the same path, it is replaced.</summary> |
| | 12 | | /// <typeparam name = "TService" > An interface with the DefaultServicePathAttribute attribute. The path of the |
| | 13 | | /// mapped service corresponds to the value of this attribute.</typeparam> |
| | 14 | | /// <param name="router">The router being configured.</param> |
| | 15 | | /// <param name="service">The target service of this route.</param> |
| | 16 | | /// <returns>The router being configured.</returns> |
| | 17 | | /// <exception cref="InvalidOperationException">Thrown if <see cref="IDispatcher.DispatchAsync" /> was already |
| | 18 | | /// called on this router.</exception> |
| | 19 | | public static Router Map<TService>(this Router router, IDispatcher service) |
| | 20 | | where TService : class => |
| 1 | 21 | | router.Map(typeof(TService).GetDefaultServicePath(), service); |
| | 22 | |
|
| | 23 | | /// <summary>Creates a sub-router, configures this sub-router and mounts it (with |
| | 24 | | /// <see cref="Router.Mount(string, IDispatcher)" />) at the given <c>prefix</c>.</summary> |
| | 25 | | /// <param name="router">The router being configured.</param> |
| | 26 | | /// <param name="prefix">The prefix of the route to the sub-router.</param> |
| | 27 | | /// <param name="configure">A delegate that configures the new sub-router.</param> |
| | 28 | | /// <returns>The new sub-router.</returns> |
| | 29 | | /// <exception cref="FormatException">Thrown if <paramref name="prefix" /> is not a valid path.</exception> |
| | 30 | | public static Router Route(this Router router, string prefix, Action<Router> configure) |
| 16 | 31 | | { |
| 16 | 32 | | ServiceAddress.CheckPath(prefix); |
| 16 | 33 | | var subRouter = new Router($"{router.AbsolutePrefix}{prefix}"); |
| 16 | 34 | | configure(subRouter); |
| 16 | 35 | | router.Mount(prefix, subRouter); |
| 16 | 36 | | return subRouter; |
| 16 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary>Adds a middleware that creates and inserts the <see cref="IDispatchInformationFeature" /> feature |
| | 40 | | /// in all requests.</summary> |
| | 41 | | /// <param name="router">The router being configured.</param> |
| | 42 | | /// <returns>The router being configured.</returns> |
| | 43 | | public static Router UseDispatchInformation(this Router router) => |
| 0 | 44 | | router.Use(next => new InlineDispatcher((request, cancellationToken) => |
| 0 | 45 | | { |
| 0 | 46 | | request.Features = request.Features.With<IDispatchInformationFeature>( |
| 0 | 47 | | new DispatchInformationFeature(request)); |
| 0 | 48 | | return next.DispatchAsync(request, cancellationToken); |
| 0 | 49 | | })); |
| | 50 | |
|
| | 51 | | /// <summary>Adds a middleware that sets a feature in all requests.</summary> |
| | 52 | | /// <typeparam name="TFeature">The type of the feature.</typeparam> |
| | 53 | | /// <param name="router">The router being configured.</param> |
| | 54 | | /// <param name="feature">The value of the feature to set in all requests.</param> |
| | 55 | | /// <returns>The router being configured.</returns> |
| | 56 | | public static Router UseFeature<TFeature>(this Router router, TFeature feature) => |
| 2 | 57 | | router.Use(next => new InlineDispatcher((request, cancellationToken) => |
| 1 | 58 | | { |
| 1 | 59 | | request.Features = request.Features.With(feature); |
| 1 | 60 | | return next.DispatchAsync(request, cancellationToken); |
| 3 | 61 | | })); |
| | 62 | | } |