0x4a52466c696e74 il y a 1 an
Parent
commit
73f03915de
4 fichiers modifiés avec 21 ajouts et 3 suppressions
  1. 1 1
      example/application.go
  2. 10 0
      request.go
  3. 5 0
      rest_http/request.go
  4. 5 2
      rest_http/rest.go

+ 1 - 1
example/application.go

@@ -69,7 +69,7 @@ func ExampleNew() {
 		},
 	)
 	// создаем сервер
-	restServ := rest_http.New(App)
+	restServ := rest_http.New(App, App)
 	// пробуем запустить его. Если через секунду запуск не удался, будет возвращена ошибка
 	if err := restServ.Listen(time.Second); err != nil {
 		// ошибка запуска

+ 10 - 0
request.go

@@ -15,6 +15,7 @@ type IRequest interface {
 	File(name string) (io.Reader, bool)
 	GenerateToken(data json.Map, expire int64) (string, error)
 	Root() any
+	Core() any
 	ResponseSuccess(data json.Map, files map[string]io.ReadCloser) IResponse
 	ResponseError(code int, err IErrorArgs) IResponse
 }
@@ -25,6 +26,7 @@ type Request struct {
 	data          json.Map `json:"data"`
 	fileKeys      []string `json:"file_keys"`
 	files         map[string]io.Reader
+	core          any
 	root          any
 	generateToken func(data json.Map, expire int64) (string, error)
 	//Response      func(data json.Map, files map[string]io.ReadCloser) IResponse
@@ -52,6 +54,10 @@ func (s *Request) SetRoot(root any) {
 	s.root = root
 }
 
+func (s *Request) SetCore(core any) {
+	s.core = core
+}
+
 func (s *Request) SetGenerateToken(generateToken func(data json.Map, expire int64) (string, error)) {
 	s.generateToken = generateToken
 }
@@ -87,6 +93,10 @@ func (s *Request) Root() any {
 	return s.root
 }
 
+func (s *Request) Core() any {
+	return s.core
+}
+
 func (s *Request) ResponseSuccess(data json.Map, files map[string]io.ReadCloser) IResponse {
 	return &Response{
 		code:  200,

+ 5 - 0
rest_http/request.go

@@ -11,6 +11,7 @@ import (
 // Request реализует объект запроса
 type Request struct {
 	*http.Request
+	core           any
 	auth           json.Map
 	data           json.Map
 	files          map[string]io.ReadCloser
@@ -21,6 +22,10 @@ func (s *Request) Root() any {
 	return s.Request
 }
 
+func (s *Request) Core() any {
+	return s.core
+}
+
 // GenerateToken создает новый токен авторизации. expire - timestamp даты, после которой токен не будет действителен (если указан 0 - токен бессрочный)
 func (s *Request) GenerateToken(data json.Map, expire int64) (string, error) {
 	return s.tokenGenerator(data, expire)

+ 5 - 2
rest_http/rest.go

@@ -18,9 +18,10 @@ import (
 	jwt "github.com/dgrijalva/jwt-go"
 )
 
-func New(app rest.IApplication) *Rest {
+func New(app rest.IApplication, core any) *Rest {
 	return &Rest{
-		app: app,
+		app:  app,
+		core: core,
 	}
 }
 
@@ -28,6 +29,7 @@ type Rest struct {
 	opened atomic.Bool
 	server *http.Server
 	app    rest.IApplication
+	core   any
 }
 
 func (s *Rest) App() rest.IApplication {
@@ -91,6 +93,7 @@ func (s *Rest) handle(w http.ResponseWriter, r *http.Request) {
 	// Инициализация restRequest
 	rr := &Request{
 		Request:        r,
+		core:           s.core,
 		data:           mjson.Map{},
 		files:          make(map[string]io.ReadCloser),
 		tokenGenerator: s.TokenGenerate,