苏州瑞玛APS项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.6 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/kataras/iris/v12/context"
  7. "github.com/kataras/iris/v12/core/router"
  8. "leit.com/LAPP_CHEERSSON_BACKEND/global"
  9. "reflect"
  10. "strings"
  11. )
  12. const (
  13. ServicesPackageName = "services"
  14. ServiceNameSuffix = "Service"
  15. PathSeparator = "/"
  16. HttpPost HttpMethod = "POST"
  17. HttpPut HttpMethod = "POU"
  18. HttpGet HttpMethod = "Get"
  19. HttpDelete HttpMethod = "DELETE"
  20. )
  21. var ServiceNameSuffixLength = len([]rune(ServiceNameSuffix))
  22. type HttpMethod string
  23. type (
  24. // 调用器
  25. // 参数
  26. // 1.参数列表
  27. // 返回值:
  28. // 1.返回值列表
  29. Caller = func([]reflect.Value) []reflect.Value
  30. // 调用器工厂
  31. // 参数
  32. // 1.会话标识
  33. // 2.用户信息
  34. // 返回值:
  35. // 1.调用器
  36. // 2.错误
  37. CallerFactory = func(string, *global.User) (Caller, error)
  38. // 路由
  39. Router = context.Handler
  40. // 路由创建器
  41. // 参数
  42. // 1.会话管理器
  43. // 2.服务类型
  44. // 3.服务方法名
  45. // 3.服务方法类型
  46. // 返回值:
  47. // 1.HTTP请求方法
  48. // 2.附加路径
  49. // 3.路由
  50. // 4.错误
  51. RouterBuilder = func(CallerFactory, string, reflect.Type) (HttpMethod, string, Router, error)
  52. )
  53. // *SessionContext类型常量
  54. var sessionContextType = reflect.TypeOf((*SessionContext)(nil))
  55. // 空参数列表
  56. var emptyParameters []reflect.Value
  57. // Factory零值
  58. var zeroOfFactory = Factory(reflect.ValueOf(nil))
  59. // *error 类型常量
  60. var errorType = reflect.TypeOf((*error)(nil)).Elem()
  61. // *RequestContext类型常量
  62. var RequestContextType = reflect.TypeOf((*RequestContext)(nil))
  63. // 无会话(XORM会话)上下文
  64. var ContextOfNoSession = NewNoSessionBrokerBuilder()
  65. // 新会话(XORM会话)上下文
  66. var ContextOfNewSession = NewNewSessionBrokerBuilder()
  67. // 新事务代理创建器上下文
  68. var ContextOfNewTransaction = NewNewTransactionBrokerBuilder()
  69. //
  70. //// 组件信息管理器
  71. //var currentInformationManager = NewInformationManager()
  72. // 解析组件工厂,返回:组件工厂Value,组件模块名,组件接口,错误
  73. // 参数
  74. // 1.工厂方法
  75. // 返回值:
  76. // 1.组件工厂
  77. // 2.组件接口类型
  78. // 异常:
  79. // 1.工厂不能为空
  80. // 2.工厂必须是func
  81. // 3.工厂不能有参数
  82. // 4.工厂必值需有返回值
  83. // 5.工厂只能有一个返回值
  84. // 6.工厂的返回值必需是interface
  85. func parseFactory(factory interface{}) (Factory, Interface) {
  86. if factory == nil {
  87. panic(fmt.Sprintf("工厂不能为空!"))
  88. }
  89. valueOfFactory := reflect.ValueOf(factory)
  90. typeOfFactory := valueOfFactory.Type()
  91. if typeOfFactory.Kind() != reflect.Func {
  92. panic(fmt.Sprintf("工厂必须是func!"))
  93. }
  94. if typeOfFactory.NumIn() != 0 {
  95. panic(fmt.Sprintf("工厂不能有参数!"))
  96. }
  97. if typeOfFactory.NumOut() < 1 {
  98. panic(fmt.Sprintf("工厂必值需有返回值!"))
  99. }
  100. if typeOfFactory.NumOut() > 1 {
  101. panic(fmt.Sprintf("工厂只能有一个返回值!"))
  102. }
  103. if typeOfFactory.Out(0).Kind() != reflect.Interface {
  104. panic(fmt.Sprintf("工厂的返回值必需是interface!"))
  105. }
  106. componentType := typeOfFactory.Out(0)
  107. return Factory(valueOfFactory), componentType
  108. }
  109. // Caller相等性判断
  110. // 参数
  111. // 1.比较对象1
  112. // 3.比较对象2
  113. // 返回值:
  114. // 1.判断结果
  115. func CallerEqual(caller1 Caller, caller2 Caller) bool {
  116. if caller1 == nil {
  117. if caller2 == nil {
  118. return true
  119. } else {
  120. return false
  121. }
  122. } else {
  123. if caller2 == nil {
  124. return false
  125. } else {
  126. return reflect.ValueOf(caller1).Pointer() == reflect.ValueOf(caller2).Pointer()
  127. }
  128. }
  129. }
  130. var GlobalInformations = NewInformationManager()
  131. // 注册路由
  132. // 参数:
  133. // 1.路由分组
  134. // 2.组件信息管理器
  135. // 3.事务句柄工厂
  136. // 返回值:
  137. // 1.错误
  138. // 异常:
  139. // 1.组件信息管理器不能为空
  140. // 2.事务句柄工厂不能为空
  141. // 3.服务不在包或他的子包之下
  142. // 4.组件接口名称的后缀不正确
  143. func RegisterRoutes(party router.Party, defaultRouterBuilder RouterBuilder, informationManager InformationManager, transactionHandlerFactory TransactionHandlerFactory) {
  144. sessionManager := NewSessionManager(informationManager, transactionHandlerFactory)
  145. groups := make(map[string][]*ServiceInformation, 10)
  146. for _, information := range informationManager.Items() {
  147. if serviceInformation, ok := information.(*ServiceInformation); ok {
  148. packagePath := serviceInformation.Interface().PkgPath()
  149. group, ok := groups[packagePath]
  150. if !ok {
  151. group = make([]*ServiceInformation, 0, 10)
  152. }
  153. groups[packagePath] = append(group, serviceInformation)
  154. }
  155. for packagePath, group := range groups {
  156. packageList := strings.Split(packagePath, PathSeparator)
  157. moudlePath := ""
  158. for index, packageName := range packageList {
  159. if strings.ToLower(packageName) == ServicesPackageName {
  160. moudlePath = strings.Join(packageList[index+1:], PathSeparator)
  161. break
  162. }
  163. }
  164. if moudlePath == "" {
  165. panic(fmt.Sprintf("包%s不在包%s或他的子包之下!", packagePath, ServicesPackageName))
  166. }
  167. moudleParty := party.Party(fmt.Sprintf("/%s", strings.ToLower(moudlePath)))
  168. for _, serviceInformation := range group {
  169. serviceType := serviceInformation.Interface()
  170. serviceName := serviceType.Name()
  171. if !strings.HasSuffix(serviceName, ServiceNameSuffix) {
  172. panic(fmt.Sprintf("组件%s接口名称的后缀不是%s!", serviceName, ServiceNameSuffix))
  173. }
  174. runesOfserviceName := []rune(serviceName)
  175. serviceParty := moudleParty.Party(fmt.Sprintf("/%s", strings.ToLower(string(runesOfserviceName[:len(runesOfserviceName)-ServiceNameSuffixLength]))))
  176. for _, serviceMethodInformation := range serviceInformation.Methods() {
  177. methodName := serviceMethodInformation.MethodName()
  178. routerBuilder := serviceMethodInformation.RouterBuilder()
  179. if routerBuilder == nil {
  180. routerBuilder = defaultRouterBuilder
  181. }
  182. var callerFactory = func(sessionId string, user *global.User) (Caller, error) {
  183. session, err := sessionManager.GetSession(sessionId, user)
  184. if err != nil {
  185. return nil, err
  186. }
  187. serviceHandler, ok := session.Handlers().Handler(serviceType).(*ServiceHandler)
  188. if !ok {
  189. return nil, errors.New(fmt.Sprintf("竟然没找到服务组件句柄!"))
  190. }
  191. return serviceHandler.Method(methodName).Caller(), nil
  192. }
  193. serviceMethodName := fmt.Sprintf("%s.%s", serviceName, methodName)
  194. httpMethod, addedPath, irisRouter, err := routerBuilder(callerFactory, serviceMethodName, serviceMethodInformation.MethodType())
  195. if err != nil {
  196. panic(err.Error())
  197. }
  198. serviceParty.Handle(string(httpMethod), fmt.Sprintf("/%s", strings.ToLower(methodName)+addedPath), irisRouter)
  199. }
  200. }
  201. }
  202. }
  203. }