0x4a52466c696e74 1 місяць тому
батько
коміт
2234848e28
4 змінених файлів з 72 додано та 34 видалено
  1. 3 29
      application/application.go
  2. 27 0
      request.go
  3. 37 0
      rest_gorm/request_list.go
  4. 5 5
      rest_websocket/zv_test.go

+ 3 - 29
application/application.go

@@ -2,8 +2,6 @@ package application
 
 import (
 	"context"
-	"log"
-	"sync/atomic"
 	"time"
 
 	"git.ali33.ru/fcg-xvii/rest"
@@ -13,7 +11,8 @@ import (
 
 func New(appConf *AppConfig, swaggerConf *SwaggerConf, ctx context.Context) rest.IApplication {
 	app := &Application{
-		conf: appConf,
+		conf:    appConf,
+		Sockets: NewSockets(),
 	}
 	if swaggerConf != nil && swaggerConf.Enabled {
 		app.swagger = NewSwagger(swaggerConf)
@@ -24,6 +23,7 @@ func New(appConf *AppConfig, swaggerConf *SwaggerConf, ctx context.Context) rest
 }
 
 type Application struct {
+	*Sockets
 	conf    *AppConfig
 	swagger *Swagger
 	//core      any
@@ -62,28 +62,6 @@ func (s *Application) Stop() {
 	s.cancel()
 }
 
-func (s *Application) work() {
-	var counter atomic.Int32
-	for {
-		select {
-		case <-s.ctx.Done():
-			return
-		case stream := <-s.chConnect:
-			log.Println("CONNECT-------", counter.Add(1))
-			if s.conf.OnSocketConnect != nil {
-				s.conf.OnSocketConnect(stream)
-			}
-			go func(rStream rest.IStream) {
-				<-rStream.Context().Done()
-				log.Println("DISCONNECT-------", counter.Add(-1))
-				if s.conf.OnSocketDisconnect != nil {
-					s.conf.OnSocketDisconnect(rStream)
-				}
-			}(stream)
-		}
-	}
-}
-
 func (s *Application) Executer(r rest.IRequestIn) (rest.IExecuter, bool) {
 	method := s.conf.GetCommandsMethod()
 	if command, check := method(r.RCommand()); check {
@@ -91,7 +69,3 @@ func (s *Application) Executer(r rest.IRequestIn) (rest.IExecuter, bool) {
 	}
 	return nil, false
 }
-
-func (s *Application) Connect() chan<- rest.IStream {
-	return s.chConnect
-}

+ 27 - 0
request.go

@@ -2,6 +2,7 @@ package rest
 
 import (
 	"io"
+	"time"
 
 	"git.ali33.ru/fcg-xvii/go-tools/json"
 )
@@ -181,3 +182,29 @@ type RequestOut struct {
 func (s *RequestOut) Write(w io.Writer) IErrorArgs {
 	return nil
 }
+
+//////////////////////////////////////////////////////
+
+func NewRequestStreamEvent(command string, data json.Map, files RequestFiles) IRequestOut {
+	return &RequestStream{
+		Timeout: time.Now().Add(time.Minute),
+		Request: &Request{
+			Type:    RequestTypeEvent,
+			Command: command,
+			Data:    data,
+			Files:   files,
+		},
+	}
+}
+
+func NewRequestStreamMessage(command string, data json.Map, files RequestFiles) IRequestOut {
+	return &RequestStream{
+		Timeout: time.Now().Add(time.Minute),
+		Request: &Request{
+			Type:    RequestTypeIn,
+			Command: command,
+			Data:    data,
+			Files:   files,
+		},
+	}
+}

+ 37 - 0
rest_gorm/request_list.go

@@ -2,6 +2,7 @@ package rest_gorm
 
 import (
 	"fmt"
+	"reflect"
 
 	"git.ali33.ru/fcg-xvii/rest"
 	"gorm.io/gorm"
@@ -83,3 +84,39 @@ func (s *List) Result(pg *gorm.DB, fields rest.FieldNamesList, res any) (count i
 	}
 	return
 }
+
+func toAnySlice(slice any) []any {
+	v := reflect.ValueOf(slice)
+
+	// Check if the input is a slice
+	if v.Kind() != reflect.Slice {
+		return nil
+	}
+
+	result := make([]any, v.Len())
+	for i := 0; i < v.Len(); i++ {
+		result[i] = v.Index(i).Interface()
+	}
+	return result
+}
+
+type ResultList struct {
+	Items  []any `json:"Items" rest:"fixed"`
+	Offset int   `json:"Offset" rest:"fixed"`
+	Limit  int   `json:"Limit" rest:"fixed"`
+	Count  int64 `json:"Count" rest:"fixed"`
+}
+
+func (s *List) ResultAnswer(pg *gorm.DB, fields rest.FieldNamesList, res any, offset, limit int) (*ResultList, rest.IErrorArgs) {
+	count, err := s.Result(pg, fields, res)
+	if err != nil {
+		return nil, err
+	}
+	rList := &ResultList{
+		Items:  toAnySlice(res),
+		Offset: offset,
+		Limit:  limit,
+		Count:  count,
+	}
+	return rList, nil
+}

+ 5 - 5
rest_websocket/zv_test.go

@@ -24,9 +24,9 @@ func TestMessage(t *testing.T) {
 
 func TestMessagePack(t *testing.T) {
 
-	//m := rest.Message{
-	//	Type: rest.MessageTypeText,
-	//	Data: []byte("Hello"),
-	//}
-	//t.Log(m
+	/*m := rest.Message{
+		//	Type: rest.MessageTypeText,
+		//	Data: []byte("Hello"),
+	}
+	//t.Log(m*/
 }