Stan 1 день тому
батько
коміт
c0bc71fbb9
1 змінених файлів з 35 додано та 1 видалено
  1. 35 1
      http_server.go

+ 35 - 1
http_server.go

@@ -6,6 +6,7 @@ import (
 	"io"
 	"net"
 	"net/http"
+	"reflect"
 	"time"
 )
 
@@ -122,13 +123,17 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 		return
 	}
 
-	handler, ok := this.handlers[HTTPServerAction{Method: method, Path: requestStream.URL.Path}]
+	handlerSpec, ok := this.handlers[HTTPServerAction{Method: method, Path: requestStream.URL.Path}]
 	if !ok {
 		this.log.Error("handler not found", LogValue("path", requestStream.URL.Path))
 		responseStream.WriteHeader(http.StatusInternalServerError)
 		return
 	}
 
+	handler := HTTPServerHandler{}
+	copyStruct(handler, handlerSpec)
+	handlerSpec = handler
+
 	// Parse GET query
 	if method == METHOD_GET && handler.Data != nil {
 		query := map[string]any{}
@@ -249,3 +254,32 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 		}
 	}
 }
+
+func copyStruct(dst interface{}, src interface{}) {
+	dstVal := reflect.ValueOf(dst)
+	srcVal := reflect.ValueOf(src)
+
+	if dstVal.Kind() != reflect.Ptr || dstVal.Elem().Kind() != reflect.Struct {
+		return
+	}
+
+	if srcVal.Kind() == reflect.Ptr {
+		srcVal = srcVal.Elem()
+	}
+
+	if srcVal.Kind() != reflect.Struct {
+		return
+	}
+
+	dstVal = dstVal.Elem()
+	for i := 0; i < srcVal.NumField(); i++ {
+		srcField := srcVal.Field(i)
+		fieldName := srcVal.Type().Field(i).Name
+
+		dstField := dstVal.FieldByName(fieldName)
+
+		if dstField.IsValid() && dstField.CanSet() && dstField.Type() == srcField.Type() {
+			dstField.Set(srcField)
+		}
+	}
+}