request.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package rest
  2. import (
  3. "io"
  4. "time"
  5. "git.ali33.ru/fcg-xvii/go-tools/json"
  6. )
  7. type (
  8. // RequestType - тип запроса
  9. RequestType byte
  10. // RequestFiles - словарь файлов запроса
  11. RequestFiles map[string]IReadCloserLen
  12. // TokenGenerateMethod - функция генерации токена авторизации
  13. TokenGenerateMethod func(data json.Map, expire int64) (string, error)
  14. // TokenParseMethod - функция для расшифровки токена
  15. TokenParseMethod func(token string) (json.Map, error)
  16. )
  17. const (
  18. RequestTypeIn RequestType = iota
  19. RequestTypeOut
  20. RequestTypeEvent
  21. )
  22. type IRequest interface {
  23. RType() RequestType
  24. RCommand() string
  25. RData() json.Map
  26. RFiles() RequestFiles
  27. RFile(string) (IReadCloserLen, bool)
  28. RClose()
  29. }
  30. ////////////////////////////////////////////
  31. type Request struct {
  32. Type RequestType
  33. Command string
  34. Data json.Map
  35. Files RequestFiles
  36. }
  37. func (s *Request) RType() RequestType {
  38. return s.Type
  39. }
  40. func (s *Request) RCommand() string {
  41. return s.Command
  42. }
  43. func (s *Request) RData() json.Map {
  44. if s.Data == nil {
  45. return json.Map{}
  46. }
  47. return s.Data
  48. }
  49. func (s *Request) RFiles() RequestFiles {
  50. return s.Files
  51. }
  52. func (s *Request) RFile(name string) (IReadCloserLen, bool) {
  53. res, check := s.Files[name]
  54. return res, check
  55. }
  56. func (s *Request) RClose() {
  57. for _, file := range s.Files {
  58. file.Close()
  59. }
  60. s.Files = nil
  61. }
  62. func (s *Request) ParseOffset() int {
  63. return s.Data.Int32("offset", 0)
  64. }
  65. func (s *Request) ParseLimit() int {
  66. return s.Data.Int32("limit", 20)
  67. }
  68. ////////////////////////////////////////////////////////////
  69. type IRequestIn interface {
  70. IRequest
  71. Auth() json.Map
  72. SetAuth(json.Map)
  73. GenerateToken(data json.Map, expire int64) (string, error)
  74. ParseToken(token string) (json.Map, error)
  75. ROwner() IOwner
  76. RCore() any
  77. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  78. OutError(err IErrorArgs) IRequestOut
  79. ClientData(key string) (any, bool)
  80. SetClientData(key string, data any)
  81. Fields() FieldList
  82. }
  83. type RequestIn struct {
  84. IRequest
  85. Owner IOwner
  86. Core any
  87. GeneratorToken TokenGenerateMethod
  88. ParserToken TokenParseMethod
  89. }
  90. func (s *RequestIn) Auth() json.Map {
  91. if store, check := s.Owner.(IStream); check {
  92. return store.Auth()
  93. }
  94. return nil
  95. }
  96. func (s *RequestIn) SetAuth(auth json.Map) {
  97. if store, check := s.Owner.(IStream); check {
  98. store.SetAuth(auth)
  99. }
  100. }
  101. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  102. return s.GeneratorToken(data, expire)
  103. }
  104. func (s *RequestIn) ParseToken(token string) (json.Map, error) {
  105. return s.ParserToken(token)
  106. }
  107. func (s *RequestIn) ClientData(key string) (any, bool) {
  108. if store, check := s.Owner.(IStream); check {
  109. return store.ClientData(key)
  110. }
  111. return nil, false
  112. }
  113. func (s *RequestIn) SetClientData(key string, data any) {
  114. if store, check := s.Owner.(IStream); check {
  115. store.SetClientData(key, data)
  116. }
  117. }
  118. func (s *RequestIn) ROwner() IOwner {
  119. return s.Owner
  120. }
  121. func (s *RequestIn) RCore() any {
  122. return s.Core
  123. }
  124. func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
  125. return &RequestOut{
  126. Request: &Request{
  127. Type: RequestTypeOut,
  128. Command: s.RCommand(),
  129. Data: data,
  130. Files: files,
  131. },
  132. }
  133. }
  134. func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
  135. return &RequestOut{
  136. Err: err,
  137. }
  138. }
  139. /////////////////////////////////////////////////////////
  140. type IRequestOut interface {
  141. IRequest
  142. Write(io.Writer) IErrorArgs
  143. }
  144. /////////////////////////////////////////////////////////
  145. type RequestOut struct {
  146. *Request
  147. Err IErrorArgs
  148. }
  149. func (s *RequestOut) Write(w io.Writer) IErrorArgs {
  150. return nil
  151. }
  152. //////////////////////////////////////////////////////
  153. func NewRequestStreamEvent(command string, data json.Map, files RequestFiles) IRequestOut {
  154. return &RequestStream{
  155. Timeout: time.Now().Add(time.Minute),
  156. Request: &Request{
  157. Type: RequestTypeEvent,
  158. Command: command,
  159. Data: data,
  160. Files: files,
  161. },
  162. }
  163. }
  164. func NewRequestStreamMessage(command string, data json.Map, files RequestFiles) IRequestOut {
  165. return &RequestStream{
  166. Timeout: time.Now().Add(time.Minute),
  167. Request: &Request{
  168. Type: RequestTypeIn,
  169. Command: command,
  170. Data: data,
  171. Files: files,
  172. },
  173. }
  174. }