Jelajahi Sumber

in progress

0x4a52466c696e74 1 tahun lalu
induk
melakukan
f04fac4d99
2 mengubah file dengan 14 tambahan dan 7 penghapusan
  1. 1 1
      example/application.go
  2. 13 6
      rest_http/rest.go

+ 1 - 1
example/application.go

@@ -70,7 +70,7 @@ func ExampleNew() {
 	)
 	// создаем сервер
 	server := rest.NewServer("localhost:8080", []byte("top-secret"))
-	restServ := rest_http.New(App, App)
+	restServ := rest_http.New(App, App, nil)
 	restServ.Prepare(server, "/")
 	// пробуем запустить его. Если через секунду запуск не удался, будет возвращена ошибка
 	if err := server.Listen(time.Second); err != nil {

+ 13 - 6
rest_http/rest.go

@@ -13,17 +13,19 @@ import (
 	jwt "github.com/dgrijalva/jwt-go"
 )
 
-func New(app rest.IApplication, core any) *Rest {
+func New(app rest.IApplication, core any, responseHeaders map[string]string) *Rest {
 	return &Rest{
-		app:  app,
-		core: core,
+		app:             app,
+		core:            core,
+		responseHeaders: responseHeaders,
 	}
 }
 
 type Rest struct {
-	server rest.IServer
-	app    rest.IApplication
-	core   any
+	server          rest.IServer
+	app             rest.IApplication
+	core            any
+	responseHeaders map[string]string
 }
 
 func (s *Rest) App() rest.IApplication {
@@ -46,6 +48,11 @@ func responseError(w http.ResponseWriter, err rest.IErrorArgs, code int) {
 
 // handle
 func (s *Rest) handle(w http.ResponseWriter, r *http.Request) {
+	// установка заголовков ответа
+	for k, v := range s.responseHeaders {
+		w.Header().Set(k, v)
+	}
+
 	// Инициализация rest Request
 	rr := &RequestIn{
 		RequestIn: &rest.RequestIn{