z_test_api.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package rest
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "log"
  7. "git.ali33.ru/fcg-xvii/go-tools/json"
  8. )
  9. type ObjectDemo struct{}
  10. func (s *ObjectDemo) Validate(r *Request) *Response {
  11. key := r.data.KeysExists([]string{"get-one", "get-two"})
  12. if key != "" {
  13. return ResponseErrKeyNotExists(key)
  14. }
  15. if !r.data.IsBoolean("get-one") {
  16. return ResponseErrKeyInvalidType("get-one", "boolean")
  17. }
  18. if !r.data.IsBoolean("get-two") {
  19. return ResponseErrKeyInvalidType("get-two", "boolean")
  20. }
  21. return nil
  22. }
  23. func (s *ObjectDemo) Execute(r *Request) *Response {
  24. if fKeys := r.FileKeys(); len(fKeys) > 0 {
  25. log.Println("FILES", fKeys)
  26. tFile, _ := r.File(fKeys[0])
  27. fContent, _ := io.ReadAll(tFile)
  28. log.Println("FILE CONTENT", string(fContent))
  29. }
  30. resp := NewResponse()
  31. var fileNames []string
  32. if r.data.Bool("get-one", true) {
  33. fileNames = append(fileNames, "get-one.txt")
  34. buf := bytes.NewBuffer([]byte("get-one txt file :)"))
  35. resp.FileSet("get-one.txt", io.NopCloser(buf))
  36. }
  37. if r.data.Bool("get-two", true) {
  38. fileNames = append(fileNames, "get-two.txt")
  39. buf := bytes.NewBuffer([]byte("get-two txt file :)"))
  40. resp.FileSet("get-two.txt", io.NopCloser(buf))
  41. }
  42. resp.KeySet("file_names", fileNames)
  43. return resp
  44. }
  45. ////////////////////////////////////////////////////
  46. type JSONDebug struct{}
  47. func (s *JSONDebug) Validate(r *Request) *Response {
  48. return nil
  49. }
  50. func (s *JSONDebug) Execute(r *Request) *Response {
  51. resp := NewResponse()
  52. resp.KeySet("completed", true)
  53. return resp
  54. }
  55. ////////////////////////////////////////////////////
  56. type AuthDebug struct{}
  57. func (s *AuthDebug) Validate(r *Request) *Response {
  58. if r.RPath() == "/register" {
  59. data := r.Data()
  60. if !data.KeyExists("login") {
  61. return ResponseErrKeyNotExists("login")
  62. }
  63. return nil
  64. } else if r.RPath() == "/login" {
  65. if !r.IsAuth() {
  66. return ResponseError(
  67. errors.New("NotAuth"),
  68. nil,
  69. 500,
  70. )
  71. }
  72. return nil
  73. }
  74. return ResponseNotFound(nil)
  75. }
  76. func (s *AuthDebug) Execute(r *Request) *Response {
  77. switch r.RPath() {
  78. case "/register":
  79. return s.execRegister(r)
  80. case "/login":
  81. return s.execLogin(r)
  82. }
  83. return nil
  84. }
  85. func (s *AuthDebug) execRegister(r *Request) *Response {
  86. log.Println("REDISTER")
  87. data := r.Data()
  88. token, err := r.GenerateToken(json.Map{
  89. "login": data.StringVal("login", ""),
  90. }, 0)
  91. if err != nil {
  92. return ResponseError(err, nil, 500)
  93. }
  94. resp := NewResponse()
  95. resp.KeySet("token", token)
  96. return resp
  97. }
  98. func (s *AuthDebug) execLogin(r *Request) *Response {
  99. log.Println("login", r.Auth())
  100. resp := NewResponse()
  101. return resp
  102. }