ApiLoader.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Vit.Extensions;
  9. namespace Sers.Serslot
  10. {
  11. public class ApiLoader : SersLoader.ApiLoader
  12. {
  13. SerslotServer server;
  14. public ApiLoader(SerslotServer server) :base()
  15. {
  16. this.server = server;
  17. }
  18. protected override IEnumerable<Type> LoadControllers(ApiLoaderConfig config)
  19. {
  20. var types = ControllerHelp.Assembly_GetControllers(config.assembly);
  21. return types;
  22. }
  23. #region GetRoutePrefix MethodInfoGetReturnType
  24. /// <summary>
  25. /// demo: ["/Auth/fold1/fold2","/api","/"]
  26. /// </summary>
  27. /// <param name="config"></param>
  28. /// <param name="type"></param>
  29. /// <returns></returns>
  30. protected override List<string> GetRoutePrefixs(ApiLoaderConfig config, Type type)
  31. {
  32. return ControllerHelp.Controller_GetRoutePrefixs(type);
  33. }
  34. protected override Type MethodInfoGetReturnType(MethodInfo method)
  35. {
  36. return ControllerHelp.Action_GetReturnType(method);
  37. }
  38. #endregion
  39. #region LoadApiNodes
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. /// <param name="routePrefixs">demo: ["/Auth/fold1/fold2","/api"]</param>
  44. /// <param name="method"></param>
  45. /// <param name="CreateApiDesc"></param>
  46. protected override List<IApiNode> LoadApiNodes(List<String> routePrefixs, MethodInfo method, Func<SsApiDesc> CreateApiDesc)
  47. {
  48. var routes = ControllerHelp.Action_GetRoutes(routePrefixs, method);
  49. // routes -> apiNode
  50. return routes.Select(routeInfo => {
  51. (string route, string httpMethod, string oriRoute) = routeInfo;
  52. var apiDesc = CreateApiDesc();
  53. apiDesc.route = route;
  54. apiDesc.HttpMethodSet(httpMethod);
  55. apiDesc.OriRouteSet(oriRoute);
  56. apiDesc.SysDescAppend("oriRoute: " + oriRoute);
  57. IApiNode apiNode = new LocalApiNode(apiDesc, server);
  58. return apiNode;
  59. }).ToList();
  60. }
  61. #endregion
  62. }
  63. }