ApiLoader.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Sers.SersLoader;
  6. using Sers.Core.Module.Api.ApiDesc;
  7. using Sers.Core.Module.Api.LocalApi;
  8. using Sers.Serslot;
  9. using Vit.Extensions;
  10. using Vit.Ioc;
  11. using Sers.Core.Module.Api.LocalApi.Event;
  12. namespace Sers.NetcoreLoader
  13. {
  14. public class ApiLoader : SersLoader.ApiLoader
  15. {
  16. static ApiLoader()
  17. {
  18. #region use ioc
  19. LocalApiEventMng.Instance.UseIoc();
  20. #endregion
  21. }
  22. public ApiLoader() :base()
  23. {
  24. }
  25. protected override IEnumerable<Type> LoadControllers(ApiLoaderConfig config)
  26. {
  27. var types = ControllerHelp.Assembly_GetControllers(config.assembly);
  28. foreach (var t in types)
  29. {
  30. IocHelp.AddScoped(t);
  31. }
  32. IocHelp.Update();
  33. return types;
  34. }
  35. #region GetRoutePrefix MethodInfoGetReturnType
  36. /// <summary>
  37. /// demo: ["/Auth/fold1/fold2","/api","/"]
  38. /// </summary>
  39. /// <param name="config"></param>
  40. /// <param name="type"></param>
  41. /// <returns></returns>
  42. protected override List<string> GetRoutePrefixs(ApiLoaderConfig config, Type type)
  43. {
  44. return ControllerHelp.Controller_GetRoutePrefixs(type);
  45. }
  46. protected override Type MethodInfoGetReturnType(MethodInfo method)
  47. {
  48. return ControllerHelp.Action_GetReturnType(method);
  49. }
  50. #endregion
  51. #region LoadApiNodes
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="routePrefixs">demo: ["/Auth/fold1/fold2","/api"]</param>
  56. /// <param name="method"></param>
  57. /// <param name="CreateApiDesc"></param>
  58. protected override List<IApiNode> LoadApiNodes(List<String> routePrefixs, MethodInfo method, Func<SsApiDesc> CreateApiDesc)
  59. {
  60. var routes = ControllerHelp.Action_GetRoutes(routePrefixs, method);
  61. // routes -> apiNode
  62. return routes.Select(routeInfo => {
  63. (string route, string httpMethod, string oriRoute) = routeInfo;
  64. var apiDesc = CreateApiDesc();
  65. apiDesc.route = route;
  66. apiDesc.HttpMethodSet(httpMethod);
  67. apiDesc.OriRouteSet(oriRoute);
  68. apiDesc.SysDescAppend("oriRoute: " + oriRoute);
  69. IApiNode apiNode = new LocalApiNode(apiDesc, method);
  70. return apiNode;
  71. }).ToList();
  72. }
  73. #endregion
  74. }
  75. }