0x4a52466c696e74 5 месяцев назад
Родитель
Сommit
a011a53bd8
5 измененных файлов с 55 добавлено и 48 удалено
  1. 0 1
      example_chat/users/auth.go
  2. 9 1
      example_chat/users/register.go
  3. 37 46
      example_chat/zv_test.go
  4. 4 0
      request_stream.go
  5. 5 0
      rest_websocket/rest.go

+ 0 - 1
example_chat/users/auth.go

@@ -22,7 +22,6 @@ func (s *Auth) Validate(req rest.IRequestIn) rest.IRequestOut {
 }
 
 func (s *Auth) Execute(req rest.IRequestIn) rest.IRequestOut {
-	log.Println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO")
 	core := req.RCore().(*chat.Chat)
 	user, check := core.Auth(s.Name, s.Password)
 	if !check {

+ 9 - 1
example_chat/users/register.go

@@ -33,8 +33,16 @@ func (s *Register) Execute(req rest.IRequestIn) rest.IRequestOut {
 	if err != nil {
 		return req.OutError(err)
 	}
+	authMap := json.Map{
+		"id": user.ID,
+	}
+	token, tErr := req.GenerateToken(authMap, 0)
+	if err != nil {
+		return req.OutError(rest.ErrorMessage("ErrTokenGenerate", tErr.Error()))
+	}
 	req.SetAuth(json.Map{
-		"user": user,
+		"user":  user,
+		"token": token,
 	})
 	//time.Sleep(time.Second * 5)
 	return req.OutSuccess(fields, nil)

+ 37 - 46
example_chat/zv_test.go

@@ -4,7 +4,6 @@ import (
 	"testing"
 	"time"
 
-	"git.ali33.ru/fcg-xvii/go-tools/json"
 	"git.ali33.ru/fcg-xvii/rest"
 	"git.ali33.ru/fcg-xvii/rest/example_chat/chat"
 	ws "git.ali33.ru/fcg-xvii/rest/rest_websocket"
@@ -12,8 +11,7 @@ import (
 
 func TestChat(t *testing.T) {
 
-	addr := "127.0.0.1:40000"
-	clAddr := "ws://127.0.0.1:40000/ws"
+	addr := "192.168.88.27:40000"
 
 	core := chat.New()
 
@@ -27,54 +25,47 @@ func TestChat(t *testing.T) {
 	restEngine := ws.New(app, core)
 	restEngine.Prepare(server, "/ws")
 
+	ch := make(chan struct{})
+	<-ch
+
 	//ch := make(chan struct{})
 	//<-ch
 
-	cl, err := ws.NewClient(clAddr)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	t.Log(cl)
-
 	/*
-		mes := rest_websocket.(
-			"/users/register",
-			json.Map{
-				"name":     "LETO",
-				"password": "my-pass-99",
+		clAddr := "ws://127.0.0.1:40000/ws"
+		cl, err := ws.NewClient(clAddr)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		t.Log(cl)
+
+		mes := &rest.RequestStream{
+			Timeout: time.Now().Add(time.Second),
+			Request: &rest.Request{
+				Type:    rest.RequestTypeIn,
+				Command: "/users/register",
+				Data: json.Map{
+					"name":     "LETO",
+					"password": "my-pass-99",
+				},
 			},
-			nil,
-			time.Second*10,
-			rest.RequestTypeMessage,
-		)
+		}
+
+		answ, err := cl.SendMessage(mes)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		resp, ok := <-answ
+		if !ok {
+			t.Log("answer is not received")
+		} else {
+			t.Log("resp", resp.Data)
+		}
+
+		time.Sleep(time.Second * 5)
+		cl.Close()
 	*/
 
-	mes := &rest.RequestStream{
-		Timeout: time.Now().Add(time.Second),
-		Request: &rest.Request{
-			Type:    rest.RequestTypeIn,
-			Command: "/users/register",
-			Data: json.Map{
-				"name":     "LETO",
-				"password": "my-pass-99",
-			},
-		},
-	}
-
-	answ, err := cl.SendMessage(mes)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	resp, ok := <-answ
-	if !ok {
-		t.Log("answer is not received")
-	} else {
-		t.Log("resp", resp.Data)
-	}
-
-	time.Sleep(time.Second * 5)
-	cl.Close()
-	time.Sleep(time.Second * 60)
 }

+ 4 - 0
request_stream.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"log"
 	"time"
 )
 
@@ -23,11 +24,13 @@ func ReadRequestStream(r io.Reader) (*RequestStream, IErrorArgs) {
 	if err := ReadByte(r, "type", (*byte)(&req.Type)); err != nil {
 		return nil, err
 	}
+	log.Println("TYPE", req.Type)
 	if req.Type == RequestTypeIn || req.Type == RequestTypeOut {
 		// id
 		if err := ReadInt64(r, 2, "id", &req.ID); err != nil {
 			return nil, err
 		}
+		log.Println("ID", req.ID)
 		if req.Type == RequestTypeIn {
 			// timeout
 			var timeout int64
@@ -35,6 +38,7 @@ func ReadRequestStream(r io.Reader) (*RequestStream, IErrorArgs) {
 				return nil, err
 			}
 			req.Timeout = time.Unix(timeout, 0)
+			log.Println("TIMEOUT", req.Type)
 		}
 	}
 	// command

+ 5 - 0
rest_websocket/rest.go

@@ -13,6 +13,11 @@ func New(app rest.IApplicationStream, core any) *Rest {
 		upgrader: &websocket.Upgrader{
 			ReadBufferSize:  1024,
 			WriteBufferSize: 1024,
+			CheckOrigin: func(r *http.Request) bool {
+				// Проверьте ориджин здесь, и верните true, если он разрешен.
+				//return r.Header.Get("Origin") == "http://example.com"
+				return true
+			},
 		},
 		appConf: &appConfig{
 			app:  app,