IRpcRouteProvider.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using EdjCase.JsonRpc.Router.MethodProviders;
  2. using Microsoft.AspNetCore.Authorization;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace EdjCase.JsonRpc.Router.Abstractions
  9. {
  10. /// <summary>
  11. /// Interface to provide the middleware with available routes and their criteria
  12. /// </summary>
  13. public interface IRpcRouteProvider
  14. {
  15. RpcPath BaseRequestPath { get; }
  16. List<IRpcMethodProvider> GetMethodsByPath(RpcPath path);
  17. }
  18. #if !NETSTANDARD1_3
  19. public class RpcAutoRoutingOptions
  20. {
  21. /// <summary>
  22. /// Sets the required base path for the request url to match against
  23. /// </summary>
  24. public RpcPath BaseRequestPath { get; set; }
  25. public Type BaseControllerType { get; set; } = typeof(RpcController);
  26. }
  27. #endif
  28. public class RpcManualRoutingOptions
  29. {
  30. /// <summary>
  31. /// Sets the required base path for the request url to match against
  32. /// </summary>
  33. public RpcPath BaseRequestPath { get; set; }
  34. public Dictionary<RpcPath, List<IRpcMethodProvider>> Routes { get; set; } = new Dictionary<RpcPath, List<IRpcMethodProvider>>();
  35. public void RegisterMethods(RpcPath path, IRpcMethodProvider methodProvider)
  36. {
  37. if (!this.Routes.TryGetValue(path, out List<IRpcMethodProvider> methodProviders))
  38. {
  39. methodProviders = new List<IRpcMethodProvider>();
  40. this.Routes[path] = methodProviders;
  41. }
  42. methodProviders.Add(methodProvider);
  43. }
  44. public void RegisterController<T>(RpcPath path = default(RpcPath))
  45. {
  46. this.RegisterMethods(path, new ControllerPublicMethodProvider(typeof(T)));
  47. }
  48. }
  49. public interface IRpcMethodProvider
  50. {
  51. List<MethodInfo> GetRouteMethods();
  52. }
  53. }