123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package rest
- import (
- "bytes"
- "errors"
- "io"
- "log"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- type ObjectDemo struct{}
- func (s *ObjectDemo) Validate(r *Request) *Response {
- key := r.data.KeysExists([]string{"get-one", "get-two"})
- if key != "" {
- return ResponseErrKeyNotExists(key)
- }
- if !r.data.IsBoolean("get-one") {
- return ResponseErrKeyInvalidType("get-one", "boolean")
- }
- if !r.data.IsBoolean("get-two") {
- return ResponseErrKeyInvalidType("get-two", "boolean")
- }
- return nil
- }
- func (s *ObjectDemo) Execute(r *Request) *Response {
- if fKeys := r.FileKeys(); len(fKeys) > 0 {
- log.Println("FILES", fKeys)
- tFile, _ := r.File(fKeys[0])
- fContent, _ := io.ReadAll(tFile)
- log.Println("FILE CONTENT", string(fContent))
- }
- resp := NewResponse()
- var fileNames []string
- if r.data.Bool("get-one", true) {
- fileNames = append(fileNames, "get-one.txt")
- buf := bytes.NewBuffer([]byte("get-one txt file :)"))
- resp.FileSet("get-one.txt", io.NopCloser(buf))
- }
- if r.data.Bool("get-two", true) {
- fileNames = append(fileNames, "get-two.txt")
- buf := bytes.NewBuffer([]byte("get-two txt file :)"))
- resp.FileSet("get-two.txt", io.NopCloser(buf))
- }
- resp.KeySet("file_names", fileNames)
- return resp
- }
- ////////////////////////////////////////////////////
- type JSONDebug struct{}
- func (s *JSONDebug) Validate(r *Request) *Response {
- return nil
- }
- func (s *JSONDebug) Execute(r *Request) *Response {
- resp := NewResponse()
- resp.KeySet("completed", true)
- return resp
- }
- ////////////////////////////////////////////////////
- type AuthDebug struct{}
- func (s *AuthDebug) Validate(r *Request) *Response {
- if r.RPath() == "/register" {
- data := r.Data()
- if !data.KeyExists("login") {
- return ResponseErrKeyNotExists("login")
- }
- return nil
- } else if r.RPath() == "/login" {
- if !r.IsAuth() {
- return ResponseError(
- errors.New("NotAuth"),
- nil,
- 500,
- )
- }
- return nil
- }
- return ResponseNotFound(nil)
- }
- func (s *AuthDebug) Execute(r *Request) *Response {
- switch r.RPath() {
- case "/register":
- return s.execRegister(r)
- case "/login":
- return s.execLogin(r)
- }
- return nil
- }
- func (s *AuthDebug) execRegister(r *Request) *Response {
- log.Println("REDISTER")
- data := r.Data()
- token, err := r.GenerateToken(json.Map{
- "login": data.StringVal("login", ""),
- }, 0)
- if err != nil {
- return ResponseError(err, nil, 500)
- }
- resp := NewResponse()
- resp.KeySet("token", token)
- return resp
- }
- func (s *AuthDebug) execLogin(r *Request) *Response {
- log.Println("login", r.Auth())
- resp := NewResponse()
- return resp
- }
|